使用php将科学记数法转换为十进制
比特币的数学给了我很多问题使用php将科学记数法转换为十进制,php,Php,比特币的数学给了我很多问题 $value = bcmul((float)$TotalMoney, $p,8); $value = bcdiv((float)$Value, 100,8); 返回8.431e-05作为脚本中的值之一 我试过了 $newNum = (float)$value; $newNum = number_format((float)$value, 8); $newNum = sprintf('%.8f',$value); function
$value = bcmul((float)$TotalMoney, $p,8);
$value = bcdiv((float)$Value, 100,8);
返回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);
在phpfiddle中试用过,效果很好。可能问题在于将其存储在数据库中。它在数据库中存储为8.431e-05
我做错了什么?在处理比特币余额时,建议将金额作为整数存储在satoshis的数据库中,然后在屏幕上向用户显示时将其转换回8位小数
$amount = 0.0132;
$convert = $amount * 100000000;
// store in DB as the converted amount 1320000 as an integer
// when grabbing from DB convert it back
$databaseValue = 1320000;
$convertBack = $databaseValue / 100000000;
$display = number_format($convertBack, 8);
echo $display;
使用以下示例在PHP上将科学符号转换为浮点/十进制:
我只是用
您的PHP的实际情况:
$n=pow(71663616,2);
echo $n //5.1356738581955E+15
现在调用十进制表示法:
$n=pow(71663616,2);
echo number_format($n) //5,135,673,858,195,456
sprintf“%.8f”,floatval$值;为我工作。这不适用于小数位数未知的任意数字。@Dukecz给我们举个例子$n=0.0000015684;echo$n。PHP_EOL;回声编号\u格式$n;产生:1.5684E-6和0,因为数字_格式需要已知的小数位数
$n=pow(71663616,2);
echo number_format($n) //5,135,673,858,195,456