Alexandru R
Alexandru R

Reputation: 8833

Round 2 decimals

I have an issue with php 5.6 rounding to two decimals. If you test the following it will export $y exactly as 19.620000000000001

I know there are solutions to display it using number_format, but how do I make it to be exactly 19.62?

Thanks.

$x = 19.620000000000001;
$y = round($x, 2);
var_export($y);

Later Edit. I need it for exact math operations, not display.

Upvotes: 0

Views: 99

Answers (3)

MrPerry95
MrPerry95

Reputation: 29

It's because you're using var_export

Try using echo or var_dump()

$x = 19.620000000000001;
$y = round($x, 2);
echo $y;

Upvotes: 2

Mat
Mat

Reputation: 2438

If you do number_format:

$y = number_format($x, 2);

The $y will have 2 decimals but it will also be a string. Not sure if it's satisfactory.

Upvotes: 0

Miggy
Miggy

Reputation: 816

Try using echo to display your output. It will give you your desired output

Upvotes: 3

Related Questions