How do I tell if a decimal or double value is an integer?
For example:
decimal d = 5.0; // Would be true
decimal f = 5.5; // Would be false
or
double d = 5.0; // Would be true
double f = 5.5; // Would be false
The reason I would like to know this is so that I can determine programmatically if I want to output the value using .ToString("N0") or .ToString("N2"). If there is no decimal point value, then I don’t want to show that.
For floating point numbers,
n % 1 == 0is typically the way to check if there is anything past the decimal point.Output:
Update: As @Adrian Lopez mentioned below, comparison with a small value
epsilonwill discard floating-point computation mis-calculations. Since the question is aboutdoublevalues, below will be a more floating-point calculation proof answer: