user3332631
user3332631

Reputation: 394

PHP round precision bug?

Is this a bug or some math thing I don't understand?

round(58.900662, 2) => 58.9

Where is the last zero?

EDIT: In my opinion, the documentation is lacking information.

The following code does as expected

number_format(round(58.900662, 2), 2) => 58.90

Upvotes: 0

Views: 307

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31739

round will take care of the precision if that digit is not 0 as the last 0 has no effect in any mathematical operation.

If the number is 1.569 then round will return 1.57

If you want that 0 the you can use -

number_format(58.900662, 2, '.', ',');

number_format()

Upvotes: 2

kojow7
kojow7

Reputation: 11384

You told it to round it to the 2nd decimal place which would be 58.90. However, 58.90 can be simplified to 58.9. It is the same number.

If you want to force the display of two decimal places you can use PHP's number_format() function.

Upvotes: 1

Related Questions