在PHP中显示小数点数

时间:2022-06-02 17:08:24

I hold decimals in a database using DECIMAL(10,5)

我使用DECIMAL(10,5)在数据库中保存小数

I would like to format these numbers according to a few rules:

我想根据一些规则格式化这些数字:

  • A zero decimal should display as 0
  • 零小数应显示为0

  • Show a long decimal (no trailing zero's) with all of it's numbers
  • 显示一个长十进制(没有尾随零)的所有数字

  • When possible, I would like to only show up to 2 decimal places (when there are trailing zeros)
  • 如果可能的话,我想只显示2位小数(当有尾随零时)

Here are some examples:

这里有些例子:

  • The left side corresponds to how the number is stored in database.
  • 左侧对应于数字在数据库中的存储方式。

  • The right number is how I would like to display the number in my application.
  • 正确的数字是我想在我的应用程序中显示数字的方式。


0.00000 => 0
0.51231 => 0.51231
0.12000 => 0.12
0.40000 => 0.40
0.67800 => 0.678
12.10000 => 12.10

5 个解决方案

#1


2  

This will work for you:

这对你有用:

function format($x){
    if(!(int)substr_replace($x, '', $dpos = strpos($x, '.'), 1))
        return 0;
    else
        return str_pad((rtrim($x, '0')), $dpos + 3, '0'); 
}

Example

#2


1  

Well here's one way (I haven't tested it yet so there may be minor errors):

那么这是一种方式(我还没有测试它,所以可能会有一些小错误):

$pattern = '/([0-9]+)\\.{0,1}([0-9]*?)0*$/';
$subject = 12.10000;
$matches = array();
$result = preg_match ($pattern, $subject, $matches);
$number = $matches[1];
if ($matches[2] != 0) {
    $number .= '.'.$matches[2];
    if ($matches[2] < 10) {
        $number .= '0';
    }
}
echo $number;

And here's another way (probably a little faster):

这是另一种方式(可能更快一点):

$x = 1.000;
$result = (int)$x;

$trimmed = rtrim($x, 0);
if ($trimmed[strlen($trimmed) - 1] != '.') {
    if ($trimmed[strlen($trimmed) - 2] == '.') {
        $result = $trimmed.'0';
    } else {
        $result = $trimmed;
    }
}
echo $result;

#3


0  

I would utilize the number_format function in php to actually do the formatting after you determine the amount of decimal places to the number has.

在确定数字的小数位数后,我会利用php中的number_format函数实际进行格式化。

Source: http://php.net/manual/en/function.number-format.php

Example Usage:

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

#4


0  

I haven't used it myself, but theres the NumberFormatter class: http://php.net/manual/class.numberformatter.php as part of the Internationalization Functions for this stuff. Using that is a little more involved i think though.

我自己没有使用它,但是还有NumberFormatter类:http://php.net/manual/class.numberformatter.php作为这个东西的国际化功能的一部分。尽管如此,使用它还是更复杂一些。

#5


0  

I know this is an old question, but the following quick function I wrote for my own project might help someone looking for this.

我知道这是一个老问题,但我为自己的项目编写的以下快速功能可能会帮助有人找到这个。

function number_format_least_dp($number, $decimal_point = '.', $thousand_seperator = ','){
    if (floatval($number) == (int)$number){
        $number = number_format($number, 0, $decimal_point, $thousand_seperator);
    } else {
        $number = rtrim($number, '.0');
        $number = number_format($number, strlen(substr(strrchr($number, '.'), 1)), $decimal_point, $thousand_seperator);
    }
    return $number;
}

#1


2  

This will work for you:

这对你有用:

function format($x){
    if(!(int)substr_replace($x, '', $dpos = strpos($x, '.'), 1))
        return 0;
    else
        return str_pad((rtrim($x, '0')), $dpos + 3, '0'); 
}

Example

#2


1  

Well here's one way (I haven't tested it yet so there may be minor errors):

那么这是一种方式(我还没有测试它,所以可能会有一些小错误):

$pattern = '/([0-9]+)\\.{0,1}([0-9]*?)0*$/';
$subject = 12.10000;
$matches = array();
$result = preg_match ($pattern, $subject, $matches);
$number = $matches[1];
if ($matches[2] != 0) {
    $number .= '.'.$matches[2];
    if ($matches[2] < 10) {
        $number .= '0';
    }
}
echo $number;

And here's another way (probably a little faster):

这是另一种方式(可能更快一点):

$x = 1.000;
$result = (int)$x;

$trimmed = rtrim($x, 0);
if ($trimmed[strlen($trimmed) - 1] != '.') {
    if ($trimmed[strlen($trimmed) - 2] == '.') {
        $result = $trimmed.'0';
    } else {
        $result = $trimmed;
    }
}
echo $result;

#3


0  

I would utilize the number_format function in php to actually do the formatting after you determine the amount of decimal places to the number has.

在确定数字的小数位数后,我会利用php中的number_format函数实际进行格式化。

Source: http://php.net/manual/en/function.number-format.php

Example Usage:

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

#4


0  

I haven't used it myself, but theres the NumberFormatter class: http://php.net/manual/class.numberformatter.php as part of the Internationalization Functions for this stuff. Using that is a little more involved i think though.

我自己没有使用它,但是还有NumberFormatter类:http://php.net/manual/class.numberformatter.php作为这个东西的国际化功能的一部分。尽管如此,使用它还是更复杂一些。

#5


0  

I know this is an old question, but the following quick function I wrote for my own project might help someone looking for this.

我知道这是一个老问题,但我为自己的项目编写的以下快速功能可能会帮助有人找到这个。

function number_format_least_dp($number, $decimal_point = '.', $thousand_seperator = ','){
    if (floatval($number) == (int)$number){
        $number = number_format($number, 0, $decimal_point, $thousand_seperator);
    } else {
        $number = rtrim($number, '.0');
        $number = number_format($number, strlen(substr(strrchr($number, '.'), 1)), $decimal_point, $thousand_seperator);
    }
    return $number;
}