In mysql, the price uses the "DECIMAL" type. Decimal is a data type specially designed for financial-related issues. It is actually stored in the form of a string. The number of digits in the integer part and decimal part can be demarcated when defining; when the accuracy requirements are relatively high (such as currency, Scientific data), it is better to use the DECIMAL type.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
In mysql, the price uses the "DECIMAL" type.
Decimal is a data type specially designed for financial related problems. In MySQL, fixed-point numbers are stored in the form of strings. When precision requirements are relatively high (such as currency, scientific data), it is better to use the DECIMAL type.
Decimal is a data type in databases such as SQL Server and MySql. It does not belong to the floating-point number type. The number of digits in the integer part and decimal part can be specified when defining. Using precise decimal types not only ensures more accurate data calculations, but also saves storage space. For example, decimal(4,2) can be used for percentages. The storage data range is: -10^38~10^38-1 numbers with fixed precision and decimal places. A decimal type data occupies 2~17 bytes.
Syntax:
decimal[ (p[ , s] )]
p (significant digits): The maximum total number of decimal digits that can be stored, including both sides of the decimal point. The number of significant digits must be a value between 1 and the maximum number of significant digits, 38. The default number of valid digits is 18.
s (decimal digits): The maximum number of decimal digits that can be stored to the right of the decimal point. The number of decimal places must be a value from 0 to p. The number of decimal places can be specified only if significant digits are specified. The default number of decimal places is 0; therefore, 0 <= s <= p. The maximum bank size will vary with the number of significant bits.
Decimal(n,m)
indicates that there are n digits in the value, including n-m digits for integers and m digits for decimals.
Example: decimal(10,6)
, there are 10 digits in the value, of which 4 are integers and 6 are decimals.
Example: decimal(2,1)
At this time, inserting data "12.3", "12", etc. will cause a "data overflow error" exception; inserting "1.23" or " 1.2345..." will be automatically rounded to "1.2"; inserting "2" will automatically fill in "2.0" to ensure an effective length of 2 digits, including 1 decimal place.
It is mentioned above that inserting "2" will automatically fill in "2.0", but in actual operation, it will not be filled in automatically.
[Related recommendations: mysql video tutorial]
The above is the detailed content of What type is used for prices in mysql?. For more information, please follow other related articles on the PHP Chinese website!