VB.NET 存储货币值的最佳数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2256064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:41:45  来源:igfitidea点击:

VB.NET best data type for storing currency values

vb.nettypescurrency

提问by burntsugar

What is the most appropriate data type for storing currency values in VB.NET?

在 VB.NET 中存储货币值的最合适的数据类型是什么?

回答by Mehrdad Afshari

Decimal(alias for System.Decimalstructurein the BCL) is designed for storing monetary values. It's a 128 bit decimal floating point type (as opposed to binary floating point) and is useful for storing "real-world" values with high decimal precision. By real-world, I specifically mean measurements that are originally made in decimal. Double is generally suitable for calculations that don't need as much accuracy when they are represented as decimal numbers.

Decimal(BCL 中System.Decimal结构的别名)设计用于存储货币值。它是一种 128 位十进制浮点类型(与二进制浮点相反),对于存储具有高十进制精度的“真实世界”值非常有用。在现实世界中,我特别指的是最初以十进制进行的测量。Double 通常适用于在表示为十进制数时不需要那么高的精度的计算。

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding. For example, the following code produces a result of 0.9999999999999999999999999999 rather than 1.

Decimal 值类型表示从正 79,228,162,514,264,337,593,543,950,335 到负 79,228,162,514,264,337,593,543,950,335 的十进制数。Decimal 值类型适用于需要大量有效整数和小数位且无舍入错误的财务计算。Decimal 类型不会消除四舍五入的需要。相反,它最大限度地减少了由于舍入而导致的错误。例如,以下代码产生的结果是 0.9999999999999999999999999999 而不是 1。