- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 decimal
类型的变量,其值为 1.0
。我将它保存到 SQL Server 2012 表的一个列中,该表的类型是 decimal(10, 8)
。
检索值后,我看到它是 1,但是当我调用 ToString()
时,返回的值是 "1.00000000"
(见下文)。
我意识到小数点后8位对应数据库中的数据类型。但是, Entity Framework 生成的属性中没有赋予它这种行为的属性或任何东西,所以我不知道这是怎么发生的。
以下是我在立即窗口中进行的一些测试:
myDecimal
1
myDecimal.ToString()
"1.00000000"
myDecimal == 1
true
myDecimal == 1.0m
true
正如您从最后 2 次测试中看到的那样,这也不是浮点错误的情况(不是我期望的,因为十进制是定点数,但我不得不尝试,因为我用完了想法)。
知道小数的 ToString()
是如何产生一个有 8 位小数的字符串的吗?
编辑:为了比较,请看下面的测试。
1m.ToString()
"1"
最佳答案
原因是小数类型没有规范化。同一个数字有多种表示形式,这些表示形式将表示为不同的字符串。
这不是您的数据库类型的特殊属性,这是 decimal 的正常工作方式。没有特殊的 DataAnotation 或任何附加到变量的东西。
(1m).ToString() == "1"
(1.00000000m).ToString() == "1.00000000"
((1m)==(1.00000000m)) == true
对于给定的 double,只有一种有效表示,即 mantissa * 2exponent
的一种组合对于十进制,mantissa * 10exponent 有多个有效表示。每个代表相同的数字,但通过多种可能的表示形式提供的附加信息用于在将十进制转换为字符串时选择尾随数字的默认数量。确切的细节并没有很好的记录,而且我没有找到任何关于在添加或乘以小数时指数究竟发生了什么的信息。但它对 ToString() 的影响很容易验证。
缺点是 Equals() 和 GetHashCode() 操作比规范化数字格式更复杂,并且在实现中存在细微错误:C# Why can equal decimals produce unequal hash values?
This article by Jon Skeet goes into a bit more detail :
A decimal is stored in 128 bits, even though only 102 are strictly necessary. It is convenient to consider the decimal as three 32-bit integers representing the mantissa, and then one integer representing the sign and exponent. The top bit of the last integer is the sign bit (in the normal way, with the bit being set (1) for negative numbers) and bits 16-23 (the low bits of the high 16-bit word) contain the exponent. The other bits must all be clear (0). This representation is the one given by decimal.GetBits(decimal) which returns an array of 4 ints. [...]
The decimal type doesn't normalize itself - it remembers how many decimal digits it has (by maintaining the exponent where possible) and on formatting, zero may be counted as a significant decimal digit.
您可以通过比较 decimal.GetBits() 返回的值来验证您拥有的两个小数是否不相同,即:
decimal.GetBits(1m) == {int[4]}
[0]: 1
[1]: 0
[2]: 0
[3]: 0
decimal.GetBits(1.00000000m) == {int[4]}
[0]: 100000000
[1]: 0
[2]: 0
[3]: 524288
依赖这种行为来格式化小数可能很诱人,但我建议在转换为字符串时始终明确选择精度,以避免混淆和不可预见的意外,例如,如果数字事先乘以某个因子。
关于c# - 为什么这个小数点在 ToString() 上显示小数点后 8 位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27268008/
我试图找到一种方法来限制 Haskell,以便返回只有 2 个十进制名称的数字(例如 1.24) 我的代码是: sumSquares = sum . map (^ 2) 在 Haskell 中如何做到
我在向输入框添加停靠点验证时遇到一些问题。仅允许数字的限制(第二个条件)有效,但第一个条件也可能不存在。输入框中仍然没有出现句号和小数点。 function isNumberKey(evt) {
如何处理R中的p值? 我期望非常低的 p 值,例如: 1.00E-80 我需要-log10 -log10(1.00E-80) -log10(0) 是 Inf,但 Inf 也有四舍五入的感觉。 但似乎在
我的数据库中有一个包含货币字段的表。我已经为该货币字段创建了实体并创建了 decimal 属性。当该字段的值显示在我的 MVC3 View 上时,它在小数点后有四个零 0000,如下所示:5489.0
label.text = [NSString stringWithFormat:@"%.2f",final]; 上面的语句显示变量“final”中可用的浮点值,小数点后两位。 我想显示小数位数,具体取
有没有办法在 Qt 5 中绘制带有小数点大小的文本。我正在尝试使用 QFont::setPointSizeF() 但它似乎在我尝试过的任何平台(mac/linux/windows)上都不起作用,并且点
我有一个关于如何将基数为 10 的数字转换为 IEEE 754 浮点表示的示例 Number: 45.25 (base 10) = 101101.01 (base 2) Sign: 0 Normali
我有一个包含以下值的 CSV 文件: 3271.96;274;272;1;1;0;1;0.071690;0;0;0;0;0;0;1.753130;1.75;0;1.75; 但是当我用 Excel 打开
有人可以对 here 中的这些特殊字符示例给出更好的解释吗? ?或者提供一些更清楚的例子? (x) The '(foo)' and '(bar)' in the pattern /(foo) (bar
我编写了 change() 来帮助我将字符串转换为 double。 例如:如果字符串是“5.5”,我希望数字是5.5。如果字符串是“0.0”,我希望数字是0。另一个例子:“50”到50。 现在的问题是
我正在尝试确定 python float 与零进行比较时的精确度。请注意以下几点: 1e-323 == 0 > False 1e-324 == 0 > True 我认为阈值是 324 个小数点,至少对
我想使用 .ftr 文件来快速分析数百个表。不幸的是,我在十进制和千位分隔符方面遇到了一些问题,类似于 that post ,只是 read_feather 不允许 decimal=',', thou
在 SQL Server 2008 中,我正在运行一个报告,我需要在其中划分两列(称为 Completes 和 Prescreens,整数),然后转到 Excel并得到 Prescreens 除以 C
我有一个 NSString,我想将其转换为 NSDecimalNumber。该字符串是从服务器接收的,并且始终使用 en_US 语言环境进行格式化,例如 XXX.YYY 而不是 XXX,YYY。我想创
我是一名优秀的程序员,十分优秀!