MYSQL:DECIMAL,逗号后精确到10位数字

发布于 2024-11-17 15:50:34 字数 145 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装纯掩盖桑 2024-11-24 15:50:34

第一个数字是要存储的总位数,第二个数字是小数部分的位数。因此 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 like DECIMAL (20, 10) to allow 10 digits on both the integral and fractional parts.

雪落纷纷 2024-11-24 15:50:34

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). With 10,10, you're saying "10 digits after the decimal", leaving 10-10 = 0 before the decimal. This means you cannot store 1, and 0.9999999999 is the nearest value that fits within your field definition.

红颜悴 2024-11-24 15:50:34

@Xint0 的答案是正确的。您已将精度和小数位设置为相同的位数,因此只能插入小于 1.0 的值。

正在发生的另一件事是 MySQL 默认行为,如果值不适合列的数据类型,则会截断值。

mysql> CREATE TABLE foo (dec DECIMAL(10,10));
mysql> INSERT INTO foo VALUES (1.0);
Query OK, 1 row affected, 1 warning (0.00 sec)

请注意,这会生成警告。

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1264 | Out of range value for column 'dec' at row 1 |
+---------+------+----------------------------------------------+

mysql> SELECT * FROM foo;
+--------------+
| dec          |
+--------------+
| 0.9999999999 |
+--------------+

您可以使用严格的 SQL 模式将警告变成错误:

mysql> SET SQL_MODE = STRICT_ALL_TABLES;
mysql> INSERT INTO foo VALUES (1.0);
ERROR 1264 (22003): Out of range value for column 'dec' at row 1

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.

mysql> CREATE TABLE foo (dec DECIMAL(10,10));
mysql> INSERT INTO foo VALUES (1.0);
Query OK, 1 row affected, 1 warning (0.00 sec)

Note this generates a warning.

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1264 | Out of range value for column 'dec' at row 1 |
+---------+------+----------------------------------------------+

mysql> SELECT * FROM foo;
+--------------+
| dec          |
+--------------+
| 0.9999999999 |
+--------------+

You can turn the warning into an error with the strict SQL mode:

mysql> SET SQL_MODE = STRICT_ALL_TABLES;
mysql> INSERT INTO foo VALUES (1.0);
ERROR 1264 (22003): Out of range value for column 'dec' at row 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文