Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use decimal in MySQL

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

MySQL how to use decimal, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Decimal introduction

When designing a database, if you want to store data with decimal points such as prices, such as prices, I will use the decimal type and will not consider float,double, because they are prone to errors. Numeric and decimal are synonymous with numeric, and numeric will be automatically converted to decimal.

Ps: as for why there are errors, please see my other article: floating-point calculation problems in php.

Decimal usage

DECIMAL was introduced from MySQL 5. 1, and the declaration syntax for columns is DECIMAL (MMagne D).

In MySQL 5.1, the range of parameters is as follows:

M is the maximum number of numbers (precision). The range is 1: 65 (in older versions of MySQL, the allowed range is 1: 254), and the default value for M is 10.

D is the number of digits to the right of the decimal point (scale). Its range is 030, but not more than M.

Description: 4 bytes for float, 8 bytes for double, and 2 bytes for decimail.

For example, the maximum value of DECIMAL (5J2) is 9999.99 because there are 7 bytes available.

First of all, for things with high precision, such as money, I will use the decimal type and will not consider float,double, because they are prone to errors, numeric and decimal are synonymous, and numeric will be automatically converted to decimal.

DECIMAL was introduced from MySQL 5. 1, and the declaration syntax for columns is DECIMAL (MMagne D). In MySQL 5.1, the range of parameters is as follows:

M is the maximum number of numbers (precision). The range is 1: 65 (in older versions of MySQL, the allowed range is 1: 254), and the default value for M is 10.

D is the number of digits to the right of the decimal point (scale). Its range is 030, but not more than M.

Description: 4 bytes for float, 8 bytes for double, and 2 bytes for decimail.

For example, the maximum value of DECIMAL (5) is 9 99 9. 9 9, because there are 7 bytes available.

The influence of M and D on the value range of DECIMAL (M, D).

Value range of type description (MySQL

< 3.23)取值范围(MySQL >

= 3.23)

MySQL

< 3.23 MySQL >

= 3.23

DECIMAL (4,1)-9.9 to 99.9-999.9 to 9999.9

DECIMAL (5Pol 1)-99.9 to 999.9-9999.9 to 99999.9

DECIMAL (6Jing 1)-999.9 to 9999.9-99999.9 to 999999.9

DECIMAL (6Pol 2)-99.99 to 999.99-9999.99 to 99999.99

DECIMAL (6p3)-9.999 to 99.999-999.999 to 9999.999

# in MySQL 3.23 and later, the value range of DECIMAL (M, D) is equal to that of DECIMAL (M + 2, D) in earlier versions.

Conclusion

1. When the value is within its range and there are more decimal places, the decimal will be rounded.

two。 If the value is outside its range, it is filled with the maximum (minimum) value.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report