【问题标题】:How can i Set decimal value in SQL (something like 10.12345678)我如何在 SQL 中设置十进制值(类似于 10.12345678)
【发布时间】:2019-09-26 08:06:27
【问题描述】:

不能在 SQL 中存储十进制值。 想像十进制(2,8)一样存储

点赞 32.12345678

[Column(TypeName = "decimal(2,8)")]
        [Display(Name = "Latitude")]
        public decimal Latitude { get; set; }

在 NuGet 控制台中

Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [DredgingDatas] (
    [Id] int NOT NULL IDENTITY,
    [DredgerId] int NOT NULL,
    [DredgingTime] datetime2 NOT NULL,
    [Density] decimal NOT NULL,
    [Velocity] decimal NOT NULL,
    [Production] decimal NOT NULL,
    [Latitude] decimal(2, 8) NOT NULL,
    [Longitude] decimal(2, 8) NOT NULL,
    [Tide] decimal NOT NULL,
    [DredgeHead] float NOT NULL,
    CONSTRAINT [PK_DredgingDatas] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_DredgingDatas_Dredgers_DredgerId] FOREIGN KEY ([DredgerId]) REFERENCES [Dredgers] ([DredgerId]) ON DELETE CASCADE
);

The scale (8) for column 'Latitude' must be within the range 0 to 2.

【问题讨论】:

  • 错误很明显 - 值的顺序错误。使用(8,2)。两位数对坐标没有意义,你可能想要(10,8)
  • 另一方面,如果您想执行空间搜索,您可能需要使用 SQL Server 的空间几何或地理类型
  • decimal(p,s) 规范中逗号前的数字是数字中的总位数,因此要存储您的样本编号,您需要使用 decimal(10,8)
  • 感谢您的帮助,成功了。但这次我不需要空间数据库。

标签: sql sql-server entity-framework


【解决方案1】:

您的问题是对十进制数据类型的轻微误解。声明的第一部分是数据字段的总长度,第二部分是小数的数量。

例如:decimal(5,3) 显示总精度为 5 位,其中 3 位为小数位 (12.345)。

我认为您正在寻找数据类型 decimal(10,8),它将为您提供总共 10 位数字,其中 8 位在小数点后

这是解释精度如何工作的文档的链接;

https://docs.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-server-2017

【讨论】:

  • 感谢 Bennar 先生的热心帮助,成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 2010-10-23
  • 2014-03-04
  • 2017-05-03
  • 2014-03-10
相关资源
最近更新 更多