用php将科学记数法转换为十进制

use*_*607 0 php

比特币的数学给我带来了问题

        $value = bcmul((float)$TotalMoney, $p,8);
        $value = bcdiv((float)$Value, 100,8);
Run Code Online (Sandbox Code Playgroud)

返回 8.431e-05 作为脚本中的值之一

我试过了

$newNum = (float)$value; 
$newNum = number_format((float)$value, 8); 
$newNum = sprintf('%.8f',$value);

function scientific_notation($in_float_value, $in_decimal_place_count = -1) 
{

  // Get the exponent
  $abs_float_value = abs($in_float_value);
  $exponent = floor($abs_float_value == 0 ? 0 : log10($abs_float_value));
  // Scale to get the mantissa
  $in_float_value *= pow(10, -$exponent);
  // Create the format string based 
  // on the requested number of decimal places.
  $format = ($in_decimal_place_count >= 0) ? "." . $in_decimal_place_count : "";
  //echo("Format0: $format");
  // Format the exponent part using zero padding.
  $formatted_exponent = "+" . sprintf("%02d", $exponent);
  if($exponent < 0.0)
  {
      $formatted_exponent = "-" . sprintf("%02d", -$exponent);
  }
  $format = "%" . $format . "fe%s";
  //echo("Format1: $format");
  // Return the final value combining mantissa and exponent
  return sprintf($format, $in_float_value, $exponent);

}
$newNum = scientific_notation($value,8);
Run Code Online (Sandbox Code Playgroud)

在 phpfiddle 中尝试过,它有效。也许问题是将它存储在数据库中。它在数据库中存储为 8.431e-05

我究竟做错了什么?

小智 6

使用下面的示例在 PHP 上将科学记数法转换为浮点数/十进制数:

echo sprintf('%f', floatval('-1.0E-5'));//default 6 decimal places
echo sprintf('%.8f', floatval('-1.0E-5'));//force 8 decimal places
echo rtrim(sprintf('%f',floatval(-1.0E-5)),'0');//remove trailing zeros
Run Code Online (Sandbox Code Playgroud)