ItSolutionStuff.com

PHP Format Number with 2 Decimals Example

By Hardik Savani β€’ May 14, 2024
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: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

How to Install PHP DOM Extension in Ubuntu 22.04?

Read Now β†’
β˜…

How to Capitalize First Letter of String in Laravel?

Read Now β†’
β˜…

How to Remove Numeric Values in PHP Array?

Read Now β†’
β˜…

Python Count Number of Special Characters in String Example

Read Now β†’
β˜…

How to Remove Numbers from String in PHP?

Read Now β†’
β˜…

How to Convert Number to Words in Laravel?

Read Now β†’
β˜…

How to Find Number from String in PHP?

Read Now β†’
β˜…

Calculate Number of Days Between Two Dates in JQuery

Read Now β†’
β˜…

How to Only Allow Numbers in a Text Box using jQuery?

Read Now β†’
β˜…

How to Get File Name without Extension in PHP?

Read Now β†’
β˜…

How to Count Number of Files in Directory using PHP?

Read Now β†’
β˜…

How to Generate Random Alphanumeric String in PHP?

Read Now β†’