Antipattern: Use FLOAT Data Type
Let’s learn how using the FLOAT data type for non-integer data affects the output.
We'll cover the following
Most programming languages support a data type for real numbers, called float
or double
. SQL supports a similar data type of the same name. Many programmers naturally use the SQL FLOAT
data type everywhere they need fractional numeric data because they are accustomed to programming with the float
data type.
The FLOAT
data type in SQL, like float
in most programming languages, encodes a real number in a binary format according to the IEEE 754 standard. We need to understand some characteristics of floating-point numbers in this format to use them effectively.
Rounding by necessity
Many programmers are not aware of a characteristic of this floating-point format: not all values that can be described in decimals can be stored in binary. Out of necessity, some numbers must be rounded to a very close value.
Get hands-on with 1400+ tech skills courses.