如何使number_format()不向上舍入数字

Psy*_*che 22 php

我有这个号码:

$double = '21.188624';
Run Code Online (Sandbox Code Playgroud)

使用后number_format($double, 2, ',', ' ')我得到:

21,19
Run Code Online (Sandbox Code Playgroud)

但我想要的是:

21,18
Run Code Online (Sandbox Code Playgroud)

任何想法我怎样才能使这项工作?

谢谢.

Wri*_*ken 32

number_format 将永远这样做,你唯一的解决方案是提供不同的东西:

$number = intval(($number*100))/100;
Run Code Online (Sandbox Code Playgroud)

要么:

$number = floor(($number*100))/100;
Run Code Online (Sandbox Code Playgroud)

  • 试试这个号码:0.29,它就失败了! (3认同)

ust*_*tro 8

我知道这是一个老问题,但它仍然是实际的:).

这个功能怎么样?

function numberFormatPrecision($number, $precision = 2, $separator = '.')
{
    $numberParts = explode($separator, $number);
    $response = $numberParts[0];
    if(count($numberParts)>1){
        $response .= $separator;
        $response .= substr($numberParts[1], 0, $precision);
    }
    return $response;
}
Run Code Online (Sandbox Code Playgroud)

用法:

// numbers test
numberFormatPrecision(19, 2, '.'); // expected 19 return 19
numberFormatPrecision(19.1, 2, '.'); //expected 19.1 return 19.1
numberFormatPrecision(19.123456, 2, '.'); //expected 19.12 return 19.12

// negative numbers test
numberFormatPrecision(-19, 2, '.'); // expected -19 return -19
numberFormatPrecision(-19.1, 2, '.'); //expected -19.1 return -19.1
numberFormatPrecision(-19.123456, 2, '.'); //expected -19.12 return -19.12

// precision test
numberFormatPrecision(-19.123456, 4, '.'); //expected -19.1234 return -19.1234

// separator test
numberFormatPrecision('-19,123456', 3, ','); //expected -19,123 return -19,123  -- comma separator
Run Code Online (Sandbox Code Playgroud)


Der*_*zed 8

功能(仅精度):

function numberPrecision($number, $decimals = 0)
{
    $negation = ($number < 0) ? (-1) : 1;
    $coefficient = pow(10, $decimals);
    return $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
}
Run Code Online (Sandbox Code Playgroud)

例子:

numberPrecision(2557.9999, 2);     // returns 2557.99
numberPrecision(2557.9999, 10);    // returns 2557.9999
numberPrecision(2557.9999, 0);     // returns 2557
numberPrecision(2557.9999, -2);    // returns 2500
numberPrecision(2557.9999, -10);   // returns 0
numberPrecision(-2557.9999, 2);    // returns -2557.99
numberPrecision(-2557.9999, 10);   // returns -2557.9999
numberPrecision(-2557.9999, 0);    // returns -2557
numberPrecision(-2557.9999, -2);   // returns -2500
numberPrecision(-2557.9999, -10);  // returns 0
Run Code Online (Sandbox Code Playgroud)

功能(全功能):

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);
}
Run Code Online (Sandbox Code Playgroud)

例子:

numberFormat(2557.9999, 2, ',', ' ');     // returns 2 557,99
numberFormat(2557.9999, 10, ',', ' ');    // returns 2 557,9999000000
numberFormat(2557.9999, 0, ',', ' ');     // returns 2 557
numberFormat(2557.9999, -2, ',', ' ');    // returns 2 500
numberFormat(2557.9999, -10, ',', ' ');   // returns 0
numberFormat(-2557.9999, 2, ',', ' ');    // returns -2 557,99
numberFormat(-2557.9999, 10, ',', ' ');   // returns -2 557,9999000000
numberFormat(-2557.9999, 0, ',', ' ');    // returns -2 557
numberFormat(-2557.9999, -2, ',', ' ');   // returns -2 500
numberFormat(-2557.9999, -10, ',', ' ');  // returns 0
Run Code Online (Sandbox Code Playgroud)


T30*_*T30 5

我使用这个功能:

function cutNum($num, $precision = 2) {
    return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

cutNum(5)          //returns 5 
cutNum(5.6789)     //returns 5.67 (default precision is two decimals)
cutNum(5.6789, 3)  //returns 5.678
cutNum(5.6789, 10) //returns 5.6789
cutNum(5.6789, 0)  //returns 5. (!don't use with zero as second argument: use floor instead!)
Run Code Online (Sandbox Code Playgroud)

说明:这里你有相同的功能,只是更详细地帮助理解它的行为:

function cutNum($num, $precision = 2) {
    $integerPart = floor($num);
    $decimalPart = str_replace($integerPart, '', $num);
    $trimmedDecimal = substr($decimalPart, 0, $precision + 1);
    return $integerPart . $trimmedDecimal;
}
Run Code Online (Sandbox Code Playgroud)

  • 你的功能不正确。99.997092 应该返回 99.99 时将返回 99.70。这是正确的功能。https://pastebin.com/6i9X536G (2认同)