Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Numerical data types
Integer data types
Floating point data types
Decimal data types
11. decimal numbers
Summary

Instruction

Good! That was a rather peculiar result, wasn't it? Let's see how we can fix it.

What numbers are the most important for every business? Money, of course. Because of rounding errors, floating point numbers are not suitable for money values. After all, we don't want the bank to think that we ALMOST or NEARLY paid our monthly payment!

Money values can be stored in another data type: decimal. What is unique about this data type is that it stores numbers in the decimal system.

decimal values are precise when they are added or divided by integer numbers. This is why they are an excellent choice for financial operations (like calculating tax).

In SQL, decimal(p, s) takes two numbers: p is the precision (that is, the total number of digits in the number) and s is the scale (the number of digits after the decimal point). decimal(5, 2) is a number which has 5 digits, two of which are after the decimal point.

Exercise

The online dating business is going really well. Peter needs another table to store information about money transfers from his users. Create a simple table called payment that has two columns: user_id (which stores integer values) and amount (a decimal value with 6 digits, of which two are after the decimal point).