"Mastering PHP: Format Numbers with 2 Decimal Places"

Sandra Jun 29, 2026

Formatting Numbers to Two Decimals in PHP

two tables with numbers on them and one has the same number in each column, which is
two tables with numbers on them and one has the same number in each column, which is

In PHP, you can format numbers to display with a specific number of decimal places using various built-in functions. When you need to display a number with exactly two decimal places, you have a couple of methods to choose from. Let's explore these methods and understand how to use them effectively.

Binary to Decimal Table
Binary to Decimal Table

Using Number Format Function

The `number_format()` function in PHP is a versatile tool for formatting numbers as strings. It allows you to specify the number of decimal places, among other formatting options. Here's how you can use it to format a number to two decimal places:

Place Value - Charts, With Decimals | What is Place Value?
Place Value - Charts, With Decimals | What is Place Value?

```php $number = 1234.5678; echo number_format($number, 2); ```

This will output: 1234.57

Using sprintf Function

Numerix - Flutter Package
Numerix - Flutter Package

The `sprintf()` function is another powerful tool for formatting strings in PHP. It allows you to insert formatted values into a string using placeholders. To format a number to two decimal places using `sprintf()`, you can use the following syntax:

```php $number = 1234.5678; echo sprintf("%.2f", $number); ```

This will also output: 1234.57

Comparison of Methods

Number System Converter | Number System Calculator
Number System Converter | Number System Calculator

Both `number_format()` and `sprintf()` can be used to format numbers to two decimal places in PHP. However, they have some key differences:

Function Output Additional Formatting
`number_format()` String Yes (thousands separator, decimal separator, etc.)
`sprintf()` String No

If you need additional formatting options like a thousands separator, `number_format()` is the better choice. However, if you only need to format the number to two decimal places and prefer a more concise syntax, `sprintf()` might be more suitable.

a computer screen with several numbers on it
a computer screen with several numbers on it

Formatting Numbers with Currency Symbol

If you're working with currency values, you might want to include the currency symbol in the formatted number. Both `number_format()` and `sprintf()` support this. Here's how you can do it using each function:

Decimal Column Addition Worksheets
Decimal Column Addition Worksheets
a poster showing how to divide numbers
a poster showing how to divide numbers
Comprehensive Decimal Anchor Charts Bundle for Grades 3-7
Comprehensive Decimal Anchor Charts Bundle for Grades 3-7
Convert a hexadecimal number to a decimal number (and vice versa)
Convert a hexadecimal number to a decimal number (and vice versa)
Multiplying 3-Digit by 2-Digit Numbers with Various Decimal Places (A)
Multiplying 3-Digit by 2-Digit Numbers with Various Decimal Places (A)
Place Values After the Decimal Point
Place Values After the Decimal Point
Decimal Column (Vertical) Subtraction Worksheets
Decimal Column (Vertical) Subtraction Worksheets
Multiplying 2 digit decimals by whole numbers | K5 Learning
Multiplying 2 digit decimals by whole numbers | K5 Learning
Adding Decimal Hundredths with 2 Digits Before the Decimal (range 10.01 to 99.99) (A)
Adding Decimal Hundredths with 2 Digits Before the Decimal (range 10.01 to 99.99) (A)
1- 100 Chart - 20 Free PDF Printables | Printablee
1- 100 Chart - 20 Free PDF Printables | Printablee
the table with numbers and times for each type of item in this table is shown
the table with numbers and times for each type of item in this table is shown
a table with numbers and symbols for each type of item in the form of letters
a table with numbers and symbols for each type of item in the form of letters
How Do You Increase Decimal Points to Two in Word? | Techwalla
How Do You Increase Decimal Points to Two in Word? | Techwalla
Estimate Decimal Differences Worksheet – Fun Practice for 2nd Grade Math
Estimate Decimal Differences Worksheet – Fun Practice for 2nd Grade Math
Adding decimals (2 digits) | K5 Learning
Adding decimals (2 digits) | K5 Learning
Conversion Table Decimal – Hexadecimal – Binary - Resources For Electrical & Electronic Engineers
Conversion Table Decimal – Hexadecimal – Binary - Resources For Electrical & Electronic Engineers
Worksheet on Decimal Numbers | Decimals Number Concepts | Answers
Worksheet on Decimal Numbers | Decimals Number Concepts | Answers
Decimal - Hexadecimal Conversion
Decimal - Hexadecimal Conversion
a screenshot of a computer screen with the text python program to convert a decal number to its roman numeral equivalent
a screenshot of a computer screen with the text python program to convert a decal number to its roman numeral equivalent
85 Engaging Decimal Ordering Worksheets for 4th & 5th Grade Math Review
85 Engaging Decimal Ordering Worksheets for 4th & 5th Grade Math Review

```php $currency = "USD"; $amount = 1234.5678; // Using number_format() echo number_format($amount, 2) . " " . $currency; // Using sprintf() echo sprintf("%.2f", $amount) . " " . $currency; ```

Both examples will output: 1234.57 USD

Conclusion

Formatting numbers to two decimal places in PHP is straightforward using either the `number_format()` or `sprintf()` functions. The choice between these functions depends on your specific use case and formatting requirements. Understanding how to use these functions effectively will help you create clean, readable, and well-formatted code.