Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a rough time decided on FLOAT, DOUBLE, or DECIMAL

Tags:

mysql

Currently using MySQL version 5.1.6

This is my first real world build and so far I have actually been enjoying it; however, I now am stuck on a decision regarding a field datatype and hoping someone could sort it out for me.

I essentially have 10 fields that are all different test results. The numbers range from -100 to 100 and can have a decimal with one spot after the actual point.

For example, -5.1, 0, 1, 16.3, 99.2, and 100 are all possible data. From what I have read, one should use DECIMAL for those things that we usually measure and are exact (which these are), whereas FLOAT and DOUBLE are approximations, which I do not really want (though I am sure at this level, the approximation is very small if existent at all).

If I use DECIMAL, do I have to include a space for the '-' at the beginning if used? I.E, would I use DECIMAL(4,1) or DECIMAL(5,1) or am I way off here? I might be overthinking this a bit.

like image 776
shelzmike Avatar asked Mar 20 '12 18:03

shelzmike


People also ask

What is the difference between float double and decimal?

Float uses 32 bits to represent data. Double uses 64 bits to represent data. Decimal uses 128 bits to represent data.

What is the difference between long float and double?

float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.

Should I use float or double in C#?

Use float or double ? The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

What is the difference between double and decimal in mysql?

Double Type Some data may take more digits to the right of the decimal point. While the storage size of the decimal type is variable, the double type takes 8 bytes storage size. Also double precision ranges up to fifteen decimal digits.


1 Answers

DECIMAL(4,1) will be enough, the sign digit does not need to be included.

More info: http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html

For example, a DECIMAL(3,0) column supports a range of -999 to 999

Decimal will be indeed the best option for your needs. Float and Double can give you ugly numbers (e.g. 0.2 cannot be represented as float, you'd get 0.19999999)

like image 170
Schiavini Avatar answered Oct 18 '22 07:10

Schiavini