what data type should i use in CURRENCY?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

what data type should i use in CURRENCY?

Post by zplits »

Good day everyone. Does anyone here have an idea what is the best data type to use when dealing with money?

Need your advice. Thank you.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: what data type should i use in CURRENCY?

Post by Christopher »

Usually there is a DECIMAL type that works well for money. Otherwise one if the FLOAT types. Be aware that floating point arithmetic can be problematic in almost all computer languages.
(#10850)
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: what data type should i use in CURRENCY?

Post by zplits »

can i use INT and just display it in a page formatted #,###.##?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: what data type should i use in CURRENCY?

Post by onion2k »

zplits wrote:can i use INT and just display it in a page formatted #,###.##?
You can, but all the prices in your database will have to be stored in whatever the smallest denomination of currency you have. That's pennies if you're in the UK like me. That's not really a problem but it's not the most efficient way of storing the data. Using a DECIMAL field means there'll be less wasted space on the server.

That is assuming you're using MySQL. Other databases, MSSQL Server for example, have a 'money' data type. You should use that if it's an option.
Post Reply