Rounding Mechanism to nearest 0.05

后端 未结 10 1936
离开以前
离开以前 2021-01-05 00:15

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/in

相关标签:
10条回答
  • 2021-01-05 00:35

    use this function

    function rndfunc($x){
      return round($x * 2, 1) / 2;
    }
    
    0 讨论(0)
  • 2021-01-05 00:35

    I'm sure there are more elegant solutions, but this appears to suit the task:

    <?php
    
    // setup test
    $start_num = 89.90;
    $iterations = 10;
    
    // loop through test numbers
    for ($i = 0; $i < $iterations; $i++) {
      nickleRound($start_num + (0.01 * $i));
      echo "\n\n";
    }
    
    //
    function nickleRound($num) {
      $p = 0.05;
      echo "\n" . 'p= ' . $p;
    
      $num = round($num, 2);
      echo "\n" . 'num= ' . $num;
    
      $r = ($num / $p);
      echo "\n" . 'r= ' . $r;
    
      $r2 = ceil($r) - $r;  
      echo "\n" . 'r2= ' . $r2;
    
      $a = round($num, 1);
      if (($r2 > 0) && ($r2 < 0.5)) {
        $a = $a + 0.05; 
      }
      echo "\n" . 'a= ' . $a;
    }
    
    0 讨论(0)
  • 2021-01-05 00:39

    Hint:-

    $input1 = 24.05;

    $things = abs($input * 20 ); // 481 ".05"s

    $tenpcnt = abs($things / 10); // 48 ".05"s

    $ouput = $tenpcnt / 20;

    echo $ouput; // 2.40

    0 讨论(0)
  • 2021-01-05 00:43
    //Round to nearest 0.05
    echo round ($number * 20, 0) / 20;
    
    //Round Up to nearest 0.05
    echo ceil ($number * 20) / 20;
    
    //Round Down to nearest 0.05
    echo floor ($number * 20) / 20;
    
    0 讨论(0)
提交回复
热议问题