Working with decimal numbers in PHP

I want to somehow round the numbers for a rating system in PHP as follows:

4.6667 = 4.6 5.0001 = 5.0 

Is there any way to do this? (BTW, I read the data from the database.)

+4
source share
2 answers

You do not agree with any rule. For instance:

 4.6667 = 4.6 5.0001 = 5.1 

See these functions anyway:

round
ceil
floor

And number_format .

+6
source

Use this:

 echo round(1.45667, 2); 

The number "2" is the number of decimal places you want.

This is conclusion 1.46.

+5
source

Source: https://habr.com/ru/post/1309971/


All Articles