gpt4 book ai didi

c# - C#中的 "decimal"类型是二进制编码的十进制吗?

转载 作者:太空狗 更新时间:2023-10-30 00:04:57 24 4
gpt4 key购买 nike

我想使用 C# 处理十进制货币 (EUR) 的价格,而不用担心舍入错误,也不想用我自己的方式进行十进制算术。 The C# reference for the decimal type

Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

什么?

我不在乎精度,我只想要大约七个信号图。但我想知道 5.31 EUR 是一个精确值。 A,二进制编码的十进制类型将是理想的。所以我的问题是 C# decimal 是否是那种类型。

最佳答案

不,这不是BCD (其中每个数字都单独编码为特定位数)- 但您不希望它是。 (而且我当然从未声称它是。)

decimal 是一种浮点类型,因为它有一个有效数字和一个指数,都是整数 - 只是与 floatdouble 不同,被指数移动的“点”是小数点而不是二进制小数点。不幸的是,MSDN 说的是“与浮点类型相比”,而实际上它的意思是“与二进制浮点类型相比”。

decimal documentation确实确实相当清楚:

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. Therefore, the binary representation of a Decimal value the form, ((-296 to 296) / 10(0 to 28)), where -(296-1) is equal to MinValue, and 296-1 is equal to MaxValue. For more information about the binary representation of Decimal values and an example, see the Decimal(Int32[]) constructor and the GetBits method.

关于c# - C#中的 "decimal"类型是二进制编码的十进制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23090614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com