如何使用 MySQL 十进制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4834390/
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
How to use MySQL DECIMAL?
提问by Charles Zink
I can't quite get a grasp of MySQL's DECIMAL. I need the row to be able to contain a number anywhere from 00.0001 to 99.9999. How would I structure it to work like so?
我不太了解 MySQL 的 DECIMAL。我需要该行能够包含 00.0001 到 99.9999 之间的任何数字。我将如何构建它来像这样工作?
回答by Alex Recarey
DOUBLE columns are notthe same as DECIMAL columns, and you will get in troubleif you use DOUBLE columns for financial data.
双列不一样的DECIMAL列,你会遇到麻烦,如果你使用双列的财务数据。
DOUBLE is actually just a double precision (64 bit instead of 32 bit) version of FLOAT. Floating point numbers are approximate representations of real numbersand they are not exact. In fact, simple numbers like 0.01 do not have an exact representation in FLOAT or DOUBLE types.
DOUBLE 实际上只是FLOAT的双精度(64 位而不是 32 位)版本。浮点数是实数的近似表示,它们并不准确。事实上,像 0.01 这样的简单数字在 FLOAT 或 DOUBLE 类型中没有准确的表示。
DECIMAL columns are exact representations, but they take up a lot more space for a much smaller range of possible numbers. To create a column capable of holding values from 0.0001 to 99.9999 like you asked you would need the following statement
DECIMAL 列是精确表示,但对于更小范围的可能数字,它们占用更多空间。要创建一个能够像您要求的那样保存从 0.0001 到 99.9999 的值的列,您需要以下语句
CREATE TABLE your_table
(
your_column DECIMAL(6,4) NOT NULL
);
The column definition follows the format DECIMAL(M, D) where Mis the maximum number of digits (the precision) and Dis the number of digits to the right of the decimal point (the scale).
列定义如下格式DECIMAL(M,d)其中,中号是数字的最大数(精度)和d是位小数点(右边的数尺度)。
This means that the previous command creates a column that accepts values from -99.9999 to 99.9999. You may also create an UNSIGNED DECIMAL column, ranging from 0.0000 to 99.9999.
这意味着前面的命令创建了一个接受从 -99.9999 到 99.9999 的值的列。您还可以创建一个 UNSIGNED DECIMAL 列,范围从 0.0000 到 99.9999。
For more information on MySQL DECIMAL the official docsare always a great resource.
有关 MySQL DECIMAL 的更多信息,官方文档始终是一个很好的资源。
Bear in mind that all of this information is true for versions of MySQL 5.0.3 and greater. If you are using previous versions, you really should upgrade.
请记住,所有这些信息对于 MySQL 5.0.3 及更高版本都是正确的。如果您使用的是以前的版本,则确实应该升级。
回答by Cristiano
Although the answers above seems correct, just a simple explanation to give you an idea of how it works.
虽然上面的答案似乎是正确的,但只是一个简单的解释,让您了解它是如何工作的。
Suppose that your column is set to be DECIMAL(13,4)
. This means that the column will have a total size of 13 digits where 4 of these will be used for precision representation.
假设您的列设置为DECIMAL(13,4)
. 这意味着该列的总大小为 13 位,其中 4 位将用于精度表示。
So, in summary, for that column you would have a max value of: 999999999.9999
因此,总而言之,对于该列,您的最大值为: 999999999.9999
回答by Zoltán Hajdú
There are correct solutions in the comments, but to summarize them into a single answer:
评论中有正确的解决方案,但将它们总结为一个答案:
You have to use DECIMAL(6,4).
您必须使用 DECIMAL(6,4)。
Then you can have 6 total number of digits, 2 before and 4 after the decimal point (the scale). At least according to this.
然后你可以有 6 个总位数,小数点前 2 位和小数点后 4 位(刻度)。至少按照这个。
回答by Markus AO
MySQL 5.x specificationfor decimal datatype is: DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
. The answer above is wrong in saying that unsigned decimals are not possible.
十进制数据类型的MySQL 5.x规范是:DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
. 上面的答案是错误的说无符号小数是不可能的。
To define a field allowing only unsigned decimals, with a total length of 6 digits, 4 of which are decimals, you would use: DECIMAL (6,4) UNSIGNED
.
要定义一个仅允许无符号小数的字段,总长度为 6 位,其中 4 位是小数,您可以使用:DECIMAL (6,4) UNSIGNED
。
You can likewise create unsigned (ie. not negative) FLOAT and DOUBLE datatypes.
您同样可以创建无符号(即非负) FLOAT 和 DOUBLE 数据类型。