The use of decimal in mysql

Difference between float, double and decimal

Create table test_float_double_decimal

CREATE TABLE `test_float_double_decimal` (
  `id` int(11) NOT NULL COMMENT 'id',
  `float7.4` float(7,4) NOT NULL,
  `float255.30` float(255,30) NOT NULL,
  `double15.4` double(15,4) NOT NULL,
  `double255.30` double(255,30) NOT NULL,
  `decimal10.4` decimal(10,4) NOT NULL,
  `decimal65.30` decimal(65,30) NOT NULL,
  `float_default` float NOT NULL,
  `double_default` double NOT NULL,
  `decimal_default` decimal(10,0) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `test_float_double_decimal`(`float7.4`, `float255.30`, `double15.4`, `double255.30`, `decimal10.4`, `decimal65.30`, `float_default`, `double_default`, `decimal_default`) VALUES (789.123456789,123456789.123456789,123456789.123456789,123456789.123456789,789.123456789,123456789.123456789,123456789.123456789,123456789.123456789,123456789.123456789);
INSERT INTO `test_float_double_decimal`(`float7.4`, `float255.30`, `double15.4`, `double255.30`, `decimal10.4`, `decimal65.30`, `float_default`, `double_default`, `decimal_default`) VALUES (789.123456789,12345678987654321.12345678987654321,12345678987.12345678987654321,12345678987654321.12345678987654321,789.12345678987654321,12345678987654321.12345678987654321,12345678987654321.12345678987654321,12345678987654321.12345678987654321,1234567898.12345678987654321);


After the first piece of data is inserted, the floats are all deviated, the double is normal (the last digit value is rounded off), and the decimal is normal (the last digit value is rounded off).

After the second piece of data was inserted, it was found that the float was the most deviated, and the double also began to deviated. decimal is the most normal.

decimalDetails

decimal(a,b)

Parameter description
a specifies the maximum number of decimal digits that can be stored on the left and right sides of the specified decimal point, the default is 10, and the maximum precision is 65.
b specifies the maximum number of decimal digits that can be stored to the right of the decimal point. The number of decimal places must be a value from 0 to a. The default number of decimal places is 0 and the maximum value is 30.
Remarks
DECIMAL data types are used in calculations that require very high precision. These types allow specifying the precision of the numerical value and the counting method as selection parameters. Precision here refers to the total number of significant digits saved for the value, while count method refers to the number of digits after the decimal point. For example, the statement DECIMAL (5,2) specifies that the stored value will not exceed 5 digits, with 2 digits after the decimal point.

Summary: Data such as coordinates and money can be stored in decimal for those with high precision requirements.

decimal(10,2) 表示钱,decimal(10,6)可以表示坐标。

The coordinates are stored in the data table in the project, float is used, and the results are as follows.

All the data is lost. Originally it was followed by 6 decimals. After the above experiment summary, it is suitable to use decimal for storage. Double is also OK, but the number of digits needs to be set.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325021522&siteId=291194637