简体   繁体   English

四舍五入[c#]

[英]Large numbers rounding off [c#]

I am having some weird issue here. 我在这里有一些奇怪的问题。 I have a database table which has huge value stored on a column. 我有一个数据库表,其中存储了巨大的值。 My application (C#) is reading this value and keeping in a double type. 我的应用程序(C#)正在读取此值并保持双精度类型。 This application will insert the same value to another table. 此应用程序将相同的值插入另一个表。 Note : I am not doing any calculations/processing on the value read from the first table. 注意:我没有对从第一个表读取的值进行任何计算/处理。 It is just kept for updating the second table. 它只是用于更新第二个表。

Issue here is, the second table is getting slightly different value than in the first table. 这里的问题是,第二个表的值与第一个表的值略有不同。 Looks like the number is rounding off when I keep in the double type. 当我保持双重类型时,看起来数字正在四舍五入。

Here is an example of values. 这是一个值的例子。

Original Value : 18014398509481984 原始值: 18014398509481984

Value copied to new table : 18014398509482000 复制到新表的值: 18014398509482000

The values looks different, but both are same in reality. 值看起来不同,但实际上两者都是相同的。 I did a google search with 18014398509481984 - 18014398509482000 as a search term and it returned result 0 , which means both are same. 我用18014398509481984 - 18014398509482000作为搜索词进行了谷歌搜索,它返回结果0 ,这意味着两者都是相同的。

Questions : 问题

1 - If both are same, why the second value looks different? 1 - 如果两者相同,为什么第二个值看起来不同? I can see 1984 turned into 2000 . 我可以看到1984年变成了2000年

2 - Why the conversion happens? 2 - 为什么要进行转换?

3 - How can I avoid this type of conversions? 3 - 如何避免此类转换?

Any help would be great! 任何帮助都会很棒!

Try using a System.Decimal to store the value from the first table, instead of a System.Double . 尝试使用System.Decimal存储第一个表中的值,而不是System.Double System.Double doesn't seem to contain enough significant digits to store that large of a value accurately. System.Double似乎没有足够的有效数字来准确地存储那么大的值。

A double precision value is accurate only to 15 or 16 decimal digits (see here for an explanation). 双精度值仅精确到15或16位十进制数字(有关说明,请参见此处 )。 If you need to store more than this, then you will have to use a different number format. 如果您需要存储更多,则必须使用不同的数字格式。 If you want to work with very big integers without losing accuracy, then there are various classes out there to help you like this one . 如果你想使用非常大的整数而不会失去准确性,那么有各种各样的课程来帮助你喜欢这个

If you're getting a value out of SQL, make sure that your target data type in .NET matches - SQL bigint for C# long for example - to avoid rounding issues like this. 如果您从SQL获得了一个值,请确保.NET中的目标数据类型匹配 - 例如C#的SQL bigint - 以避免像这样的舍入问题。

I believe this is due to floating point precision (the large number will use a mantissa an exponent), which means it would essentially be represented as a factional number with a power. 我相信这是由于浮点精度(大数将使用尾数指数),这意味着它基本上表示为具有幂的派系数。 Fractional numbers however encounter rounding errors due to floating point arithmetic. 然而,由于浮点运算,小数会遇到舍入误差。

Normally the way round this is to avoid floating point values (try Int64), use a more precise type (Decimal) or account for the error and do an 'approx equal to'. 通常,绕过这一点的方法是避免浮点值(尝试Int64),使用更精确的类型(十进制)或说明错误并执行'大约等于'。

Do you need to store these as floating point numbers? 你需要将它们存储为浮点数吗?

If not then you could use 64-bit integers instead: BIGINT in the database, and long / Int64 in your app. 如果没有,那么你可以使用64位整数:数据库中的BIGINT和应用程序中的long / Int64

These have a range from –9,223,372,036,854,775,808 up to 9,223,372,036,854,775,807 and no precision/accuracy issues. 它们的范围从-9,223,372,036,854,775,808到9,223,372,036,854,775,807并且没有精度/准确度问题。

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

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