简体   繁体   English

MySQL-存储大于1的数字,并带有大量小数位

[英]MySQL - Storing numbers, greater than 1, with a large number of decimal places

I need to use an accurate number that has many decimal places. 我需要使用具有许多小数位的准确数字。 If I use the type decimal (because float is not precise enough to store such numbers) I can have something like 0.003352466 and it is preserved accurately. 如果我使用decimal类型(因为float不够精确,无法存储此类数字),我可以得到类似0.003352466并且可以准确地保留它。 However if I make that number larger than 1, I cannot do it 1.003352466 will fail to be stored. 但是,如果我将该数字设置为大于1,将无法存储1.003352466

How do I store both types of numbers in one column? 如何将两种类型的数字存储在一列中?

Like, if one row has the value 0.003352466 but the next row needs to store 1.003352466 , how do I do this? 就像,如果一行的值是0.003352466但是下一行需要存储1.003352466 ,我该怎么做? I have not been able to figure this out. 我一直无法弄清楚。

FYI I had initially tried DECIMAL(10,10) and that suffered the same failure as mentioned above. 仅供参考,我最初尝试过DECIMAL(10,10),并且遭受了与上述相同的失败。


EDIT: 编辑:

I have tried specifying DECIMAL(2,10) but that fails as the number on the left must be equal to or greater than that on the right. 我尝试指定DECIMAL(2,10)但是失败,因为左侧的数字必须等于或大于右侧的数字。 SO I tried DECIMAL(10,10) and that simply fails to write to the database. 所以我尝试了DECIMAL(10,10)DECIMAL(10,10)简直无法写入数据库。 As I mentioned above it WILL let me enter 0.003352466 but not 1.003352466 正如我上面提到的,它将让我输入0.003352466但不能输入1.003352466

Solution... make the first number larger than the second and it works! 解决方案...使第一个数字大于第二个数字,它可以工作!

DECIMAL(10,10) will fail to write as mentioned but DECIMAL(10,9) will succeed. 如前所述, DECIMAL(10,10)将无法写入,但DECIMAL(10,9)将成功。 Go figure. 去搞清楚。

You probably have a wrong table definition (care to show it?). 您可能有错误的表定义(是否愿意显示?)。 From the manual : 手册

The declaration syntax for a DECIMAL column is DECIMAL(M,D). DECIMAL列的声明语法为DECIMAL(M,D)。 The ranges of values for the arguments in MySQL 5.1 are as follows: MySQL 5.1中参数的值范围如下:

M is the maximum number of digits (the precision). M是最大位数(精度)。 It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.) 它的范围是1到65。(较早版本的MySQL允许范围是1到254。)

D is the number of digits to the right of the decimal point (the scale). D是小数点右边的位数(标度)。 It has a range of 0 to 30 and must be no larger than M. 范围是0到30,并且不得大于M。

So, a DECIMAL(11,9) would work in this case, alter as required. 因此,在这种情况下, DECIMAL(11,9)将起作用,并根据需要进行更改。

Provide the precision to your field's declaration. 提供字段声明的精度。

DECIMAL(N, M) means " N digits total, with M decimal places". DECIMAL(N, M)表示“总共N位,小数位数为M ”。

Say, DECIMAL(10, 2) will allow you to store numbers from 0.00 to ±99999999.99 . 说, DECIMAL(10, 2)将允许您存储从0.00±99999999.99数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM