PHP Format Number with 2 Decimals Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Install PHP DOM Extension in Ubuntu 22.04?
- How to Capitalize First Letter of String in Laravel?
- How to Remove Numeric Values in PHP Array?
- Python Count Number of Special Characters in String Example
- How to Remove Numbers from String in PHP?
- How to Convert Number to Words in Laravel?
- How to Find Number from String in PHP?
- Calculate Number of Days Between Two Dates in JQuery
- How to Only Allow Numbers in a Text Box using jQuery?
- How to Get File Name without Extension in PHP?
- How to Count Number of Files in Directory using PHP?
- How to Generate Random Alphanumeric String in PHP?