# Check if the result is a double or an int

Page 1 of 1

## 12 Replies - 752 Views - Last Post: 28 March 2011 - 01:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=224646&amp;s=5d163213681133da4a185c0db857de47&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 IamTw_

• D.I.C Head

Reputation: 32
• Posts: 238
• Joined: 25-February 11

# Check if the result is a double or an int

Posted 27 March 2011 - 04:40 PM

Hey

How do you check if the number you got is an int or a double?

I have done some researching, and found out that you can use Math.floor(double a), and Math.ceil(...).
But these methods aren't useful in my situation, or maybe I just don't use them right.

Let's say that I make this computation:

int result = 42/4 //now clearly this result is a double value, so the program should not take it into account. It should only be taken into account if the result is an int. Howw would you do that?

So, something like:
Forgot the while true and other mistakes, I'll fix those, only help needed is for the problem I'm asking

```public int tryDevide(int number){
int res = 0;

while(true){
for(int i = 3000; i > 1; i--){
for(int j = 1; j < 10; j++){
if(number / j == an int){
res = number;
} else{
number is a double, and is useless;

```

This post has been edited by IamTw_: 27 March 2011 - 04:41 PM

Is This A Good Question/Topic? 0

## Replies To: Check if the result is a double or an int

### #2 Manbearpig101

• D.I.C Head

Reputation: 15
• Posts: 62
• Joined: 17-June 10

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 04:51 PM

If you have a double, you can check if it has anything past the decimal if you do:

```double d = 12.24;
if ((d - ((int) d)) != 0) {
//It is a double
} else {
//it is doesn't contain anything past decimal
}

```

Was This Post Helpful? 0

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 06:54 PM

IamTw_, on 27 March 2011 - 06:40 PM, said:

How do you check if the number you got is an int or a double?

Is that a serious question ?
If it is the case you seriously miss the concept here
It simply depends in which type (int or double) of variable you store the result in

Manbearpig101, on 27 March 2011 - 06:51 PM, said:

If you have a double, you can check if it has anything past the decimal if you do:

```double d = 12.24;
if ((d - ((int) d)) != 0) {
//It is a double
} else {
//it is doesn't contain anything past decimal
}

```

and 12.00 is not a valid double
Hope I'll never live near a nuclear electricity plan where you work for
It is a code writting time that you should decide if the result should be considered as double or int not at run time
Was This Post Helpful? 0

### #4 Manbearpig101

• D.I.C Head

Reputation: 15
• Posts: 62
• Joined: 17-June 10

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 07:06 PM

I'm sure he means if the number given (of type double) contains numbers that aren't 0s passed the decimal.
Was This Post Helpful? 0

### #5 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 07:09 PM

IamTw_, on 27 March 2011 - 06:40 PM, said:

How do you check if the number you got is an int or a double?

You are asking the wrong question. Your code makes it clear that you simply want to know if number is evenly divisible by j.

To determine that, use the modulus operator & see if the remainder is 0:
```        if (number % j == 0) {
// result is an integer
}
else {
// result is fractional
}

```

Was This Post Helpful? 2

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 07:10 PM

Manbearpig101, on 27 March 2011 - 09:06 PM, said:

I'm sure he means if the number given (of type double) contains numbers that aren't 0s passed the decimal.

Still a really bad design issue
Is the result expected double or int ?
Was This Post Helpful? 0

### #7 Manbearpig101

• D.I.C Head

Reputation: 15
• Posts: 62
• Joined: 17-June 10

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 07:13 PM

I'm not sure, I just interpreted what I thought he was trying to ask, so I could be posting code that is completely useless to him.
Was This Post Helpful? 0

### #8 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 07:16 PM

I don't think it's really a design issue -- it's just a matter of using the wrong words. All of the OP's variables are int, and based on what was stated, an int is what is desired.

This post has been edited by r.stiltskin: 27 March 2011 - 07:18 PM

Was This Post Helpful? 2

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Check if the result is a double or an int

Posted 27 March 2011 - 07:18 PM

Manbearpig101, on 27 March 2011 - 09:13 PM, said:

I'm not sure, I just interpreted what I thought he was trying to ask, so I could be posting code that is completely useless to him.

I am afraid so, and even misleading... but this is OK no offense at all, you'll learn to
Was This Post Helpful? 0

### #10 IamTw_

• D.I.C Head

Reputation: 32
• Posts: 238
• Joined: 25-February 11

## Re: Check if the result is a double or an int

Posted 28 March 2011 - 11:29 AM

Here is another example.

I want to find out, if you devide a number, lets call that number X, with a series of numbers (call them Y) from 1 to 10 as an example. I want to find out if the result X / Y = int or double.

So to break it down, lets say X = 10, and the first number we look at for Y is 2. Lets have another variable called Z, to represent the result of X/Y

Clearly X/Y = 10/2 = 5 = Z. So Z here is an int and not double. Now take Y = 3 -> X/Y = 10/3 = 3.33333 = Z. Here Z is not an int, but a double, so Z is useless for me, since I'm only interested in Z = int, and not Z = double.

So somehow I need to check if X/Y == int, or X/Y == double.
How would you do that?

You cannot say, if(X % Y == 0), because Y is not necassarily an even number, Y goes from 1 to 10.
Was This Post Helpful? 0

### #11 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Check if the result is a double or an int

Posted 28 March 2011 - 12:26 PM

First of all, I already gave you the solution in Post #5. If you would try it you would see that.

Second, please use the correct terminology. You have to learn to distinguish mathematics terminology from programming terminology. They often sound similar, but they mean different things. int and double are programming terms, not mathematical terms.

If X is an int and Y is an int, X/Y is NEVER a double. That is "integer division" and the result is always an int.

If your program says
```int x = 10;
int y = 3;
int z = x/y;

```

the value of z will not be 3.3333. It will be 3. Try it & see.

On the other hand, if you write
```int x = 10;
int y = 2;
int z = x % y;

```
the value of z will be 0,
but in
```int x = 10;
int y = 3;
int z = x%y;

```
the value of z will be 1.

You should try things out before you pronounce them to be wrong.

double is a data type in the programming languages Java, C, C++ (and others). It is not a mathematical concept. What you are calling "double" is actually a rational number which is a mathematical concept and is the result of mathematically dividing one integer by another, NOT the result of integer-dividing one int by another in a Java program.

The number 3.3333 is not a "double". It's a decimal number that happens to have a fractional part (the .3333 part) and it can be represented in Java or C using the float or double data types.

This post has been edited by r.stiltskin: 28 March 2011 - 12:40 PM

Was This Post Helpful? 4

### #12 IamTw_

• D.I.C Head

Reputation: 32
• Posts: 238
• Joined: 25-February 11

## Re: Check if the result is a double or an int

Posted 28 March 2011 - 01:02 PM

r.stiltskin, on 28 March 2011 - 12:26 PM, said:

Second, please use the correct terminology.

Thank you for the help.
But do you think I used the wrong terminology on purpose?
Was This Post Helpful? 0

### #13 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Check if the result is a double or an int

Posted 28 March 2011 - 01:08 PM

IamTw_, on 28 March 2011 - 03:02 PM, said:

r.stiltskin, on 28 March 2011 - 12:26 PM, said:

Second, please use the correct terminology.

Thank you for the help.
But do you think I used the wrong terminology on purpose?

No, I just thought that was more polite than saying:
"Use the correct terminology, dammit!"
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }