Coder Perfect

How do you know if a decimal or double number is an integer?

Problem

What is the best way to 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

I’d like to know this so that I can select whether I want to output the value utilizing programmatically. or ToString(“N0”) ToString(“N2”). I don’t want to show a decimal point value if there isn’t one.

Asked by Jim Geurts

Solution #1

If you’re working with floating point numbers, n percent 1 == 0 is a good technique to see if there’s anything past the decimal point.

public static void Main (string[] args)
{
    decimal d = 3.1M;
    Console.WriteLine((d % 1) == 0);
    d = 3.0M;
    Console.WriteLine((d % 1) == 0);
}

Output:

False
True

Update: As @Adrian Lopez said below, comparing a tiny number epsilon to a large value epsilon will eliminate floating-point computation errors. Because this is a query regarding double values, here’s a more floating-point calculation proof answer:

Math.Abs(d % 1) <= (Double.Epsilon * 100)

Answered by Mark Rushakoff

Solution #2

This can be accomplished in a variety of ways. Consider the following scenario:

double d = 5.0;
bool isInt = d == (int)d;

Modulo is another option.

double d = 5.0;
bool isInt = d % 1 == 0;

Answered by Erik Funkenbusch

Solution #3

How about this?

public static bool IsInteger(double number) {
    return number == Math.Truncate(number);
}

The decimal equivalent is the same code.

Actually, Mark Byers made a valid point: this could not be what you truly desire. If you’re only interested in whether a number is an integer when it’s rounded to the nearest two decimal places, you may do this instead:

public static bool IsNearlyInteger(double number) {
    return Math.Round(number, 2) == Math.Round(number);
}

Answered by Dan Tao

Solution #4

While the recommended techniques appear to work in simple cases, doing so in general is not a good idea. If a number isn’t exactly an integer, but it’s close enough to be formatted as an integer, you’ll get 1.000000. This can happen if you do a calculation that, in theory, should return exactly 1, but in fact, due to rounding mistakes, returns a value that is very close to but not exactly equal to one.

Instead, format it first, then strip the zeros if your string finishes with a period. There are also certain formats that will automatically trim trailing zeros. It’s possible that this will suffice for your needs.

double d = 1.0002;
Console.WriteLine(d.ToString("0.##"));
d = 1.02;
Console.WriteLine(d.ToString("0.##"));

Output:

1
1.02

Answered by Mark Byers

Solution #5

bool IsInteger(double num) {
    if (ceil(num) == num && floor(num) == num)
        return true;
    else
        return false;
}

Problemo solvo.

Mark Rushakoff pwned the game.

Answered by Puppy

Post is based on https://stackoverflow.com/questions/2751593/how-to-determine-if-a-decimal-double-is-an-integer