TIL – MySQL Data Type DECIMAL

This data type comes up when we design a column to store money amount. We’d like to have accuracy and avoid the funny famous issue in programming 0.1 + 0.2 !== 0.3.

The MySQL data type DECIMAL (with NUMERIC as its alias) is used for storing decimal numbers with fixed precision and scale. It is suitable for dealing with precise numeric values where accuracy is crucial, such as monetary amounts.

To handle the range from -999.99 to 999.99, you can define a DECIMAL column as DECIMAL(5, 2). This means the column can store up to 5 digits in total, with 2 digits after the decimal point.

Use the DECIMAL data type when you need accurate representation without any loss of precision. It helps to avoid rounding errors and ensures consistency in your data, especially for calculations involving monetary transactions or other precise measurements.

References:

Leave a comment