CodeIgniter Forums
Enter Dollar Amounts Into Database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Enter Dollar Amounts Into Database (/showthread.php?tid=69087)



Enter Dollar Amounts Into Database - wolfgang1983 - 10-06-2017

Hi,  When I insert lets say 50.00 into my database on the database it displays it like 50.0000

What is the best way to configure the column so if I put $50.00 it will stay that way or even if enter a value $1,000.00 

Thank you.


[Image: 3cpRqYm60Oz8.png]

Second Image

[Image: 3cpSNLJXjs4t.png]


RE: Enter Dollar Amounts Into Database - neuron - 10-06-2017

As u can see in your table structure, 
the column types of cash_total_value and gift_card_value are type of DECIMAL(15,4) this means this columns can have total of 15 digits and 4 out of 15 will be fraction part.

In order to get 2 digits after "." you need to change type of columns to DECIMAL(15,2).

1,000.00 like numbers need to be converted to 1000.00 before you insert to database


RE: Enter Dollar Amounts Into Database - wolfgang1983 - 10-06-2017

(10-06-2017, 01:31 AM)neuron Wrote: As u can see in your table structure, 
the column types of cash_total_value and gift_card_value are type of DECIMAL(15,4) this means this columns can have total of 15 digits and 4 out of 15 will be fraction part.

In order to get 2 digits after "." you need to change type of columns to DECIMAL(15,2).

1,000.00 like numbers need to be converted to 1000.00 before you insert to database

Thank you will try it out.


RE: Enter Dollar Amounts Into Database - Shawn - 10-08-2017

(10-06-2017, 01:40 AM)wolfgang1983 Wrote:
(10-06-2017, 01:31 AM)neuron Wrote: As u can see in your table structure, 
the column types of cash_total_value and gift_card_value are type of DECIMAL(15,4) this means this columns can have total of 15 digits and 4 out of 15 will be fraction part.

In order to get 2 digits after "." you need to change type of columns to DECIMAL(15,2).

1,000.00 like numbers need to be converted to 1000.00 before you insert to database

Thank you will try it out.

You may want to keep the 4 decimal points precision. Options are discussed in this stackoverflow question and others.
https://stackoverflow.com/questions/224462/storing-money-in-a-decimal-column-what-precision-and-scale

You can always display or retrieve the values at two decimal points. For example
Code:
SELECT ROUND(gross_amount, 2) AS gross_amount
FROM donations;