簡體   English   中英

PHP number_format是四舍五入的?

[英]PHP number_format is rounding?

我有一個價格“0,10”或“00000,10”

現在,當我嘗試

number_format($price, 2, ',', '')

我得到0,00。 我怎樣才能解決這個問題? 我想要0,10美元。 我不想舍入。

或者當我有5,678時,我得到5,68。 但我想要5,67。

你可以增加數的大小舍去與前地板

$price = floor($price * 100) / 100;
$formatted = number_format($price, 2, ',', '');

另一種解決方案,它可以提供更好的精度,因為它避免了浮點運算,是用三位小數格式化它並丟棄格式化后的最后一位數:

$formatted = substr(number_format($price, 3, ',', ''), 0, -1);

有幾個人提到將它四舍五入到3然后刪掉最后一個字符。 這實際上不起作用。 假設您有2.9999並將其四舍五入至3.000。

這仍然不准確,最好的解決方案是:

$price = '5.678';
$dec = 2;
$price = number_format(floor($price*pow(10,$dec))/pow(10,$dec),$dec);

這樣做取價格並乘以100(10 ^十進制)得到567.8,然后我們用floor來得到它567,然后我們將它除以100得到5.67

你應該使用str_replace將逗號填充的數字轉換回正常的小數。 $ number = str_replace(“,”,“。”,$ number); 然后你可以使用number_format

"00000,10"是一個字符串。 你應該小數點。 要獲得所需的行為,您可以使用:

echo substr(number_format(str_replace(',', '.', $price), 3, ',', ''), 0, -1);

我的問題是html驗證器錯誤messege thar number_format()參數不是double。

我通過為該參數放置floatval來解決這個錯誤消息,例如number_format(floatval($var),2,'.',' ') ,這樣做很好。

就在執行number_format之前,字符串“0,10”被php轉換為數字。 因為php總是使用英語符號,所以它不會用逗號來表示。

echo "4 apples" + 2;  
output: 6

“apples”部分被忽略 ,就像你的“,10”被忽略一樣。

將“,”轉換為“。” 允許php查看其他數字。

$price = str_replace(',', '.', '0,10');
number_format($price, 2, ',', '');

使用此(需要激活的intl PHP擴展)

$numberFmtCurrency = new NumberFormatter('de_AT', NumberFormatter::CURRENCY);
$numberFmtCurrency->setAttribute(NumberFormatter::ROUNDING_INCREMENT, 0);
$numberFmtCurrency->formatCurrency(328.13, 'EUR'); // prints € 328.13 (and not 328.15)

如果你只是想要清除前導零而只是限制長度,而不是舍入到一定數量的小數位,一個更通用的解決方案可能是這個函數:

    function cutafter($string,$cutpoint,$length)
    {
        $temp = explode($cutpoint,$string);
        $int = $temp[0];
        $sub = $temp[1];
        return number_format($int,0).','.substr($sub,0,$length);
    }

例:

    $number = "005,678";
    $answer = cutafter($number,",",2);

$ answer現在等於“5,67”

function format_numeric($value) {

    if (is_numeric($value)) { // is number

        if (strstr($value, ".")) { // is decimal

            $tmp = explode(".", $value);
            $int = empty($tmp[0]) ? '0' : $tmp[0];
            $dec = $tmp[1];
            $value = number_format($int, 0) . "." . $dec;
            return $value;
        }

        $value =  number_format($value);
        return $value;
    }

    return $value; // is string
}

Unit Testing:

Passed / 1100000 => 1,100,000
Passed / ".9987" => .9987
Passed / 1100.22 => 1,100.22
Passed / 0.9987 => 0.9987
Passed / .9987 => 0.9987
Passed / 11 => 11
Passed / 11.1 => 11.1
Passed / 11.1111 => 11.1111
Passed / "abc" => "abc" 

有關詳細信息,請參閱此答案

function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
    $negation = ($number < 0) ? (-1) : 1;
    $coefficient = pow(10, $decimals);
    $number = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
    return number_format($number, $decimals, $decPoint, $thousandsSep);
}

暫無
暫無

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

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