deane034 Posted January 2, 2009 Share Posted January 2, 2009 Hi, Does anyone know how to : 1) Recognize a formatted number string such as 5,700.99 as a float number. That is a function which returns true of it's a formatted number and false if it's a general string e.g. "some string" 2) Convert that formatted number into a numeric float. What I'm looking for essentially is a reverse for the PHP number_format function. Which does the opposite. Any ideas? thanks. Link to comment https://forums.phpfreaks.com/topic/139177-identifying-a-formatted-number-string-eg-5700-as-a-number/ Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 str_replace to remove the , You could use regex to to check if string look like formatted number. Link to comment https://forums.phpfreaks.com/topic/139177-identifying-a-formatted-number-string-eg-5700-as-a-number/#findComment-727945 Share on other sites More sharing options...
thebadbad Posted January 2, 2009 Share Posted January 2, 2009 Wrote a function using number_format(): <?php function is_number_format($number, $dec_point = '.', $thousands_sep = ',') { $numeric = str_replace($thousands_sep, '', $number); $parts = explode($dec_point, $numeric); $dec = strlen(end($parts)); if ($dec_point != '.') { $numeric = str_replace($dec_point, '.', $numeric); } if ($number == number_format((float) $numeric, $dec, $dec_point, $thousands_sep)) { return true; } else { return false; } } //test $n = '5,700.99'; if (is_number_format($n)) { echo "$n is a formatted number!"; } ?> Seems to work fine. Just pass the second and third parameter if the dec. point and thousands sep. are different from a dot and a comma. Link to comment https://forums.phpfreaks.com/topic/139177-identifying-a-formatted-number-string-eg-5700-as-a-number/#findComment-727949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.