将 php 十进制值舍入到最后 0 之后的第二个数字 [英] round php decimal value to second digit after last 0
本文介绍了将 php 十进制值舍入到最后 0 之后的第二个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以十进制值结尾的即时"计算结果(使用money_format转换后)如下:
I have results of an "on the fly" calculation that end in decimal values (after converted using money_format) as follows:
$cost_per_trans = 0.0000000476
$cost_per_trans = 0.0000007047
money_format 前对应的值为:
The corresponding values before the money_format are:
4.7564687975647E-8
7.0466204408366E-7
这些值的长度可能不同,但我希望能够将它们四舍五入到0"字符串后的最后 2 位数字,例如:
These values may be of different lengths but I would like to be able to round them to the last 2 digits after the string of "0s", to get this for example:
$cost_per_trans = 0.000000048
$cost_per_trans = 0.00000070
我不确定
如何在正确的位置进行回合?
how to do the round in the right spot?
是在money_format之前还是之后舍入?
whether to round before or after the money_format?
推荐答案
function format_to_last_2_digits($number) {
$depth = 0;
$test = $number;
while ($test < 10) { // >10 means we have enough depth
$test = $test * 10;
$depth += 1;
}
return number_format($number, $depth);
}
$cost_per_trans = 0.0000000476;
var_dump(format_to_last_2_digits($cost_per_trans)); // 0.000000048
$high_number = 300;
var_dump(format_to_last_2_digits($high_number)); // 300
这篇关于将 php 十进制值舍入到最后 0 之后的第二个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文