php round 2e decimal to 0 or 5

后端 未结 2 1356
走了就别回头了
走了就别回头了 2020-12-22 13:15

i want to round $t 2e decimal to 0 or 5.

 if $t= 1.31; //round to 1.30
 if $t= 1.32  //round to 1.30
 if $t= 1.33; //round to 1.35
 if $t= 1.34; //round to 1         


        
相关标签:
2条回答
  • 2020-12-22 13:50

    You can double the value so you can round to 1 decimal, then simple divide it by 2 again

    $t = round(($t*2), 1) / 2;
    
    0 讨论(0)
  • 2020-12-22 13:58

    try this one check the demo

    0.05 * round($t * 20)

    <?php
      $ts= [1.31, 1.32, 1.33, 1.34, 1.35, 1.36, 1.37, 1.38, 1.39, 1.40];
      array_walk($ts, function($t){echo number_format($t, 2)."\t".number_format(0.05*round($t*20), 2)."\n";});
    

    output,

      1.31  1.30
    1.32    1.30
    1.33    1.35
    1.34    1.35
    1.35    1.35
    1.36    1.35
    1.37    1.35
    1.38    1.40
    1.39    1.40
    1.40    1.40
    
    0 讨论(0)
提交回复
热议问题