note 43262 modified in function.floatval by danbrown

From: Date: Mon, 15 Dec 2008 16:37:16 +0000
Subject: note 43262 modified in function.floatval by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148181@lists.php.net to get a copy of this message
Easier-to-grasp-function for the ',' problem. <?php function Getfloat($str) { if(strstr($str, ",")) { $str = str_replace(".", "", $str); // replace dots (thousand seps) with blancs $str = str_replace(",", ".", $str); // replace ',' with '.' } if(preg_match("#([0-9\.]+)#", $str, $match)) { // search for number that may contain '.' return floatval($match[0]); } else { return floatval($str); // take some last chances with floatval } } echo Getfloat("$ 19.332,35-"); // will print: 19332.35 ?> --was-- Easier-to-grasp-function for the ',' problem. function Getfloat($str) { if(strstr($str, ",")) { $str = str_replace(".", "", $str); // replace dots (thousand seps) with blancs $str = str_replace(",", ".", $str); // replace ',' with '.' } if(preg_match("#([0-9\.]+)#", $str, $match)) { // search for number that may contain '.' return floatval($match[0]); } else { return floatval($str); // take some last chances with floatval } } echo Getfloat("$ 19.332,35-"); // will print: 19332.35 http://php.net/manual/en/function.floatval.php

« previous php.notes (#148181) next »