A data engineer is adding a commission_pct column to a Unity Catalog listings table in Azure Databricks. The column stores the commission percentage agreed for each property sale and is used in fee calculations.
- The values range from 0.7500 to 3.2500.
- Each value has exactly four digits after the decimal point, and all four must be kept.
- Values must be stored exactly, without the rounding errors of binary floating-point representation.
Which data type should the engineer use for the commission_pct column?
For exact base-10 values used in financial calculations, DECIMAL is the appropriate type rather than a floating-point type. In DECIMAL(p,s), p is the total number of digits and s is the number after the decimal point, so four decimal places need s = 4 and at least one more digit is needed for the integer part of values up to 3.2500. DECIMAL(5,4) satisfies the range, the scale and the exactness requirements together, while each other option fails one of them.
- Correct:
DECIMAL(5,4). DECIMAL is exact, and with 5 total digits and 4 after the decimal point it keeps all four decimal digits and allows one digit before the point, so it holds values up to 9.9999. - Not correct:
DOUBLE. DOUBLE is a floating-point type that stores numbers in a binary format, so decimal fractions like these may be held only approximately, which breaks the exact-storage requirement. - Not correct:
DECIMAL(5,2). A scale of 2 keeps only two digits after the decimal point, so the third and fourth decimal digits would be lost. - Not correct:
DECIMAL(4,4). All 4 digits are after the decimal point, leaving no digit before it, so values such as 3.2500 cannot be stored.