php 浮点数后最多显示 2 位数字……仅当它是具有 2 个以上浮点数的浮点数时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17771621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:24:34  来源:igfitidea点击:

show 2 digits max after floating point ... only if it is a float number with more than 2 float digits

phpmathfloating-point

提问by max

in my app i do some math and the result can be float or int

在我的应用程序中,我做了一些数学运算,结果可以是 float 或 int

i want to show the final result with two digit after the decimal point max ... if result is a float number

我想显示小数点后两位最大的最终结果......如果结果是一个浮点数

there are two options to do this

有两种选择可以做到这一点

number_format($final ,2);

and

sprintf ("%.2f", $final );

but problem is ... if my final result is a int like 25i end up with

但问题是......如果我的最终结果是一个像25我一样的 int

25.00 

or if final result is some thing like 12.3it gives me

或者如果最终结果是12.3它给我的东西

12.30

and i dont want that

我不想要那个

is there any way to format a number to show 2 digits after float point ONLY IF it's a float number with more than 2 digits after decimal point ? or should i do some checking before formatting my number ?

有没有办法格式化一个数字以在浮点数后显示 2 位数字,前提是它是一个小数点后超过 2 位数字的浮点数?或者我应该在格式化我的号码之前做一些检查?

回答by user4035

<?php
$number = 25;
print round($number, 2);

print "\n";

$number = 25.3;
print round($number, 2);

print "\n";

$number = 25.33;
print round($number, 2);

prints:

印刷:

25
25.3
25.33

回答by rnbguy

I think there is no such short bypass for it.
Manually check if it has 2 or more digits after decimal.

我认为没有这么短的旁路。
手动检查小数点后是否有 2 位或更多位。

How to know if it has less one or zero digits after decimal ? Just multiply with 10 and check if it is an integer. If it is, print the number as it is. If it's now use '%.2f' to print it.

如何知道小数点后是少一位还是零位?只需乘以 10 并检查它是否为整数。如果是,则按原样打印数字。如果现在使用 '%.2f' 打印它。

回答by SmootQ

You have to add an if condition, if it has more than 0 digits after the point. I don't see any other solution.

你必须添加一个 if 条件,如果它在点后有超过 0 个数字。我没有看到任何其他解决方案。

A simple and fast way to do this.

一个简单而快速的方法来做到这一点。

$final=3.40;
$decimalNbr= strlen(substr(strrchr($final, "."), 1));
$final = number_format($final,(is_float($final) ? (($decimalNbr>2) ? 2 : $decimalNbr) : 0));


echo $final;

Keep in mind too, that I added another decimal digits count before using the number_format.

还要记住,我在使用 number_format 之前添加了另一个十进制数字计数。

回答by bitWorking

I found another option. Cast to floatto strip the trailing zeros:

我找到了另一种选择。强制转换float为去除尾随零:

echo (float)sprintf("%.2f", $final);
// or
echo (float)number_format($final ,2);

But these functions seems to round the number just like round:

但这些函数似乎四舍五入数字就像round

echo sprintf("%.2f", 12.556); // 12.56
echo number_format(12.556, 2); // 12.56

So if you don't want this behaviour use this:

因此,如果您不希望这种行为,请使用以下命令:

$final = 12.556;
echo (int)(($final*100))/100; // 12.55

echo (int)((12*100))/100; // 12
echo (int)((12.3*100))/100; // 12.3
echo (int)((12.34567*100))/100; // 12.34

回答by Santosh Kumar Singh

You can use this example

你可以使用这个例子

function num_format($number,$precision=0)
{
    $precision = ($precision == 0 ? 1 : $precision);    
    $pow = pow(10, $precision);
    $value = (int)((trim($number)*$pow))/$pow;

    return number_format($value,$precision);
}
echo num_format(71730116.048758);
//Output
//71,730,116.04

回答by KRISHNA MK

Try below function to maintain a constant 2 digits after decimal point

尝试下面的函数来保持小数点后的 2 位数不变

in case you want to change the number of digits change AFTER_DESIMAL_DIGITS value

如果您想更改位数更改 AFTER_DESIMAL_DIGITS 值

function decimal_digit_maintainer($value){
    define("AFTER_DESIMAL_DIGITS ",2);
    $value_array = explode('.',$value);
    if(isset($value_array[1])){
        $value_array[1] =  str_pad($value_array[1],AFTER_DESIMAL_DIGITS,'0',STR_PAD_RIGHT);
    }
    return implode('.',$value_array);                                       
}

回答by Anthony Sterling

I don't like this.

我不喜欢这个。

<?php
function format($number) {
    return preg_replace(
        '~\.[0-9]0+$~',
        null,
        sprintf('%.2f', $number)
    );
}

echo format(23), PHP_EOL;       //23
echo format(23.3), PHP_EOL;     //23
echo format(23.33), PHP_EOL;    //23.33

回答by Ivan0x32

Yes, you should do some checking. For example check if ceil($number) > floor($number);If you need specificly two digits, that is going to take more effort.

是的,你应该做一些检查。例如检查ceil($number) > floor($number);如果您需要特定的两位数,那将需要更多的努力。