Check if decimal contains decimal places by looking at the bytes

12,314

Solution 1

If you are really talking about decimal numbers (as opposed to floating-point numbers), then Decimal.GetBits will let you look at the individual bits of a decimal. The MSDN page also contains a description of the meaning of the bits.

On the other hand, if you just want to check whether a number has a fractional part or not, doing a simple

var hasFractionalPart = (myValue - Math.Round(myValue) != 0)

is much easier than decoding the binary structure. This should work for decimals as well as classic floating-point data types such as float or double. In the latter case, due to floating-point rounding error, it might make sense to check for Math.Abs(myValue - Math.Round(myValue)) < someThreshold instead of comparing to 0.

Solution 2

If you want a reasonably efficient way of getting the 'decimal' value of a decimal type you can just mod it by one.

decimal number = 4.75M;
decimal fractionalPart = number % 1; 
Console.WriteLine(fractionalPart); //will print 0.75

While it may not be the theoretically optimal solution, it'll be quite fast, and almost certainly fast enough for your purposes (far better than string manipulation and parsing, which is a common naive approach).

Solution 3

You can use Decimal.GetBits in order to retrieve the bits from a decimal structure.

The MSDN page linked above details how they are laid out in memory:

The binary representation of a Decimal number consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the integer number 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.

The return value is a four-element array of 32-bit signed integers.

The first, second, and third elements of the returned array contain the low, middle, and high 32 bits of the 96-bit integer number.

The fourth element of the returned array contains the scale factor and sign. It consists of the following parts:

Bits 0 to 15, the lower word, are unused and must be zero.

Bits 16 to 23 must contain an exponent between 0 and 28, which indicates the power of 10 to divide the integer number.

Bits 24 to 30 are unused and must be zero.

Bit 31 contains the sign; 0 meaning positive, and 1 meaning negative.

Solution 4

Going with Oded's detailed info to use GetBits, I came up with this

const int EXP_MASK = 0x00FF0000;
bool hasDecimal = (Decimal.GetBits(value)[3] & EXP_MASK) != 0x0;
Share:
12,314
Tono Nam
Author by

Tono Nam

Updated on July 23, 2022

Comments

  • Tono Nam
    Tono Nam almost 2 years

    There is a similar question in here. Sometimes that solution gives exceptions because the numbers might be to large.

    I think that if there is a way of looking at the bytes of a decimal number it will be more efficient. For example a decimal number has to be represented by some n number of bytes. For example an Int32 is represented by 32 bits and all the numbers that start with the bit of 1 are negative. Maybe there is some kind of similar relationship with decimal numbers. How could you look at the bytes of a decimal number? or the bytes of an integer number?

  • phoog
    phoog about 12 years
    myValue - Math.Truncate(myValue) might be slightly more efficient.