四舍五入至最接近的0.05
问题描述:
我想通过使用php4,5.2及以下版本(而非5.3)来解决舍入机制 目前,我正在进行0.05舍入,类似于此页面:
I would like to solve rounding mechanism by using php4,5.2 and below (not 5.3) Currently I am doing 0.05 rounding, something like this page:
http://www.bnm. gov.my/index.php?ch=209&pg=657&ac=568
before rounding | after rounding
89.90 | 89.90
89.91 | 89.90
89.92 | 89.90
89.93 | 89.95
89.94 | 89.95
89.95 | 89.95
89.96 | 89.95
89.97 | 89.95
89.98 | 90.00
89.99 | 90.00
我尝试使用字符串将其拆分并手动进行添加,但这并不是一个很好的解决方案,希望在这里可以找到可以解决该问题的人.
I try to use string to split it out and manually do adding, but not really a good solution, hoping here can find someone to solve it.
答
使用此功能
function rndfunc($x){
return round($x * 2, 1) / 2;
}