Convert/Adjust Number in a specific counting with PHP

浪子不回头ぞ 提交于 2019-12-11 05:49:44

问题


I have the number 1543, I want to make sure that I find the number closest to 1543 which is counted every 100. For eg:

1527 = 1500

1563 = 1600

12034 = 12000

12081 = 12100

I hope that you understand.


回答1:


Use the round() function with a precision value of -2,

round( $number, -2);

Codepad Viper Demo.




回答2:


you can do that by dividing the number by 100 and using the round function.




回答3:


<?php

    function foo($number, $d)
    {
        return round($number / $d) * $d;
    }

    echo foo(1527, 100) . "\n";
    echo foo(12081, 100) . "\n";

?>


来源:https://stackoverflow.com/questions/14935390/convert-adjust-number-in-a-specific-counting-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!