Crecket
Crecket

Reputation: 718

PHP round adds decimals if a precision is defined

I've looked everywhere but I can't seem to figure this out. I have a bunch off coordinates which have up to 6 decimal places and I want to round everything down to only 2 decimals.

So for example a number can be '82.599899' and it should become '82.60'. The problem is that if I use round($number, 2) it turns into this:

82.599999999999994316

So a bunch of examples are:

round(82.599899);  //returns 83
round(82.599899, 2); //returns 82.599999999999994316
floor(82.599899* 100) / 100; //returns 82.599999999999994316
round(28.4772, 2); //returns 28.480000000000000426

Am I simply doing something wrong? I've never had issues with round, floor etz before so I'm pretty clueless right now

Upvotes: 0

Views: 55

Answers (1)

KhorneHoly
KhorneHoly

Reputation: 4766

For me this is working as expected.

round(82.599899);  //returns 83
round(82.599899, 2); //returns 82.60
round(28.4772, 2); //returns 28.48

So I guess that this is a config problem. Did you changed something in your php.ini? Maybe the precision field is changed from the default one.

Upvotes: 2

Related Questions