I'm trying to round a large number I have and it seems that round is not functioning properly. The output of this should be 1.53
but I'm getting 1.5300000000000000266453525910037569701671600341796875
.
$roundMe = 1.5294000046599232067734419615590013563632965087890625;
$rounded = [
'rounded' => round($roundMe)
];
What would cause this to not output what I expect?
round() returns a float
rather than string
so you have to deal with floating point arithmetic and its quirks:
float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
Depending on your exact needs, you may want to give number_format() a try:
string number_format ( float $number [, int $decimals = 0 ] )
… or just live with it (it shouldn't be an issue in most use cases).
P.S. Here's a test case that actually reproduces the issue:
ini_set('precision', 17);
var_dump(round(1.53, 2));
ini_set('precision', 50);
var_dump(round(1.53, 2));