abc cba
abc cba

Reputation: 2633

How To Check a double value whether is Int or double

Is That Any calculation or method allow me to check whether a double value is Int or Double in c# code

Example

Double NumberOne = 55.00 // Return False 
Double NumberTwo = 55.10 // Return True

Upvotes: 2

Views: 336

Answers (4)

Mitch
Mitch

Reputation: 526

    private bool IsDoubleNotAnInt(double num)
    {
        if ((num % 1) == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

Upvotes: 3

Rotem
Rotem

Reputation: 21947

You could check

n % 1 == 0

to determine this.

Upvotes: 2

Bartosz
Bartosz

Reputation: 3358

You could compare it with value without fractional part:

Math.Floor(n) != n

Upvotes: 1

Bart Friederichs
Bart Friederichs

Reputation: 33553

Use Math.Floor

 if (Math.Floor(number) == number) {
     // yay, an "int"
 }

Upvotes: 4

Related Questions