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. Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/ Share on other sites More sharing options...
papaface Posted July 14, 2008 Share Posted July 14, 2008 number_format() Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589902 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 Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589904 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 Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589905 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. Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589908 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 Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589914 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? Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589915 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 Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589917 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 Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589923 Share on other sites More sharing options...
GingerRobot Posted July 14, 2008 Share Posted July 14, 2008 Try: $data = str_replace(',','',$_POST['data']); Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589925 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. Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589928 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? Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589930 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 Link to comment https://forums.phpfreaks.com/topic/114720-solved-number-formatting/#findComment-589931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.