簡體   English   中英

PHP字符串浮起奇怪

[英]PHP string to float strange

我不明白結果嗎? 29.85-3.5527136788005E-15 PHP錯誤? 與floatval或cast的結果相同,您能幫我嗎? 這里的簡單示例代碼:

<?php 
$new = array('29.85');
$two = array('6.95','9.95','12.95');
$montantTotal = 0 ;
foreach ($two as $lignes) {
    $montantTotal += floatval($lignes);
    }
echo $montantTotal ;
$result = $montantTotal - floatval($new[0]);
echo $result;

您可以使用number_format

句法

string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )

    $new = array('29.85');
    $two = array('6.95','9.95','12.95');
    $montantTotal = 0 ;
    foreach ($two as $lignes) {
        $montantTotal += (float)$lignes;
        }
   // echo $montantTotal ; echo "<br>";
    $result = number_format($montantTotal,8,".","") - number_format((float)$new[0],8,".","");
    echo $result;

輸出量

0

結果很小。 它使用科學計數法。

 $montantTotal =  29.85
 $result = -3.5527136788005E-15

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM