PHP Format Number with 2 Decimals Example

PHP Format Number with 2 Decimals Example

  • Post category:PHP
  • Post comments:0 Comments

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 Learn PHP Get First and Last Element of Array Example

PHP Format Number with 2 Decimals 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

Leave a Reply