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 Quote Link to comment 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); Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
StefanRSA Posted September 1, 2009 Author Share Posted September 1, 2009 Thank You. This will do. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.