Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

How is the decimal type implemented?

Update

  • It's a 128-bit value type (16 bytes)
  • 1 sign bit
  • 96 bits (12 bytes) for the mantissa
  • 8 bits for the exponent
  • remaining bits (23 of them!) set to 0

Thanks! I'm gonna stick with using a 64-bit long with my own implied scale.

like image 983
Haywood Jablomey Avatar asked Jul 20 '10 20:07

Haywood Jablomey


People also ask

What is decimal integer in C?

A decimal integer literal contains any of the digits 0 through 9. The first digit cannot be 0. Integer literals beginning with the digit 0 are interpreted as an octal integer literal rather than as a decimal integer literal.

Which data type is used for decimal numbers?

Numbers that contain a decimal point are stored in a data type called REAL. Variables that use the real data type can accept both positive and negative numbers with a decimal place.

Can long data type have decimals?

Large Integers Long variables can hold numbers from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Operations with Long are slightly slower than with Integer . If you need even larger values, you can use the Decimal Data Type.


1 Answers

Decimal Floating Point article on Wikipedia with specific link to this article about System.Decimal.

A decimal is stored in 128 bits, even though only 102 are strictly necessary. It is convenient to consider the decimal as three 32-bit integers representing the mantissa, and then one integer representing the sign and exponent. The top bit of the last integer is the sign bit (in the normal way, with the bit being set (1) for negative numbers) and bits 16-23 (the low bits of the high 16-bit word) contain the exponent. The other bits must all be clear (0). This representation is the one given by decimal.GetBits(decimal) which returns an array of 4 ints.

like image 195
Jesse Dhillon Avatar answered Sep 21 '22 05:09

Jesse Dhillon