I’d like to display a number, integer or float in a uniform way like this:
if it is integer, for example 10, it should be displayed as 10.00
if it is a float already, for example 10.1, it should be displayed as 10.10
and if the float look like this 10.116, just display the first two number after the dot
regardless the left digits following the third one.
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
$formatted = sprintf("%.2f", $your_number);
Alternatively, if you just want to print it out, and don’t need it in a variable:
echo number_format(10, 2); // will output 10.00
You can use php’s in built number_format() function. You can get more information from php.net.
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂