How to Format Number with 2 Decimal in PHP

Websolutionstuff | Feb-26-2024 | Categories : Laravel PHP

Hey there! If you've ever needed to work with numbers in PHP, you probably know how important it is to format them properly. Getting the formatting right can make a big difference if you're dealing with money, measurements, or any other numerical data.

In this guide, I will show you step-by-step how to format numbers with two decimals in Laravel PHP.

So, let's see how to format numbers with 2 decimals in PHP, PHP format number with 2 decimals, number_format in PHP decimal, number format in laravel 8/9/10, and laravel php number format.

Step 1: Define Your Number

First, you need to have a number that you want to format. It could be a variable containing a numerical value or a number retrieved from a database or user input.

$number = 1234.56789;

 

Step 2: Use number_format() Function

PHP provides a handy function called number_format() specifically designed for formatting numbers. This function allows you to specify the number of decimal places you want to display.

$formatted_number = number_format($number, 2);

In this example, 2 specifies that we want to display two decimal places. If you need to round the number to two decimal places, you can use the round() function before applying number_format().

$formatted_number = number_format(round($number, 2), 2);

Here are multiple ways to format a number in PHP with code snippets:

Using number_format() function:

$number = 1234.56789;
$formatted_number = number_format($number, 2);
echo "Formatted Number: $formatted_number";

 

Using sprintf() function:

$number = 1234.56789;
$formatted_number = sprintf("%.2f", $number);
echo "Formatted Number: $formatted_number";

 

Using round() function with number_format():

$number = 1234.56789;
$formatted_number = number_format(round($number, 2), 2);
echo "Formatted Number: $formatted_number";

 

Using number_format() with custom thousands and decimal separators:

$number = 1234.56789;
$formatted_number = number_format($number, 2, '.', ',');
echo "Formatted Number: $formatted_number";

 

Using number_format() with localized formatting:

$number = 1234.56789;
setlocale(LC_MONETARY, 'en_US');
$formatted_number = money_format("%.2n", $number);
echo "Formatted Number: $formatted_number";

 

Step 3: Display the Formatted Number

Now that you have your formatted number, you can display it wherever needed, whether it's on a web page, a report, or any other output.

echo "Formatted Number: $formatted_number";

That's it! By following these steps, you can confidently format numbers with two decimals in PHP

 


You might also like:

Recommended Post
Featured Post
How To Image Upload With Preview In Angular 15
How To Image Upload With Previ...

In today's world of web development, being able to upload images with a preview feature is a common requirement for...

Read More

Jun-16-2023

How To Add Default Value Of Column In Laravel Migration
How To Add Default Value Of Co...

In this article, we will explore how to add default values to columns in Laravel 10 migrations, although the information...

Read More

May-01-2023

Laravel 9 Livewire Dependent Dropdown
Laravel 9 Livewire Dependent D...

In this article, we will see the laravel 9 livewire dependent dropdown. Here, we will learn how to create a dependent dr...

Read More

Nov-29-2022

Laravel 9 User Roles and Permissions Without Package
Laravel 9 User Roles and Permi...

In this article, we will see laravel 9 user roles and permissions without package. Roles and permissions are an imp...

Read More

Apr-14-2022