In mysql, currency is often represented by fields of Decimal and Numric types. These two types are implemented as the same type by MySQL; they are used to save values, and the accuracy of the value is extremely important. , such as money-related data.
# During development, currency is commonly represented by Decimal and Numric types in the database. These two types are implemented as the same type by MySQL. They are used to store values whose exact accuracy is extremely important, such as money-related data. When declaring a class to be one of these types, precision and scale can (and usually are) specified; for example:
salary DECIMAL(9,2)
In this example, 9 (precision) represents the total number of values that will be used to store the value. of decimal places, and 2(scale) represents the number of digits after the decimal point that will be used to store. Therefore, in this case, the range of values that can be stored in the salary column is from -9999999.99 to 9999999.99.
DECIMAL and NUMERIC values are stored as strings, rather than as binary floating point numbers, in order to preserve the decimal precision of those values. One character for each digit of the value, decimal point (if scale>0), and "-" sign (for negative values). If scale is 0, DECIMAL and NUMERIC values do not contain a decimal point or decimal part.
The reason why float or double is not used: Because float and double are stored in binary, there is a certain error.
For example: in the database, the storage types of c1, c2, and c3 are float (10.2), decimal (10.2), and float types respectively.
Insert data:
INTO test (c1,c2,c3) VALUES (1234567.23,1234567.23,1234567.23)
The result of data storage in the database is:
It can be seen that there is a certain error in using float type storage. Therefore use decimal or numric types.
Recommended tutorial: mysql video tutorial
The above is the detailed content of What field type does mysql currency use?. For more information, please follow other related articles on the PHP Chinese website!