Split decimal variable into integral and fraction parts

invarbrass picture invarbrass · May 22, 2012 · Viewed 29k times · Source

I am trying to extract the integral and fractional parts from a decimal value (both parts should be integers):

decimal decimalValue = 12.34m;
int integral = (int) decimal.Truncate(decimalValue);
int fraction = (int) ((decimalValue - decimal.Truncate(decimalValue)) * 100);

(for my purpose, decimal variables will contain up to 2 decimal places)

Are there any better ways to achieve this?

Answer

Girish Sakhare picture Girish Sakhare · Apr 30, 2015
       decimal fraction = (decimal)2.78;
        int iPart = (int)fraction;
        decimal dPart = fraction % 1.0m;