mysql: column type float, double

mysql: column type float, double_mysql double_Ordinary netizens' blog-CSDN blog

environment:

    window10
    vs2022
    .net 6
    mysql 8.0.25
    DBeaver

    Reference:
    "MSDN: Floating-Point Types (C# Reference)"
    "mysql: 11.1.4 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE"

1. float and double types

In mysql, float and double are both floating point number types:

    float occupies 4 bytes and has a precision of 6 bits;
    double occupies 8 bytes and has a precision of 16 bits;

Their properties are the same as those of floating point numbers in C#, except that the precision requirements are more strict and clear;

    Reference: The precision requirements for floating point types in C# are Insert picture description here

Test float and double in mysql:

create table test(
   t_float float,
   t_double double
)

insert into test.test (t_float) values(123456);-- Normal
insert into test.test (t_float) values(1234567);-- Four settings and five inputs 1234570
insert into test.test (t_float) values(12345678901);- - Distortion12345700352

insert into test.test (t_float) values(12345.6); -- Normal
insert into test.test (t_float) values(12345.62); -- Set four and five into 12345.6
insert into test.test (t_float) values(123.123); - - Normal
insert into test.test (t_float) values(12.1234); -- Normal
insert into test.test (t_float) values(1.12345); -- Normal
insert into test.test (t_float) values(1234.1234); -- Four Let five inputs be 1234.12

-- double
insert into test.test (t_double) values(1234567890123456);-- Normal
insert into test.test (t_double) values(12345678901234567);-- Four settings and five inputs 12345678901234568
insert into test.test (t_double) values(123.12345678 901 );-- normal
insert into test.test (t_double) values(1234567890.123456);-- normal

select * from test

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

The output is as follows:
Insert image description here

We can think of it like this:
when we insert data into the float type, mysql will give priority to retaining 6 significant digits and round the numbers on the right;
when we insert data into the double type, mysql will give priority to retaining 16 significant digits. , set four to five numbers on the right;

    Note: mysql will not report an error because the data exceeds the range, for example: 12345678901 is far beyond the accuracy, but it will only distort the data and will not report an error.

2. float and double syntax

The recommended usage methods are: t_float float, t_double double, that is, no other parameters are added.

However, in order to be compatible with SQL standards, mysql provides: float(p). The value range of p here is: [0,53]. If it exceeds the range, an error will be reported. In addition: p here is only used to determine the internal use of float in mysql. It is still double storage. When p∈[0,23], mysql uses float storage. When p∈[24,53], mysql uses double storage.
3. Non-recommended usage of float and double

What is mentioned above is the recommended usage. In the recommended usage, the behavior of MySQL's float and C#'s float are basically the same.

There are also non-standard usages, such as: float(M,D), but this way of writing is about to be deprecated.

Although it is deprecated, it seems to be not useless:
M in float(M,D) represents the total number of significant digits, and D represents the number of decimal places. This is a bit like decimal(M,D) with M and D.

If we define: float(5,2), it means: a maximum of 2 digits after the decimal point and a maximum of 3 digits before the decimal point. If there are more than 3 digits before the decimal point, an error will be reported. If there are more than 2 digits after the decimal point, it will be rounded off;

The test is as follows:

create table test(
    t_float5_2 float(5,2)
)

insert into test.test (t_float5_2) values(123.12);-- Normal
insert into test.test (t_float5_2) values(1234.1);-- Error: Out of range value
insert into test.test (t_float5_2) values(1.1234); -- Four settings and five inputs 1.12

select * from test

    1
    2
    3
    4
    5
    6
    7
    8
    9

The output is as follows:
Insert image description here

You can see that this non-standard usage is more restrictive, but because it is about to be enabled, we won’t explore it too much.

    In addition, there is no non-standard double(M,D) writing method for double type!

In addition, in addition to float(M,D), there are also real(M,D) and double precision(M,D).

Here is a summary of their usage and differences:

Insert picture description here
4. How to use it in c#

Since float and double in mysql are floating-point data, they basically correspond to float and double in c#, so in c# we can use float and double to replace them respectively, but generally we will use them uniformly in c# double, as follows:

public class Model
{
    public double? t_float { get; set; }
    public double? t_double { get; set; }
}

    1
    2
    3
    4
    5

5. Finally, take a look at their metadata

create table test(
    t_float float,
    
    t_float7 float(7),
    t_float10 float(10),
    t_float35 float(35),
    t_float40 float(40),    
    
    t_double double,
--  t_double double(5), -- 报错
    
    t_float7_5 float(7,5), -- 弃用
    
    t_real real,
    t_real7_5 real(7,5),
    
    t_double_precision8_5 double precision(8,5)
)

select c.TABLE_SCHEMA ,c.TABLE_NAME ,c.COLUMN_NAME ,c.ORDINAL_POSITION,c.DATA_TYPE,c.NUMERIC_PRECISION ,c.COLUMN_TYPE ,c.NUMERIC_SCALE
from information_schema.`COLUMNS` c  where TABLE_SCHEMA ='test' and TABLE_NAME ='test' order by ORDINAL_POSITION

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

Insert image description here
Finally

I know that most junior and intermediate Java engineers who want to improve their skills often have to explore and grow on their own or enroll in classes. However, the tuition fees of nearly 10,000 yuan for training institutions are really quite stressful. The effect of self-study without a system is inefficient and lengthy, and it is easy to hit the ceiling and stagnate in technology!

Therefore, I have collected and compiled a "Complete set of Java development learning materials" for everyone. The original intention is also very simple. I hope it can help friends who want to learn and improve themselves but don't know where to start, and at the same time reduce everyone's burden.

The editor has encrypted: aHR0cHM6Ly9kb2NzLnFxLmNvbS9kb2MvRFVrVm9aSGxQZUVsTlkwUnc==For security reasons, we have encoded the website through base64, and you can get the URL through base64 decoding.
——————————————
Copyright statement: This article is an original article by the CSDN blogger “ordinary netizen” and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this copy when reprinting statement.
Original link: https://blog.csdn.net/m0_67392661/article/details/126803834

Guess you like

Origin blog.csdn.net/sinat_30603081/article/details/132850890