开发者

Working with decimal numbers in PHP

开发者 https://www.devze.com 2022-12-30 07:22 出处:网络
I want to somehow round the numbers for a rating system in PHP like this: 4.6667 = 4.6 5.0001 = 5.0 Is there any way to do that? (开发者_StackOverflow中文版BTW, I read the data from a database.)Yo

I want to somehow round the numbers for a rating system in PHP like this:

4.6667 = 4.6

5.0001 = 5.0

Is there any way to do that? (开发者_StackOverflow中文版BTW, I read the data from a database.)


You are not conforming to any single rule. For example:

4.6667 = 4.6
5.0001 = 5.1

See these functions anyway:

round
ceil
floor

And number_format.


Use this:

echo round(1.45667, 2);

The number "2" is how many decimal places you want.

This is output 1.46.

0

精彩评论

暂无评论...
验证码 换一张
取 消