How to set minimum decimal digits but not maximum decimal digits. I tried with number_format
but the results were unexpected.
<?php
echo number_format("1000000", 2) . "<br>";
echo number_format("1000000.6", 2) . "<br>";
echo number_format("1000000.63", 2) . "<br>";
echo number_format("1000000.68464", 2) . "<br>";
?>
Result:
1,000,000.00
1,000,000.60
1,000,000.63
1,000,000.68
Expected Result:
1,000,000.00
1,000,000.60
1,000,000.63
1,000,000.68464
How do I do this?
2
Answers
There’s a more advanced class called
NumberFormatter
which is part of the intl extension that supports both minimum and maximum, and you can just set the latter to a high number to achieve your expected results.Output:
Demo: https://3v4l.org/filso#v8.0.25
Simple function for that. You can custom function for your idea: string to use for decimal point,…
Results: