Sometimes you want to avoid rounding up or down and always have two scale/decimal points for the given value. If you use number_format, money_format, round, so on. you will get different results. To solve this issue, you can use example below.


Function


function formatNumber($value)
{
return number_format(floor($value * 100) / 100, 2);
}

$array = [
0,
0.00,
0.4,
0.5,
1.0,
1.9,
1.99,
1.999,
1.444,
1234567,
"1.",
"1,6"
];

foreach ($array as $value) {
echo formatNumber($value).PHP_EOL;
}

Result


0.00
0.00
0.40
0.50
1.00
1.90
1.99
1.99
1.44
1,234,567.00
1.00
1.00