Detecting If a Number has a Decimal Fraction
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.