Detecting If a Number has a Decimal Fraction

emoticon

This seems like pretty basic functionality of a number type, but I can't seem to be able to find any  built-in function in any of .Net number type that support decimal (decimal, double) or the System.Math class to do this.

In any case, if you find yourself in need to find out if a number has a decimal fraction, here are a couple of ways to do it:

You can convert the number to string and if the string contains a decimal delimiter (usually it's a period if you are using English locale, but it might be comma in some locale), then you can be sure that it has a decimal fraction.

double number = 99.99f;
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("ID-id");
bool hasDecimalFraction = number.ToString(ci).Contains(ci.NumberFormat.NumberDecimalSeparator);
Console.WriteLine(hasDecimalFraction);  // should return true

 

Another way is to subtract the number by its floor and detect if what's left is greater than 0.

decimal number1 = 99.99m;
decimal number2 = 99m;
bool hasDecimalFraction = (number1 - Math.Floor(number1)) > 0;
Console.WriteLine(hasDecimalFraction);  // should return true
 
hasDecimalFraction = (number2 - Math.Floor(number2)) > 0;
Console.WriteLine(hasDecimalFraction);  // should return false

 

In any case, I really think this should be native to the number (decimal, double) / Math class.  Or is there such a function somewhere and I'm just not aware of it...  If you know otherwise, let me know.

Share this post: | | | |
Published Wednesday, May 28, 2008 3:58 PM by Jimmy Chandra
Filed under: ,

Comments

# re: Detecting If a Number has a Decimal Fraction

Decimal.parse ?

Wednesday, May 28, 2008 4:21 PM by cipto

# re: Detecting If a Number has a Decimal Fraction

Decimal.parse ... ? How?  You can convert a string to a decimal type, but I don't see any way to use this to detect if the decimal contain a decimal fraction.  I'm trying to find a function to detect if a number like 1033.39 has .39 in it.  Something like Decimal.HasDecimalFraction(1033.39) should return true or at least something like Decimal.GetDecimalFraction(1033.39) that will return .39.

Wednesday, May 28, 2008 5:01 PM by Jimmy Chandra

# re: Detecting If a Number has a Decimal Fraction

"System.Math.Truncate" will give you the integer part of your double.

I'm not aware about a built in function that gives the fractional part.

Anyway, you can simply do this:

double x = 3.1415;

bool hasFraction = x - System.Math.Truncate(x) > 0;

If you want, you can make Extension Methods into the double type to returns the Integer Part or Fractional Part of a double.

public static double IntegerPart(this double x)

{

   return System.Math.Truncate(x);

}

public static double FractionalPart(this double x)

{

  return x - System.Math.Truncate(x);

}

So, you can simply do this:

double x = 3.14.15;

bool hasFraction = x.FractionalPart > 0;

Wednesday, May 28, 2008 5:53 PM by norman

# re: Detecting If a Number has a Decimal Fraction

Can't you just mod 1 the number?

Sunday, June 22, 2008 7:43 PM by Duncan Abela

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: