speedy33417 Posted January 21, 2008 Share Posted January 21, 2008 I have a form update page where I grab some number values from a db and display it in this format 1,251.05 using this code: input name="amount" value="<?php echo number_format ($amount, 2); ?>" class="form4"> How do reverse it though? If the form is submitted and the value (1,251.05) is saved in the db (float) it will actually save 1 and it cuts off the rest of the numbers after the comma. Is there a function to make 1,251.05 into 1251.05? I tried number_format ($amount, 2, '.', '') but it doesn't work. Probably because $amount at this point already has the comma in it. Link to comment https://forums.phpfreaks.com/topic/87056-number_format-question/ Share on other sites More sharing options...
cooldude832 Posted January 21, 2008 Share Posted January 21, 2008 do this strip all non numerical digits (regex or str_replace) convert to a float divide by 100 Link to comment https://forums.phpfreaks.com/topic/87056-number_format-question/#findComment-445209 Share on other sites More sharing options...
Bauer418 Posted January 21, 2008 Share Posted January 21, 2008 $amount = str_replace(',', '', $amount); Link to comment https://forums.phpfreaks.com/topic/87056-number_format-question/#findComment-445227 Share on other sites More sharing options...
alexloh Posted March 23, 2008 Share Posted March 23, 2008 Please consider to wrap the str_replace() into a function for code reuse purposes. function stripcomma($amount) { return str_replace(',', '', $amount); } Details see code example at: http://www.thewebscripter.com/tutorial/code_examples/number_format.php Hope this helps. Alex. Link to comment https://forums.phpfreaks.com/topic/87056-number_format-question/#findComment-498859 Share on other sites More sharing options...
wildteen88 Posted March 23, 2008 Share Posted March 23, 2008 Just use number_format again: number_format ($amount, 2, '.', '') The forth parameter is the thousands seperator. Link to comment https://forums.phpfreaks.com/topic/87056-number_format-question/#findComment-498950 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.