11.1.6 The decimal type

本文介绍了decimal数据类型,它是128位数据类型,适用于金融和货币计算,能表示特定范围的值,不支持有符号零、无穷大或NaN。与float和double相比,它精度更高、范围更小,运算有特定规则,浮点类型与decimal间无隐式转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

11.1.6 The decimal type
The decimal type is a 128-bit data type suitable for financial and monetary
calculations. The decimal type
can represent values ranging from 1.0 × 10.28 to approximately 7.9 × 1028
with 28.29 significant digits.
The finite set of values of type decimal are of the form (.1)s × c ×
10-e, where the sign s is 0 or 1, the
coefficient c is given by 0 ≤ c < 296, and the scale e is such that 0 ≤ e
≤ 28. The decimal type does not
support signed zeros, infinities, or NaN’s.
A decimal is represented as a 96-bit integer scaled by a power of ten. For
decimals with an absolute value
less than 1.0m, the value is exact to the 28th decimal place, but no
further. For decimals with an absolute
value greater than or equal to 1.0m, the value is exact to 28 or 29 digits.
Contrary to the float and double
data types, decimal fractional numbers such as 0.1 can be represented
exactly in the decimal
representation. In the float and double representations, such numbers are
often infinite fractions, making
those representations more prone to round-off errors.
If one of the operands of a binary operator is of type decimal, then the
other operand must be of an integral
type or of type decimal. If an integral type operand is present, it is
converted to decimal before the
operation is performed.
The result of an operation on values of type decimal is that which would
result from calculating an exact
result (preserving scale, as defined for each operator) and then rounding
to fit the representation. Results are
rounded to the nearest representable value, and, when a result is equally
close to two representable values, to
the value that has an even number in the least significant digit position
(this is known as .banker.s
rounding.). That is, results are exact to 28 or 29 digits, but to no more
than 28 decimal places. A zero result
always has a sign of 0 and a scale of 0.
If a decimal arithmetic operation produces a value that is too small for
the decimal format after rounding, the
result of the operation becomes zero. If a decimal arithmetic operation
produces a result that is too large
for the decimal format, a System.OverflowException is thrown.
The decimal type has greater precision but smaller range than the
floating-point types. Thus, conversions
from the floating-point types to decimal might produce overflow exceptions,
and conversions from
decimal to the floating-point types might cause loss of precision. For
these reasons, no implicit conversions
exist between the floating-point types and decimal, and without explicit
casts, it is not possible to mix
floating-point and decimal operands in the same expression.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值