You could remove any character that is not a digit or a decimal point and parse that with floatval:

$number = 1200.00;
$parsed = floatval(preg_replace('/[^\d.]/', '', number_format($number)));
var_dump($number === $parsed);  // bool(true)

And if the number has not . as decimal point:

function parse_number($number, $dec_point=null) {
    if (empty($dec_point)) {
        $locale = localeconv();
        $dec_point = $locale['decimal_point'];
    }
    return floatval(str_replace($dec_point, '.', preg_replace('/[^\d'.preg_quote($dec_point).']/', '', $number)));
}
Answer from Gumbo on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.number-format.php
PHP: number_format - Manual
It's not explicitly documented; number_format also rounds: <?php $numbers = array(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009); foreach ($numbers as $number) print $number."->".number_format($number, 2, '.', ',')."<br>"; ?> 0.001->0.00 0.002->0.00 0.003->0.00 0.004->0.00 0.005->0.01 0.006->0.01 0.007->0.01 0.008->0.01 0.009->0.01 ... Outputs a human readable number. <?php # Output easy-to-read numbers # by james at bandit.co.nz function bd_nice_number($n) { // first strip any formatting; $n = (0+str_replace(",","",$n)); // is this a number?
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_string_number_format.asp
PHP number_format() Function
HTML CSS JAVASCRIPT SQL PYTHON ... TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ยท PHP HOME PHP Intro PHP Install PHP Syntax PHP Comments ... PHP Strings String Functions Modify Strings Concatenate Strings Slicing Strings Escape Characters PHP Numbers PHP Casting PHP Math PHP ...
Discussions

PHP number format - Stack Overflow
If you want numbers like 123456 be formatted as 1234,45, use: ... The zeros are automatically removed by PHP when converting the string to a number. More on stackoverflow.com
๐ŸŒ stackoverflow.com
PHP number format with integer format - Stack Overflow
I am using PHP number_format to express prices of products, using decimal point, thousands separators, etc. For example: $price = 20.456; print "$" . number_format($price, 2, ".", ","); outputs $2... More on stackoverflow.com
๐ŸŒ stackoverflow.com
php - Show a number to two decimal places - Stack Overflow
What's the correct way to round a PHP string to two decimal places? $number = "520"; // It's a string from a database $formatted_number = round_to_2dp($number); echo $formatted_number; The output More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 11, 2025
Is it possible to number_format without changing decimal places? I just want to add commas.
Down and dirty: function addCommas( $number ) { $decimal = ''; if ( strpos( $number, '.' ) !== false ) { list ( $number, $decimal ) = explode( '.', $number ); $decimal = '.' . $decimal; } $number= number_format( $number, 0 ); return $number. $decimal; } More on reddit.com
๐ŸŒ r/PHPhelp
17
6
March 16, 2023
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ php โ€บ string functions โ€บ number_format()
PHP | String Functions | number_format() | Codecademy
September 17, 2023 - This example uses the number_format() function to format a whole number using "," as a decimal separator and "x" as a thousands separator. <?php ยท echo number_format("1000000",2,",","x"); ?> Copy to clipboard ยท Copy to clipboard ยท This will ...
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ php-in-a โ€บ 0596100671 โ€บ re68.html
number_format() - PHP in a Nutshell [Book]
October 13, 2005 - That will output Total charge is $12,346, because it rounds up to the nearest decimal place. number_format($n,$p) rounds $n to $p decimal places, adding commas between thousands.
Author ย  Paul Hudson
Published ย  2005
Pages ย  372
๐ŸŒ
w3resource
w3resource.com โ€บ php โ€บ function-reference โ€บ number_format.php
PHP number_format() function - w3resource
The number_format() function is used to format a number (Floating number) with grouped thousands. ... Note: The function accepts one, two, or four parameters (not three). ... A formatted version of the number.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-number_format-function
PHP number_format() Function - GeeksforGeeks
July 11, 2025 - Examples: Input: $number = 100000 Output: 10, 000 Input: $number = 10000 $decimals = 3 $decimalpoints = "." $sep =, Output: 10, 0000.000 Below programs illustrate The number_format() function in PHP: Program 1:
Find elsewhere
๐ŸŒ
Matt Doyle
elated.com โ€บ home โ€บ blog โ€บ formatting numbers with phpโ€™s number_format() function
Formatting Numbers with PHP's number_format() Function
July 23, 2022 - You can separate thousands with ... all). In its most basic form, number_format() accepts a number to format, and returns a string containing the formatted number, rounded to the nearest whole number, with commas between each group of thousands:...
๐ŸŒ
Linux Hint
linuxhint.com โ€บ use_number_format_function_php
Use of number_format() Function in PHP
March 2, 2024 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
W3Docs
w3docs.com โ€บ php
How to Convert a String to a Number in PHP
These methods are generally applied for converting a string into matching integer and float values. ... number_format() is a built-in PHP function that is used to format numbers with grouped thousands and/or decimal points.
๐ŸŒ
Code.mu
code.mu โ€บ en โ€บ php โ€บ manual โ€บ string โ€บ number_format
The number_format Function - Number Formatting in PHP
number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): string ... Let's separate thousands with a space, round the fractional part to two decimal places, and use a slash as the decimal separator: <?php echo number_format(1234.567, ...
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ is it possible to number_format without changing decimal places? i just want to add commas.
r/PHPhelp on Reddit: Is it possible to number_format without changing decimal places? I just want to add commas.
March 16, 2023 -

For example, if I pass in 123456.123000, I want the output to be 123,456.123000 and if I pass in 123456.2, I want the output to be 123,456.2. At the time of passing in, I will not know how many decimal places the number might have, or whether any trailing zeros are included.

If not I can write a function to do it, but I'm wondering if there's a built in way. It doesn't have to be number_format if there's a different function that does it. Thanks!

Top answer
1 of 6
1

In addition to the previous answers that only use number_format() after calculations & rounding - if you want dynamic locale-dependent price and currency formatting and you have the intl-extension enabled, then you can use PHP's number formatting to achieve correct formatting per locale (https://www.php.net/manual/en/class.numberformatter.php). This implementation supports both a procedural (numfmt_*) & an object-oriented (NumberFormatter) approach.

For this example, I use the OO approach to demonstrate what you wanted to achieve (after calculations, rounding, etc.).

$price = 75.33;
$locale = 'en_GB';
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
echo $fmt->formatCurrency($price, $fmt->getSymbol($fmt::INTL_CURRENCY_SYMBOL)); // ยฃ75.33

More interesting stuff can be achieved, such as rounding decimals after calculation of exchange rate multiplications:

$price = 75.50; // Note the change in price
$locale = 'en_US'; // Note the change in locale
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$fmt->setAttribute($fmt::FRACTION_DIGITS, 0);
echo $fmt->formatCurrency($price, $fmt->getSymbol($fmt::INTL_CURRENCY_SYMBOL)); // $76

or change its rounding mode:

$fmt->setAttribute($fmt::ROUNDING_MODE, $fmt::ROUND_DOWN);
echo $fmt->formatCurrency($price, $fmt->getSymbol($fmt::INTL_CURRENCY_SYMBOL)); // $75

It's particularly handy for countries using different placement of the currency symbol, or have different number formats (think Netherlands, France or Germany for example).

In sum, it allows for creating locale-based price strings without having to think about a country's currency, or its formatting.

2 of 6
0

As specified in the documentation, number_format returns a string value, you can't reuse it as a number. Use the function round() to round your number, if you want to round it to the direct upper integer use ceil() instead.

number_format(round(12345.6789), 2);
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ php tutorial โ€บ php number_format()
PHP number_format() | Examples of PHP number_format()
June 23, 2011 - Php function number_format() either accepts one, two or four parameters. It could not accept three parameters. By default, the number_format() function uses the (.) operator for decimal points and (,) for a thousand groups. Function number_format() in returns the string value which means we cannot use its output for the mathematical calculations. So one needs to keep in mind that it needs to be used only when the output is displayed.
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
OnlinePHP
onlinephp.io โ€บ number-format โ€บ manual
number_format - OnlinePHP.io Example
Sets the number of decimal digits. If 0, the decimal_separator is omitted from the return value. ... Sets the separator for the decimal point. ... Sets the thousands separator. A formatted version of num.