10

Possible Duplicate:
How to determine if a decimal/double is an integer?

I have a unique situation in which all numbers must be saved as double data type in my database, but only in certain conditions is the precision beyond the integer level valuable.

At first, I tried to just use int and then abstract to a new table when these unique fractional occurances would happen, but after doing that for weeks I can see now that it is excessively stupid and wasting my time.

I know that I can turn a double into an int. That is easy. I know how to translate it over. What I do not know is how to TEST for translating it over. I basically wish to come up with a short, easy way to say

Is this number really a double, or is it just an int?

If it is an int (and most of the time, it will be), then I will turn it into one and treat it as such. But due to the uniqueness of the requirements, I have to still save everything in the database as double.

Any ideas? I know this is a newbie question, and I've googled around for several hours and am still left quite confused.

Community
  • 1
  • 1
Derek
  • 769
  • 1
  • 7
  • 14
  • So wouldn't you just test if the incoming value is larger than the maximum integer value? Or are you more concerned if the value has a fractional part to it? – Yuck Aug 12 '11 at 13:37
  • So is a price of $1.00 an `int` or `double` in this world? *Trick question: it's a `decimal`.* – Anthony Pegram Aug 12 '11 at 13:37
  • 1
    What do you mean 'without any loss'? Are you saying that 10.000 can be turned into 10 without loss, but 10.001 cannot? In which case you need to define how big a loss is acceptable, as you cannot say 0. You need to say +/- 0.001 or whatever. – iandotkelly Aug 12 '11 at 13:38
  • As additional commentary, I'm not sure I like this approach, treating something as an integer because it might be a round number. That should not drive your logic. If something *should* be an integer, *then* treat it like one. If something happens to be a round number, but it really doesn't need to be an integer, treat it like the type it *should* be. Normally, the type should be consistent for all values in a given column. And you should make this determination at design time, not runtime. – Anthony Pegram Aug 12 '11 at 13:41
  • @Anthony: Yes, this would be ideal, but this is not possible in my situation. – Derek Aug 12 '11 at 14:12
  • @iandotkelly: Yes, that is essentially what I am saying. `10.000` is the same as `10`. – Derek Aug 12 '11 at 14:12

4 Answers4

10

Cast it to an int and see if it's still equal:

if ((int)val == val)

(int)val will truncate any fractional portion.

Note that this may behave unexpectedly if the number is too large to retain complete precision in the double.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
9
double d = ...;

if(d == (int)d)
   //is int
else
  //is not int

Of course due to some precision issues, this may not work 100% times. You can use

double d = ...;

if(Math.Abs(d - (int)d) < Epsilon) //const double Epsilon = 0.000001;
   //is int
else
  //is not int
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
6

This similar post shows you how to determine if a double or decimal has decimal places or not. You can use this to determine what type it is and then store appropriately.

Accepted answer from that post:

For floating point numbers, n % 1 == 0 is typically the way to check if there is 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
Community
  • 1
  • 1
Chris Snowden
  • 4,982
  • 1
  • 25
  • 34
0

try this

double x = ......

if (Math.truncate(x) == x)

....... (is integer, unless its so big its outside the bounds)
Dean Chalk
  • 20,076
  • 6
  • 59
  • 90