简体   繁体   English

PHP将2e小数舍入为0或5

[英]php round 2e decimal to 0 or 5

i want to round $t 2e decimal to 0 or 5. 我想将$ t 2e小数舍入为0或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.35
 if $t= 1.36; //round to 1.35
 if $t= 1.37  //round to 1.35
 if $t= 1.38; //round to 1.40
 if $t= 1.39; //round to 1.40

try this one check the demo 试试这个检查演示

0.05 * round($t * 20) 0.05 *舍入($ 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

您可以将该值加倍,以便四舍五入到小数点后1位,然后再次简单地将其除以2

$t = round(($t*2), 1) / 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM