Saturday, March 21, 2015

Store currency / money data into database with INT datatype

Hi everyone! I just learn something new about storing currency / money data into database using INT format instead of using DECIMAL or NUMERIC datatype from Mr.Nawawi Jamili. Usually we using decimal(10,2) format to store data into database, but if you follow "Generally accepted accounting principles (GAAP)", you should use 4 decimal point which is decimal(10,4) format.

But, he suggest to use INT format because we will treat as a cent value only. The trick is to prevent you from been confuse when handling with large amount of data. Dont worry about lose precision problem, it wont be happen here. To store the data, you need to multiply it with 100 before saved. Then for output, divide the stored value with 100. If you are using MySQL, just use the query statement function given below :

Just let the programming side do the cent converting to $$ job and database side just storing the cent values only. Hey, team work right?

No comments:

Post a Comment