MySQL data type double and decimal differences detailed

Source: Internet
Author: User

See: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt336

A real number is one with a fractional part. However, they are not just for storing fractional parts, but they can also be used

Declmal stores an integer larger than the bigint. MySQL supports both exact types and imprecise types.

The FLOAT and DOUBLE types support approximate calculations using standard muddy point operations. If you need to know that floating-point operations

How to calculate the specific implementation of the floating-point number of the platform to be used by the Institute.

The decimal type is used to store exact decimals. In MySQL 5.0 and later versions, the DECIMAL type supports accurate calculations. MySQL 4.1 and earlier use floating-point arithmetic for deciaml calculations, which can cause some strange results due to the loss of precision. In these versions of MySQL, Declmal is just a "storage type".

Because the CPU does not support direct computation of the declmal, MySQL server itself implements a high-precision calculation of DECIMAL in MySQL 5.0 and later versions. In contrast, the CPU directly supports native floating-point calculations, so floating-point operations are significantly faster.

Both floating-point and DECIMAL types can specify precision. For the decimal column, you can specify the maximum number of digits allowed before and after the decimal point. This affects the space consumption of the column. MySQL 5.0 and later save the digital package to a binary string (9 digits per 4 bytes). For example, Declmal (18, 9) will store 9 numbers on each side of the decimal point, using 9 bytes altogether: the digits before the decimal point are 4 bytes, the digits after the decimal point are 4 bytes, and the decimal point itself is 1 bytes.

The Declmal type in MySQL 5.0 and later versions allows up to 65 digits. In earlier versions of MySQL, this limit was 254 digits and was saved as an uncompressed string (one byte per digit). However, these (early) versions do not actually use such a large number in calculations, because DECIMAL is just a storage format: Declmal is converted to a DOUBLE type in the calculation.

There are several ways to specify the precision required for floating-point columns, which allows MySQL to quietly select different data types, or to trade-offs the values when stored. These precision definitions are non-standard, so we recommend specifying only the data type and not specifying the precision.

Floating-point types typically use less space than DECIMAL when they store values of the same range. FLOAT uses 4 bytes of storage. Double takes 8 bytes, which has a higher precision and a larger range than float. As with the integer type, only the storage type Imysql can be selected using DOUBLE as the internal floating-point calculation type.

Because additional space and computational overhead are required, it should be used only when the decimal is accurately calculated

Decimal one by one for example, stores financial data. However, when the amount of data is relatively large, you can consider using BIGINT instead

Decimal, the unit of currency to be stored is multiplied by the number of decimal places. Suppose you want to store the wealth

Data to one out of 10,000 points, you can multiply all the amounts by 1 million, then store the results in Bigi-cho,

This avoids the problem of inaccurate calculation of floating-point storage and the high cost of accurate DECIMAL computation.

MySQL data type double and decimal differences detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.