note 85346 modified in function.floatval by danbrown
| From: | danbrown@php.net | Date: | Mon, 15 Dec 2008 16:36:31 +0000 |
| Subject: | note 85346 modified in function.floatval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148180@lists.php.net to get a copy of this message | ||
<?php
function floatvalue($value) {
return floatval(preg_replace('#^([-]*[0-9\.,\'
]+?)((\.|,){1}([0-9-]{1,2}))*$#e', "str_replace(array('.', ',',
\"'\", ' '), '', '\\1') . '.\\4'",
$value));
}
?>
It is much shorter and able to handle those one, too:
xx,-
xx,--
xx'xxx,xx
After using floatvalue() you can go forward with number_format() as usual.
--was--
Instead of Michiels function try this one:
<?php
function floatvalue($value) {
$value = preg_replace('#^([-]*[0-9\.,\' ]+?)((\.|,){1}([0-9-]{1,2}))*$#e',
"str_replace(array('.', ',', \"'\", ' '),
'', '\\1') . '.' . sprintf('%02d','\\4')",
$value);
return floatval($value);
}
?>
It is much shorter and able to handle those one, too:
xx,-
xx,--
xx'xxx,xx
After using floatvalue() you can go forward with number_format() as usual.
http://php.net/manual/en/function.floatval.php