Inexact things (scientific calculations, square roots, division, etc) should use FLOAT (or DOUBLE).įurthermore, the formatting of the output should usually be left to the application front end. OTOH, most applications do not have that many rows, so this may not be relevant.Īs a 'general' rule, "exact" values should use some form of INT or DECIMAL. 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). But then I would need to make sure to multiply by 100 before displaying the "percentage".Īll three of "percentage, average, rate" smell like floats, so that would be my first choice. Or sometimes I have used a FLOAT that held a value between 0 and 1. (Note also, that DECIMAL(2,0) cannot hold the value 100, so technically you would need DECIMAL(3,0).) There is no datatype tuned specifically for percentage. 7 is usually more than enough for sensors and scientific computations.Īs 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). MySQL's implementation of DECIMAL allows 65 significant digits FLOAT gives about 7 and DOUBLE about 16. Accountants don't like fractions of cents. DECIMAL avoids a second rounding when a value needs to be rounded to dollars/cents/euros/etc. You should do formatting as you display the values, either in app code or using the FORMAT() function of MySQL.ĭon't ever test float_value = 1.3 there are many reasons why that will fail.ĭECIMAL should be used for monetary values. FLOAT is for approximate numbers, such as percentages, averages, etc.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |