How do I convert output of number_format back to numbers in PHP?

php number format currency
php convert string to float with 2 decimal
php number format without comma
php number_format negative values
php remove decimal without rounding
php number_format rounding
php round to 2 decimal places
html number format decimal places

PHP can't recognize 1,200.00(generated by number_format) but only 1200.00,

What's the general solution for this problem?

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)));
}

How do I convert output of number_format back to numbers in PHP , here is the code to convert number to Indonesian text, this code has limitation as is return number_format($n); } ?> Outputs: 247,704,360 -> 247.7 million number_format() was changed to not being able to return -0, previously -0 could be returned for cases like where number would be -0.01. 5.4.0 This function now supports multiple bytes in dec_point and thousands_sep .

If you're using 5.3 or higher (thanks ircmaxell), use numfmt_parse.

number_format - Manual, money_format() returns a formatted version of number . a required conversion character Suppress the currency symbol from the output string. Here's what I used that worked to get what most people expect to see for a number format. number: Required. The number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as the thousands separator. decimals: Optional. Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point: decimalpoint: Optional.

You can use filter_var. e.g.

$num = '1,200,998';
$real_integer = filter_var($num, FILTER_SANITIZE_NUMBER_INT);
echo $real_integer;

will output:

1200998

money_format - Manual, You want to return a price: One parameter will round the number (it will be formatted without decimals). Two parameters should give the result you want: <? php Method 1: Using number_format () Function. The number_format () function is used to convert string into a number. It returns the formatted number on success otherwise it gives E_WARNING on failure.

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

PHP number_format() Function, Return value: A formatted version of the number. Value Type: String. Pictorial Presentation. php-string-number_format(). Example: Numbers that are stored as text can cause unexpected results. Select the cells, and then click to choose a convert option. Or, do the following if that button isn't available.

The better way is to use the options in number_format function itself. The format is

number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

I.e. if you don't need the ',' in the formatted string make, it is like

$num=23.33333
echo number_format($num,2,'.','');

If you need that as float then typecast the same to float

$floatnum= (float) number_format($num,2,'.','');

PHP number_format() function, The number_format() function can be used to format The function always returns a formatted string (not a number variable) and Output: string(12) " 4,200,000.00"� PHP can't recognize 1,200.00(generated by number_format) but only 1200.00, What's the general solution for this problem?

How to convert a string to a number in PHP, But this would only produce a number without decimals. Here are some examples: <?php // Outputs 123,457 echo number_format (� PHP convert string to number : We sometimes need to convert the string to integer. There are many ways to convert string to number. Here in this tutorial we are going to explain how you can convert a string to number forcefully using different methods. We will also explain how to convert string to float etc.

PHP: How To Format Numbers, Return Value: It returns Formatted Number in case success, otherwise it gives E_WARNING in failure. Examples: Input: $number = 100000 Output� Below is a tiny function that will take any 10 digit number and convert it into the following phone format: 970-313-2428. function phone_number_format($number

PHP, Strings in PHP can be converted to numbers (float / int / double) very easily. The number_format() function is used to convert string into a number. It returns the formatted number on success otherwise it gives E_WARNING on failure. Output: 1,000 1,000.31. Method 2: Using type casting: Typecasting can� Click Kutools > Content > Convert between Text and Number, see screenshot: 3 . In the Convert between Text and Number dialog box, check Number to text option, and then click OK or Apply button, the numbers displayed as scientific notation have been converted to normal numbers in the original range.

Comments
  • -1 to your comment @YourCommonSense , although I totally agree that formatted numbers is not something you want to store anywhere, sometimes you have to put up with rubbish code written by others and have no way to get around it and therefore the above has to be done. Your comment brings no value to this topic.
  • Also, you might need to display or input a formatted number in a form field, and when the form is submitted that value will have to be converted back to a number.
  • PHP Fatal error: Call to undefined function numfmt_parse()
  • it's good if just we use the value with comma (,) numbers. but if the value have a decimal then filter_var also remove the decimal too from the value which is not good.
  • What if the program is run in Germany which you may get 1.200,00?
  • $locale = localeconv();, Then change the ',' in above to $locale['thousands_sep']...
  • There are other similar answers posted already. This does not add much value to a decade old question.