How to Create a Variable that Stores Money in MySQL

MySQL


In this article, we show how to reate a variable in MySQL that can store money currency.

To create a variable that stores values of money into a table in MySQL, the Decimal variable type should be used.

The Decimal variable type allows you to specify the maximum amount of numbers that you would like shown in a number before the decimal point and the amount of numbers that you want to appear after the decimal point.

The general form of declaring a Decimal type variable is:

number Decimal (amount_of_numbers_before_decimal_point, amount_of_numbers_after_decimal_point)

In a money value, the second number is always 2, because money is only carried out to the cent, the hundredth place.

So the statement:

number Decimal (10,2)

allows for a maximum allowable 10 digits to show before the decimal point and shows 2 places after the decimal, as all money values should.

So declaring:

name_of_variable Decimal (10,2)

is a perfect way to declare a variable type that can store any type of money values.

HTML Comment Box is loading comments...