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 OverflowPHP number format - Stack Overflow
PHP number format with integer format - Stack Overflow
php - Show a number to two decimal places - Stack Overflow
Is it possible to number_format without changing decimal places? I just want to add commas.
Videos
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)));
}
use ; Sanitize filters
$number1= '$ 1,989.34';
$number2 = filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);//1989.34
$number2 = filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);//1,98934
From the PHP Manual page on number_format:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
If you want numbers like 123456 be formatted as 1234,45, use:
echo number_format($number / 100, 2, ",", "");
If you need a dot as thousands separator (1.234,56):
echo number_format($number / 100, 2, ",", ".");
The zeros are automatically removed by PHP when converting the string to a number.
string number_format ( float $number ,
int $decimals = 0 ,
string $dec_point = '.' ,
string $thousands_sep = ',' )
Manual: http://php.net/manual/en/function.number-format.php
// divide by 100 to shift ones place.
echo number_format((int)'000000000000100' / 100,2,',','');
Just do a loose comparison of $price cast as integer against $price, if they match (ie. it's a whole number), you can format to 0 decimal places:
number_format($price, ((int) $price == $price ? 0 : 2), '.', ',');
You can use ternary operator fot that:
$price = 20.456;
print "$" . ($price == intval($price) ? number_format($price, 0, "", ",") : number_format($price, 2, "", ","));
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
This function returns a string.
Use round() (use if you are expecting a number in float format only, else use number_format() as an answer given by Codemwnci):
echo round(520.34345, 2); // 520.34
echo round(520.3, 2); // 520.3
echo round(520, 2); // 520
From the manual:
Description:
float round(float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]]);
Returns the rounded value of
valto specifiedprecision(number of digits after the decimal point).precisioncan also be negative or zero (default).
...
Example #1
round()examples
<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>
###Example #2 mode examples
<?php
echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10
echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9
echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9
echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9
?>
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!
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.
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);