MYSQL:DECIMAL,逗号后精确到10位数字
在 MySQL 中,我将 DECIMAL 字段设置为 10,10。但是,每当我输入新值时,它都会显示 0.9999999999。我需要更改什么才能接受逗号后精确到 10 位数字的任何数字?由于某种原因,它确实可以与 10,6 一起使用。
PS:我想插入汇率。
In MySQL I have a DECIMAL field set to 10,10. However, whenever I enter a new value, it shows 0.9999999999. What would I need to change to accept any number with an accuracy of 10 digits after the comma? It does work with 10,6 for some reason.
PS: I want to insert exchange rates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个数字是要存储的总位数,第二个数字是小数部分的位数。因此
DECIMAL (10, 10)
仅适用于 10 个小数位。您可以使用DECIMAL (20, 10)
之类的内容来允许整数部分和小数部分都包含 10 位数字。The first number is the total number of digits to store, the second one is the number of digits on the fractional part. So
DECIMAL (10, 10)
is only for 10 fractional digits. You can use something likeDECIMAL (20, 10)
to allow 10 digits on both the integral and fractional parts.DECIMAL(10,10) 表示小数点之前的值没有小数位。您将事物限制为始终
x < 1.
.它是
DECIMAL(总位数,小数点后的位数)
。对于10,10
,您所说的是“小数点后 10 位”,在小数点前留下10-10 = 0
。这意味着您无法存储 1,并且 0.9999999999 是最适合您的字段定义的值。DECIMAL(10,10) means there's no decimal places left for values before the decimal place. You're limiting things to always being
x < 1
.It's
DECIMAL(total number of digits, digits after the decimal)
. With10,10
, you're saying "10 digits after the decimal", leaving10-10 = 0
before the decimal. This means you cannot store 1, and 0.9999999999 is the nearest value that fits within your field definition.@Xint0 的答案是正确的。您已将精度和小数位设置为相同的位数,因此只能插入小于 1.0 的值。
正在发生的另一件事是 MySQL 默认行为,如果值不适合列的数据类型,则会截断值。
请注意,这会生成警告。
您可以使用严格的 SQL 模式将警告变成错误:
The answer from @Xint0 is correct. You've set the precision and scale to the be same number of digits, so you can only insert values less than 1.0.
The other thing that's going on is MySQL default behavior of truncating values if they don't fit in the data type for the column.
Note this generates a warning.
You can turn the warning into an error with the strict SQL mode: