简体   繁体   English

如果 php 中的值是十进制值,如何获取舍入值?

[英]How to get round value if value is in decimal in php?

$str = 6.87;    
$x = ceil($str/5) * 5;
echo $x;

I have a variable str and I want to covert str round value.我有一个变量str ,我想转换str舍入值。 In above I have 6.87 and I want 6.9 and if I have 6.43 then it should be 6.4 similarly if $str = 6.45 then it become 6.5 .在上面我有6.87我想要6.9如果我有6.43那么它应该是6.4同样如果$str = 6.45然后它变成6.5 So, How can I do this?那么,我该怎么做呢? Please help me.请帮我。

Thank You谢谢你

Use:利用:

$str = 6.87;    
echo round($str,1);

where the first parameter is the variable, then the second is the decimal point to round.其中第一个参数是变量,然后第二个是要四舍五入的小数点。

this will output这将 output

6.9

You should use round() with 2nd parameter 1 like following code:您应该使用带有第二个参数 1 的 round(),如下面的代码:

echo $str = round(6.45,1);  

Output: Output:

6.5

it should be more like this.. the 1 being number of position(s) to round up to.它应该更像这样.. 1 是要四舍五入的位置数。

$str = 6.87;
echo(round($str,1));

which will give you 6.9这会给你6.9

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

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