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

What is the difference between float double and decimal types in MySQL

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

Share

Shulou(Shulou.com)06/01 Report--

The following mainly brings you the difference between float double and decimal types in MySQL. I hope these contents can bring you practical use, which is also the main purpose of this article that I edit the difference between float double and decimal types in MySQL. All right, don't talk too much nonsense, let's just read the following.

The float numeric type is used to represent single-precision floating-point values, while the double numeric type is used to represent double-precision floating-point values

Both float and double are floating-point, while decimal is fixed-point.

MySQL floating-point and fixed-point types can be represented by the type name followed by (MMagneD).

M represents the total length of the value, and D represents the length after the decimal point.

M and D are also known as precision and scaling, such as float (7 and 4) can be displayed as-999.9999

MySQL rounds the value when it is saved, and if you insert 999.00009, the result is 999.0001.

When ☆ FLOAT and DOUBLE do not specify precision, the default is displayed according to the actual precision, while when DECIMAL does not specify precision, the default integer is 10 and the decimal is 0.

Test 1:

CREATE TABLE test1 (f FLOAT (5) DEFAULT NULL,d DOUBLE (5) DEFAULT NULL,de DECIMAL (5) DEFAULT NULL); INSERT INTO test1 (frech d Magne de) VALUES (1.23) 1.23)

The data is inserted correctly:

two。

INSERT INTO test1 (frech djinde) VALUES (1.234 1.234 1.23)

The data was inserted correctly, but f and d dropped the last bit due to the limitation of the scale.

3.

INSERT INTO test1 (frecinct dpende) VALUES (1.234 pencils 1.234)

Insert succeeded with warning

4. Get rid of the precision and scale of fdline.

INSERT INTO test1 (frecinct dpende) VALUES (1.234 pencils 1.234)

Insert correctly [data for f and d are inserted correctly, and de is truncated], and there will be the same prompt as 3!

If the floating point number does not write precision and scale, it will be displayed according to the actual display. If there is precision and scale, the data will be rounded and inserted. The system will not report an error. If the fixed point number does not set the precision and scale, it will just operate in accordance with the default (1010). If the data exceeds the precision and scale value, it will be warned!

5. Different results will appear when data is calculated for SUM (). There are many decimal points for float and double for SUM, while for decimal for SUM, exact values are obtained:

SELECT SUM (f), SUM (d), SUM (de) FROM test1

In theory: there should not be a long string of floating point decimal points after f.m.d.

No result can be found in the default length of 6.loat column type. Precision must be specified.

SELECT * FROM test1 WHERE f = 1.234

I can't find any columns.

7.

Float: floating-point type, containing 4 bytes, 32 bits, value range is-3.4E38~3.4E38 (7 significant bits)

Double: double precision real type with 8 bytes in 64-bit range-1.7E308~1.7E308 (15 significant bits)

Decimal: digital, 128bit, with no loss of accuracy (relatively non-existent, error will be reported after 28 significant digits), often used in bank account calculation. (28 significant bits)

Float f = 345.98756f / the result shows 345.9876, showing only 7 significant bits, rounding the last digit.

Double dashes 345.975423578631442d / result shows 345.975423578631, showing only 15 significant bits, rounding the last bit.

Note: float and double multiplication operation, digital overflow will not report an error, there will be a loss of accuracy.

Decimal dd=345.545454879.// can support 28 bits, rounding the last bit. Because of the high precision, 28 significant digits, this is the main reason used in financial calculation.

All floating-point calculations have a loss of accuracy, and decimal is also a floating-point number [fixed-point number], so there is also a loss of accuracy!

The following output is the same as no addition, and there are no exceptions. It also results in a loss of accuracy, but the significant digits of dd are more than 28 digits.

To sum up, if you are doing finance, use decimal, and if you are calculating values for human use, use decimal as well. Double feels like a chicken rib and can't think of anything to use. [online point of view, for reference only]

Do you find it very helpful about the differences between float double and decimal types in MySQL? If you need to know more, please continue to follow our industry information. I'm sure you'll like it.

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