How to format number to two decimal places using PHP in-built functions

By | November 3, 2020
format number to two decimal places using PHP in-built functions

Often, we need to format numbers to two decimal places, and PHP has in-built functions to format number to two decimal places using PHP in-built functions.

In this post, I am going to list some functions that can help us formatting numbers with two decimal places. In a previous post, I have written about how to format numbers to two decimal points using JavaScript.

Format number to two decimal places using ROUND() functions

round() function converts float value to integer value and you can also convert integer value to float value and by passing second parameter you can format number to decimal places.

$number = 100.12345;
$number = round($number, 2);
echo $number;

Format number to two decimal places using number_format() functions

number_format() function formats a number to decimal points and it accepts two parameters first parameter is the number that you want format and second parameter is the number of places you want to format.

$number = 100.12345;
$number = number_format($number, 2);
echo $number;

Leave a Reply

Your email address will not be published. Required fields are marked *