PHP Format Number with 2 Decimals Example

By Hardik Savani February 5, 2024 Category : PHP

Hi,

This tutorial will give you an example of php converting float to 2 decimal places. you will learn php number with 2 decimals. We will use a php format number with 2 decimals. you will learn php format to 2 decimal places. So, let us see in detail an example.

You can use the number_format function to format a float to a specified number of decimal places. Here's an example:

index.php

<?php

$originalFloat = 123.456789;

/* Format the float to 2 decimal places */

$formattedFloat = number_format($originalFloat, 2);

/* Output the result */

echo "Original Float: $originalFloat";

echo "Formatted Float: $formattedFloat";

?>

In this example, the number_format function is used with the $originalFloat and the number of decimal places you want (2 in this case). The result is stored in $formattedFloat, and you can then use it as needed. The output will be:

Output:

Original Float: 123.456789

Formatted Float: 123.46

I hope it can help you...

Tags :
Shares