最简单的方法可以舍入到最接近的50或00

时间:2016-10-03 09:00:34

标签: php

我想整理一个像这样的数字

1439 to 1400
1399 to 1350

在php中最接近的方法是什么?

6 个答案:

答案 0 :(得分:2)

你可以这样做:

  1. 除以100。
  2. 截断。
  3. 乘以100。
  4. 这是我能想到的最好的事情。

    $num = 1401;
    $num /= 100;
    $num = round($num);
    $num *= 100;
    

答案 1 :(得分:2)

鉴于新的例子......

您似乎想要使用PHP floor并自己应用50

    sentences                                                      words
    This document is far better                                    better
    This is a great app                                            great
    The night skies were sombre and starless                       sombre 
    The app is too good and i am happy using it                    good
    The app is too good and i am happy using it                    happy
    This is how it works                                            -
    Since the app crashes frequently, I advice you guys to fix     
    the issue ASAP                                                 crahses

OLD ANSWER

从你的例子开始,而不是问题标题..

尝试PHP round功能。

在你的情况下:

50 * floor($number / 50)

第二个参数是要舍入的小数位数,负值位于'的左侧。而不是数字。

对于一些更细微的变化,还有第三个参数。

答案 2 :(得分:2)

你可以做那样的事情(只是向下舍入):

$n1 = 1439;
$n2 = 1399;

$round1 =  $n1 -  $n1 % 50; // round1 = 1439 - 39 = 1400
$round2 =  $n2 - $n2 % 50; // round2 = 1399 - 49 = 1350

要进行整理,您可以这样做:

$n1 = 1439;
$n2 = 1399;

$round1 =  $n1 + (50 -  $n1 % 50); // round1 = 1439 + (50 - 39) = 1450
$round2 =  $n2 + (50 - $n2 % 50); // round2 = 1399 + (50 - 49) = 1400

答案 3 :(得分:1)

你应该尝试:

    function specialRoundUp($val) {
      return 100 * round($val / 100);
    }

答案 4 :(得分:0)

有时我们都会思考'东西..

$ rounded_n1 = round($ n1 / 50,0)* 50;

答案 5 :(得分:0)

使用ceil总是四舍五入

$round1 = ceil($n1/ 50) * 50
相关问题