MySQL floating point precision range and rounding
黄舟
黄舟 2017-05-18 10:51:23
0
3
946

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
迷茫

Because when you created the table, you specified f3 as float(6,2), so what do these 6 and 2 mean?
6 is the total number of digits. 2 means only 2 digits are retained after the decimal point. So it will be displayed as 9999.99, which is a total of 6 9s, and there are 2 digits after the decimal

PHPzhong

It is recommended to use decimal, especially when it comes to money issues. In mysql, float and double (or real) are floating point numbers, and decimal (or numberic) is a fixed point number.

The advantage of floating-point numbers over fixed-point numbers is that when the length is certain, floating-point numbers can represent a larger data range; its disadvantage is that it can cause accuracy problems.

In future applications of floating-point numbers and fixed-point numbers, everyone should remember the following points:

1. There is an error problem in floating point numbers;

2. Accuracy-sensitive data such as currencies should be represented or stored in fixed-point numbers;

3. In programming, if floating point numbers are used, special attention should be paid to error issues and try to avoid floating point comparisons;

4. Pay attention to the handling of some special values ​​in floating point numbers.
You can check out this http://blog.csdn.net/lingmao5...

小葫芦

float(m,d) In, m represents the total number of digits, d represents the number of digits to the right of the decimal point

create table example3 (
    f1 float,
    f2 float(5,2),
    d1 double,
    d2 double(5,2)
);

insert into example3(f1, f2, d1, d2)
            values(3.141592657,3.141592657,3.141592657,3.141592657),
                  (3,3,3,3);

Displayed as follows:

f1 f2 d1 d2
3.14159 3.14 3.141592657 3.14
3 3.00 3 3.00

Explanation:

  • Not specified m,d: Take the value according to the range of float and double

  • Specify m,d : 按照 m,d: Take it according to the value of

    . If it exceeds, it will be rounded. If it does not exceed, it will be supplemented with 0
🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template