MySQL 如何在MySQL中存储十进制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16629759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:40:38  来源:igfitidea点击:

How to store decimal in MySQL?

mysql

提问by K20GH

I've tried using DECIMAL with (2,2) but it won't let me use this.

我试过将 DECIMAL 与 (2,2) 一起使用,但它不会让我使用它。

I simply want to store a number, for example 7.50 or 10.50. I need to keep both numbers after the decimal though but when I refresh the database it resets the values to 0.99. Any suggestions?

我只想存储一个数字,例如 7.50 或 10.50。我需要保留小数点后的两个数字,但是当我刷新数据库时,它会将值重置为 0.99。有什么建议?

回答by Explosion Pills

The first digit of the DECIMALdeclaration is the totaldigits. You probably want to use DECIMAL (4, 2). This allows for up to two digits before the decimal and two after.

DECIMAL声明的第一位数字是位数。您可能想要使用DECIMAL (4, 2). 这允许小数点前最多两位数和后两位数。

Documentation: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html

文档:https: //dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html

回答by hol

The syntax is DECIMAL(M,D)

语法是 DECIMAL(M,D)

M - total length

M - 总长度

D - digits right of the decimal point

D - 小数点右边的数字

http://dev.mysql.com/doc/refman/5.6/en/fixed-point-types.html

http://dev.mysql.com/doc/refman/5.6/en/fixed-point-types.html

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.6 are as follows:

M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

DECIMAL 列的声明语法是 DECIMAL(M,D)。MySQL 5.6 中参数的取值范围如下:

M 是最大位数(精度)。它的范围是 1 到 65。(旧版本的 MySQL 允许范围是 1 到 254。)

D 是小数点右边的位数(刻度)。它的范围是 0 到 30,并且不能大于 M。

回答by Aditya

CREATE TABLE IF NOT EXISTS `table_name` (`id` int(11) NOT NULL AUTO_INCREMENT,`cost` DECIMAL( 10, 2 ) NOT NULL);

This will make the cost column hold a total of 10 digits, 8 before and 2 after the decimal point.

这将使成本列总共包含 10 位数字,小数点前 8 位和小数点后 2 位。

回答by Visruth

From mysql doc:

从 mysql 文档

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments are as follows:

  • M is the maximum number of digits (the precision). It has a range of 1 to 65.
  • D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

DECIMAL 列的声明语法是 DECIMAL(M,D)。参数值的范围如下:

  • M 是最大位数(精度)。它的范围是 1 到 65。
  • D 是小数点右边的位数(刻度)。它的范围是 0 到 30,并且不能大于 M。

NB:- M is total no. of digits before decimal point + total no. of digits after decimal point.

注意:- M 是总编号。小数点前位数+总数 小数点后的位数。

In your case, 7.50has a total no of 3 digits and 10.50has a total no of 4 digits. But the declared maximum no. of digits for the column is 2 so it can store a maximum of two digits value. You cannot even store 1 because it's 1.00 i.e. total 3 digits. So the maximum allowed 2 digit value in the column is .99.

在您的情况下,7.50总共有 3 位数字,10.50总共有 4 位数字。但声明的最大数量没有。列的位数为 2,因此它最多可以存储两位数字值。您甚至无法存储 1,因为它是 1.00,即总共 3 位数字。因此该列中允许的最大 2 位数值为.99.

If you want to store xx.xx then you have to declare (4, 2) where 4 is M and 2 is D.

如果要存储 xx.xx,则必须声明 (4, 2),其中 4 是 M,2 是 D。

If you want to store any number at maximum allowed size of mysql then you can declare a column with (65, 30).

如果要以 mysql 的最大允许大小存储任何数字,则可以使用 (65, 30) 声明一个列。

Maximum no. of digits before decimal point = M - D

最大数量 小数点前的位数 = M - D

回答by Optimizer

CREATE TABLE `salary` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`salary` DECIMAL(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DECIMAL(10,2)indicates that salarywill hold a total of 10 digits out of which 2 will be after the decimal.

DECIMAL(10,2)表示salary将包含总共 10 位数字,其中 2 位将在小数点后。

i.e.

IE

8 digits before decimal and 2 digits after decimal.

小数点前8位,小数点后2位。

回答by Bengau

Change your field from INT to FLOAT

将您的字段从 INT 更改为 FLOAT