StefanRSA Posted September 1, 2009 Share Posted September 1, 2009 I am not to sure how to go around this problem.... I have a field where a user must put in a number. This is a actually a field used for currency but no calculation is done. So i am using textfield. Now I am trying to figure out how I can make it display in a standard format using: $value= trim($_POST['rand']); $rand = number_format ($value, 0); All works well if a user enters a value like 100000. But when a user enters 100,000 only 100 gets posted and am sure its because of the "number_format".... What will be the best to work with this problem? Thanks Link to comment https://forums.phpfreaks.com/topic/172669-solved-number_format-problem/ Share on other sites More sharing options...
btherl Posted September 1, 2009 Share Posted September 1, 2009 You can do it this way: $value = trim($_POST['rand']); $value = preg_replace("|[^0-9]|", "", $value); Link to comment https://forums.phpfreaks.com/topic/172669-solved-number_format-problem/#findComment-910142 Share on other sites More sharing options...
Mark Baker Posted September 1, 2009 Share Posted September 1, 2009 All works well if a user enters a value like 100000. But when a user enters 100,000 only 100 gets posted and am sure its because of the "number_format"....It's not number format that's the problem. It's the fact that the posted value is a string. number_format expects a number, and standard PHP behaviour is to cast the string to a numeric value.... by taking any only leading digits. So "13 monkeys" would give 13, because the space character isn't a digit, and "100,000" will give 100 because the comma (,) isn't a digit. Follow btherl's suggestion which strips out all non-digits from the string, before using number_format. You may need to modify the regular expression to allow for a decimal point though. Link to comment https://forums.phpfreaks.com/topic/172669-solved-number_format-problem/#findComment-910150 Share on other sites More sharing options...
StefanRSA Posted September 1, 2009 Author Share Posted September 1, 2009 Thank You. This will do. Link to comment https://forums.phpfreaks.com/topic/172669-solved-number_format-problem/#findComment-910161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.