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)));
}
If you're using 5.3 or higher (thanks ircmaxell), use numfmt_parse.