删除两个小数点后的数字,而不对值进行四舍五入 [英] Delete digits after two decimal points, without rounding the value

查看:33
本文介绍了删除两个小数点后的数字,而不对值进行四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 php 变量中有这样的值

$var='2.500000550';回声 $var

我想要的是删除2位数字后的所有小数点.

现在变量的值将是

$var='2.50';回声 $var

<块引用>

记住这个值来自mysql数据库

但是当我使用 round php 函数 时,我得到了圆形但我不需要圆形,我只需要简单地删除 2 位小数后的所有数字.

我累了,flot() 和很多其他选项都没有成功.

谢谢

解决方案

TL;DR:

PHP 原生函数 bcdiv 似乎完全符合要求且正确.

要简单地截断"一个数字,bcdiv($var, 1, 2); 其中 2 是要保留的小数位数(1 是分母 - 将数字除以 1 允许您只需将原始数字截断到所需的小数位)

完整答案(历史记录)

事实证明这比人们想象的更难以捉摸.

在这个答案(错误地)获得了相当多的支持后,我注意到即使是 sprintf 也会四舍五入.

我没有删除这个答案,而是将它变成对每个提议的解决方案的更可靠的解释/讨论.

number_format - 不正确.(回合)
尝试使用数字格式:

$var = number_format($var, 2, '.', '');//最后两个参数是可选的回声 $var;//输出 2.50

如果您希望它是一个数字,那么只需将其类型转换为浮点数:

$var = (float)number_format($var, 2, '.', '');

注意:正如评论中指出的那样,这实际上四舍五入数字.

sprintf - 不正确.(sprintf 也轮回)
如果不舍入数字很重要,那么根据下面的答案,使用 sprintf:

$var = sprintf("%01.2f", $var);

地板 - 不完全!(地板四舍五入负数)

floor,通过一些数学运算,将接近做你想做的事:

楼层(2.56789 * 100)/100;//2.56

其中 100 代表您想要的精度.如果你想要三位数,那么:

楼层(2.56789 * 1000)/1000;//2.567

然而,这有负数的问题.负数仍然被四舍五入,而不是被截断:

floor(-2.56789 * 100)/100;//-2.57

旧"正确答案:利用楼层的功能

所以一个完全健壮的解决方案需要一个函数:

function truncate_number( $number, $precision = 2) {//零导致问题,无需截断if ( 0 == (int)$number ) {返回 $number;}//我们是负数吗?$negative = $number/abs($number);//将数字转换为正数以解决舍入问题$number = abs($number);//计算除/乘的精度数$precision = pow(10, $precision);//运行数学运算,重新应用负值以确保正确返回负/正return floor( $number * $precision )/$precision * $negative;}

上述函数的结果:

echo truncate_number(2.56789, 1);//2.5echo truncate_number(2.56789);//2.56echo truncate_number(2.56789, 3);//2.567echo truncate_number(-2.56789, 1);//-2.5echo truncate_number(-2.56789);//-2.56echo truncate_number(-2.56789, 3);//-2.567

新的正确答案

使用PHP原生函数bcdiv

echo bcdiv(2.56789, 1, 1);//2.5回声 bcdiv(2.56789, 1, 2);//2.56回声 bcdiv(2.56789, 1, 3);//2.567回声 bcdiv(-2.56789, 1, 1);//-2.5回声 bcdiv(-2.56789, 1, 2);//-2.56回声 bcdiv(-2.56789, 1, 3);//-2.567

i have value in php variable like that

$var='2.500000550';
echo $var

what i want is to delete all decimal points after 2 digits.

like now value of variable will be

$var='2.50';
echo $var

keep in mind this value is coming from mysql databse

but when i use round php function i got round but i dont need round, i just need to delete all digits after 2 decimal simple.

i have tired, flot() and lot of other option no success.

Thanks

解决方案

TL;DR:

The PHP native function bcdiv seems to do precisely what is required, and properly.

To simply "truncate" a number, bcdiv($var, 1, 2); where 2 is the number of decimals to preserve (and 1 is the denomenator - dividing the number by 1 allows you to simply truncate the original number to the desired decimal places)

Full Answer (for history)

This turns out to be more elusive than one might think.

After this answer was (incorrectly) upvoted quite a bit, it has come to my attention that even sprintf will round.

Rather than delete this answer, I'm turning it into a more robust explanation / discussion of each proposed solution.

number_format - Incorrect. (rounds)
Try using number format:

$var = number_format($var, 2, '.', '');  // Last two parameters are optional
echo $var;
// Outputs 2.50

If you want it to be a number, then simply type-cast to a float:

$var = (float)number_format($var, 2, '.', '');

Note: as has been pointed out in the comments, this does in fact round the number.

sprintf - incorrect. (sprintf also rounds)
If not rounding the number is important, then per the answer below, use sprintf:

$var = sprintf("%01.2f", $var);

floor - not quite! (floor rounds negative numbers)

floor, with some math, will come close to doing what you want:

floor(2.56789 * 100) / 100; // 2.56

Where 100 represents the precision you want. If you wanted it to three digits, then:

floor(2.56789 * 1000) / 1000; // 2.567

However, this has a problem with negative numbers. Negative numbers still get rounded, rather than truncated:

floor(-2.56789 * 100) / 100; // -2.57

"Old" Correct answer: function utilizing floor

So a fully robust solution requires a function:

function truncate_number( $number, $precision = 2) {
    // Zero causes issues, and no need to truncate
    if ( 0 == (int)$number ) {
        return $number;
    }
    // Are we negative?
    $negative = $number / abs($number);
    // Cast the number to a positive to solve rounding
    $number = abs($number);
    // Calculate precision number for dividing / multiplying
    $precision = pow(10, $precision);
    // Run the math, re-applying the negative value to ensure returns correctly negative / positive
    return floor( $number * $precision ) / $precision * $negative;
}

Results from the above function:

echo truncate_number(2.56789, 1); // 2.5
echo truncate_number(2.56789);    // 2.56
echo truncate_number(2.56789, 3); // 2.567

echo truncate_number(-2.56789, 1); // -2.5
echo truncate_number(-2.56789);    // -2.56
echo truncate_number(-2.56789, 3); // -2.567

New Correct Answer

Use the PHP native function bcdiv

echo bcdiv(2.56789, 1, 1);  // 2.5
echo bcdiv(2.56789, 1, 2);  // 2.56
echo bcdiv(2.56789, 1, 3);  // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 2); // -2.56
echo bcdiv(-2.56789, 1, 3); // -2.567

这篇关于删除两个小数点后的数字,而不对值进行四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆