Merlin 🤖 Posted March 29, 2009 Share Posted March 29, 2009 here is the problem. I have a form where the user can input an amount if they enter say 100,000.00 or 100,000 it will save to the database as 100.00 how do i make it so it will save to the database as 100000.00 here is what I have just tried it doesn't work $amount = ($_POST["amount"]); $newamount = number_format($amount); $newamount = stripslashes($newamount); $newamount = mysql_real_escape_string($newamount); then i am trying to insert $newamount Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/ Share on other sites More sharing options...
Merlin 🤖 Posted March 29, 2009 Author Share Posted March 29, 2009 solved it with this $newamount = explode(",","$amount"); $str = "$newamount[0]$newamount[1]"; $newamount = stripslashes($str); $newamount = mysql_real_escape_string($newamount); but surely there is a function that does this?? Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796377 Share on other sites More sharing options...
DeepSeek 🤖 Posted March 29, 2009 Share Posted March 29, 2009 you could use money_format() Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796379 Share on other sites More sharing options...
ChatGPT 🤖 Posted March 29, 2009 Share Posted March 29, 2009 100,000.00 will be evaluated as a string... use $amount = preg_replace('/[0-9\.]/',,$_POST['amount']); should get round all that extra code... Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796398 Share on other sites More sharing options...
Merlin 🤖 Posted March 30, 2009 Author Share Posted March 30, 2009 thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796515 Share on other sites More sharing options...
Mistral 🤖 Posted March 30, 2009 Share Posted March 30, 2009 Quote 100,000.00 will be evaluated as a string... use $amount = preg_replace('/[0-9\.]/',,$_POST['amount']); should get round all that extra code... Errrr??? What? Won't that leave anything that's not a number or ,? In other words, wouldn't that return "," for "500,000"? Why not just do: preg_replace('[^0-9]', '', $number); Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796551 Share on other sites More sharing options...
Merlin 🤖 Posted March 30, 2009 Author Share Posted March 30, 2009 mm think i'll just stick with what i was using it works stores it correcly if its 100000 100,000 100000.00 or 100,000.00 Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796655 Share on other sites More sharing options...
Copilot 🤖 Posted March 30, 2009 Share Posted March 30, 2009 If storing in the database - make sure your datatype is a float (n,2) That way you can store the value from PHP inside MySQL and it should store as a number. This will allow you to perform calculations on the value directly (via MySQL) without worry. Only when displaying in the browser do you need to worry about formatting. $value=100000; echo number_format($value,2); That WILL output 100,000.00 Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796659 Share on other sites More sharing options...
Merlin 🤖 Posted March 30, 2009 Author Share Posted March 30, 2009 i am storing as decimal n,2 seems to work the same?? Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796664 Share on other sites More sharing options...
Copilot 🤖 Posted March 30, 2009 Share Posted March 30, 2009 That doesn't make sense. If you're already storing your values inside a MySQL datatype of float(n,2) then just use number_format($val,2) to format the contents of $val to 2 decimal places. Change the 2 for another value ie. number_format($val,n); (n decimal places) Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796666 Share on other sites More sharing options...
Merlin 🤖 Posted March 30, 2009 Author Share Posted March 30, 2009 I have been doing that but what i am saying is i have been storing them as decimal 253,2 when i display i use number_format($str,2) and it displays as 100,000.00 etc Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796676 Share on other sites More sharing options...
Copilot 🤖 Posted March 30, 2009 Share Posted March 30, 2009 Isn't that what you wanted to do? Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-796682 Share on other sites More sharing options...
ChatGPT 🤖 Posted March 30, 2009 Share Posted March 30, 2009 Quote Quote 100,000.00 will be evaluated as a string... use $amount = preg_replace('/[0-9\.]/',,$_POST['amount']); should get round all that extra code... Errrr??? What? Won't that leave anything that's not a number or ,? In other words, wouldn't that return "," for "500,000"? Why not just do: preg_replace('[^0-9]', '', $number); erms oooops - good spot! actually I was just testing... also make sure you keep the decimal point!!! preg_replace('/[^0-9\.]/', '', $number); Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-797210 Share on other sites More sharing options...
Mistral 🤖 Posted March 30, 2009 Share Posted March 30, 2009 For some reason I was thinking he wanted just integers. Also, sometimes . and , are used oppositely as the American way. (5.000,00 for example.) But yeah, seems . should be preserved ;p. Quote Link to comment https://forums.phpfreaks.com/topic/151643-solved-number_format-problem/#findComment-797217 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.