在小数点后两位删除数字,不舍入该值 [英] Delete digits after two decimal points, without rounding the value

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

问题描述

我对php变量有这样的价值

  $ var ='2.500000550'; 
echo $ var

我想要的是在2位数后删除所有小数点。 / p>

像现在的变量值将会是

  $ var ='2.50 ; 
echo $ var




请注意,此值即将到来来自mysql数据库


但是当我使用圆形php函数我不需要轮,我只需要删除两位小数后的所有数字简单。



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



谢谢 解决方案

TL; DR:



PHP本地函数 bcdiv 似乎正是要求的,正确的。



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



完整答案(用于历史记录)



这个结果比一个更难以捉摸可能会这样想。



在这个答案被错误地提高了一点之后,我注意到即使是sprintf也会轮到。

而不是删除这个答案,我把它变成对每个提议解决方案的更强大的解释/讨论。

number_format - 不正确。 (rounds)

尝试使用
数字格式

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

如果您希望它是一个数字,转换为浮点数:

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

注意:正如评论中指出的那样,事实上,的数字。



sprintf - 不正确。 (sprintf也轮)

如果不是四舍五入的数字很重要,那么根据下面的答案,使用
$ b

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

地板 - 不太完美! (floor rounds negative number)



floor ,有一些数学,会接近做你想做的:

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

其中100表示​​您想要的精度。如果你想要三位数字,那么:

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

然而,这对负数有一个问题。负数仍然是四舍五入的,而不是截断:

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



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

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

$ p $函数truncate_number($ number,$ precision = 2 ){
// Zero导致问题,并且如果(0 ==(int)$ number){
return $ number;}不需要截断
;
}
//我们是否定的?
$ negative = $ number / abs($ number);
//将数字转换为正数以解决四舍五入
$ number = abs($ number);
//计算除数/相乘的精度数
$ precision = pow(10,$ precision);
//运行数学运算,重新应用负值以确保正确返回负值/正值
返回楼层($ number * $ precision)/ $ precision * $ negative;
}

上述函数的结果:

  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



使用PHP本地函数 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


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天全站免登陆