Jump to content

What type should I use?


shadowayex
 Share

Recommended Posts

I'm working on a project for a friend that requires we store some fractional numbers. It looks like that, for now, we'll only be storing numbers with one decimal place. There is a small possibility we will begin storing numbers with two places, but unless that makes a huge impact on what type I should use to store, I don't think it's that important.I've seen three types I could use for this: decimal, double, and float. I'm not too experienced in MySQL types, and I was wondering the advantages and disadvantages of each, and which would be good for my case.

Link to comment
Share on other sites

According to the MySQL manual on datatype storage requirements and the numeric data type descriptions, a FLOAT can describe any approximate number that can fit in 4 bytes (assuming the whole part is 1 digit, that's at most 24 decimal digits). DOUBLE is, as you may guess, double that (8 bytes; assuming the whole part is 1 digit, up to 53 decimal digits).DECIMAL is a little different (with NUMERIC being a synonym for it). In it, you must explicitly specify the maximum number of digits in total and the maximum precision of the decimal parts, and the value will take the least amount of however bytes are necesary to deliver that precision. Naturally, because there's compression, decompression and casting happening all over, calculations with such values are slightly slower (insignificantly, mind you).So, if precision of calculations is vital (e.g. if you're dealing with money), use DECIMAL. If small storage and speed in calculation is more important than a minor loss of precision during calculations (the storage itself is till going to contain the value specified), use FLOAT or DOUBLE, depending on the maximum values desired.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...