2016-06-16 48 views
0

數學與比特幣給我的問題轉換科學記數法用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 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在數據庫中

我做錯了什麼?

+0

'sprintf('%。8f',floatval($ value));'爲我工作。 –

回答

1

在使用比特幣餘額時,建議將數量作爲整數存儲在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; 
相關問題