Behind the scenes, what's happening with decimal value type in C#/.NET?

后端 未结 6 1380
囚心锁ツ
囚心锁ツ 2020-12-30 02:18

How is the decimal type implemented?

Update

  • It\'s a 128-bit value type (16 bytes)
  • 1 sign bit
  • 96 bits (1
6条回答
  •  半阙折子戏
    2020-12-30 02:34

    As described on MSDN's Decimal Structure page at http://msdn.microsoft.com/en-us/library/system.decimal(VS.80).aspx:

    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 is of the form, ((-296 to 296) / 10(0 to 28)), where -296-1 is equal to MinValue, and 296-1 is equal to MaxValue.

    The scaling factor also preserves any trailing zeroes in a Decimal number. Trailing zeroes do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeroes can be revealed by the ToString method if an appropriate format string is applied.

提交回复
热议问题