I'll take the opposite stance.
FLOAT
is for approximate numbers, such as percentages, averages, etc. You should do formatting as you display the values, either in app code or using the FORMAT()
function of MySQL.
Don't ever test float_value = 1.3
; there are many reasons why that will fail.
DECIMAL
should be used for monetary values. DECIMAL
avoids a second rounding when a value needs to be rounded to dollars/cents/euros/etc. Accountants don't like fractions of cents.
MySQL's implementation of DECIMAL
allows 65 significant digits; FLOAT
gives about 7 and DOUBLE
about 16. 7 is usually more than enough for sensors and scientific computations.
As for "percentage" -- Sometimes I have used TINYINT UNSIGNED
when I want to consume only 1 byte of storage and don't need much precision; sometimes I have used FLOAT
(4 bytes). There is no datatype tuned specifically for percentage. (Note also, that DECIMAL(2,0)
cannot hold the value 100
, so technically you would need DECIMAL(3,0)
.)
Or sometimes I have used a FLOAT
that held a value between 0 and 1. But then I would need to make sure to multiply by 100 before displaying the "percentage".
More
All three of "percentage, average, rate" smell like floats, so that would be my first choice.
One criterion for deciding on datatype... How many copies of the value will exist?
If you have a billion-row table with a column for a percentage, consider that TINYINT
would take 1 byte (1GB total), but FLOAT
would take 4 bytes (4GB total). OTOH, most applications do not have that many rows, so this may not be relevant.
As a 'general' rule, "exact" values should use some form of INT
or DECIMAL
. Inexact things (scientific calculations, square roots, division, etc) should use FLOAT
(or DOUBLE
).
Furthermore, the formatting of the output should usually be left to the application front end. That is, even though an "average" may compute to "14.6666666...", the display should show something like "14.7"; this is friendlier to humans. Meanwhile, you have the underlying value to later decide that "15" or "14.667" is preferable output formatting.
The range "0.00 - 100.00" could be done either with FLOAT
and use output formatting or with DECIMAL(5,2)
(3 bytes) with the pre-determination that you will always want the indicated precision.