nitation Posted July 14, 2008 Share Posted July 14, 2008 Hello, I just want to know how to format an amount value in my database to have commas. example; the value i entered is 45000, how do i format it to be like this; 45,000 and when i deduct, it will deduct correctly. Cos when am trying it, it will only deduct the FIGURES before the comma sign. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
papaface Posted July 14, 2008 Share Posted July 14, 2008 number_format() Quote Link to comment Share on other sites More sharing options...
nitation Posted July 14, 2008 Author Share Posted July 14, 2008 I have tried it. How do i then insert it to my database and easily subtract without counting ignoring after the comma Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 You should only use number_format() when you display a number, not when using it in calculations or storing it in a DB. A number like 10,423 is not a number in PHP. Ken Quote Link to comment Share on other sites More sharing options...
nitation Posted July 14, 2008 Author Share Posted July 14, 2008 @ ken what do i do in this regard if am trying to subtract. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 If none of your numbers contain a comma, subtraction works fine. Also, if you try to store a number with a comma into a field of type "int" it won't work. If you already have your number stored with commas, I would suggest you modify those stored numbers to get rid of the commas. Ken Quote Link to comment Share on other sites More sharing options...
nitation Posted July 14, 2008 Author Share Posted July 14, 2008 @ken That means if i get you right, the number must be in the database without commas right? My question now is, if i try to make subtraction that contain comma in my form input would that work? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 Yes, never store numbers in the database that have commas in them. As for data which is entered in a form, just use the str_replace() function to remove the commas: <?php $data = str_replace(',',$_POST['data']); ?> Ken Quote Link to comment Share on other sites More sharing options...
nitation Posted July 14, 2008 Author Share Posted July 14, 2008 @ken I posted the code you gave me on line 29 of my code. this is what i get Warning: Wrong parameter count for str_replace() in C:\Program Files\xampp\htdocs\myfiles\vest\canada\internets\transfer.php on line 29 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 14, 2008 Share Posted July 14, 2008 Try: $data = str_replace(',','',$_POST['data']); Quote Link to comment Share on other sites More sharing options...
nitation Posted July 14, 2008 Author Share Posted July 14, 2008 @ginger, I am processing my form on the same page, do i need to paste this code $data = str_replace(',','',$_POST['data']); before or after my MYSQL insert or what. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 14, 2008 Share Posted July 14, 2008 What do you think? Here's what you've been told: 1.) You shouldn't store numbers with commas in your database 2.) You can strip out the commas with str_replace(). Now, can you think when you might want to strip out the commas? Quote Link to comment Share on other sites More sharing options...
nitation Posted July 14, 2008 Author Share Posted July 14, 2008 Thank you Am working on that. Will click solved in a moment i guess 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.