How to Convert Output of Number_Format Back to Numbers in PHP

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

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

number_format turns float into a string php

I will sum up all the comments for you. It is the intended way of using number_format
Example:

$someFloat = 10.99; // Float 10.99
$formattedString = number_format($someFloat, 2, ','); // String: 10,99
$floatFromString = (float) $formattedString // Float: 10.99 - note that this might throw an error/warning depending on language culture used by your server

The function declaration:
number_format ( float $number [, int $decimals = 0 ] ) : string
number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) : string
Input is a float, output is always a string.
Doc. reference

Converting number-formatted variable back to original format - PHP

$number = 500000;
$formattedNumber = number_format($number, 2);
$getBackOriginl = explode('.',$formattedNumber);
echo str_replace(',','',$getBackOriginl[0]);

number_format on 1 million return Nan0

Your number_format PHP code works fine, however you don't want this formatted. You want to provide the plain, raw number as the data-stop attribute.

data-stop="1164116"

PHP number_format returns 1.00

Your function returns 1,423.28? This is not a float, as a float does never contain a comma as a thousands-separator.

PHP interprets this as 1, because it "breaks" at the comma.

Remove the comma and you are fine!

$balance = str_replace(',', '', $balance);

How to convert number to float in PHP

You can use number_format function to format the numbers for desired output.

$foo = "105.01";
$zoo = "105";
echo number_format((float)$foo, 2, '.', ''); //Output 105.01
echo number_format((float)$zoo, 2, '.', ''); //Output 105.00

php converting formatted int to int

intval() won't work with the string you have because of the comma. You can remove the comma by using str_replace() and then calling intval() like so:

echo intval(str_replace(',', '', $myVal))

Show a number to two decimal places

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.

PHP number format with integer format

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), '.', ',');


Related Topics



Leave a reply



Submit