# How to use divide by in java

Page 1 of 1

## 6 Replies - 161354 Views - Last Post: 14 April 2009 - 08:21 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=99130&amp;s=3c015253fc4145863343fa72a42178d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ryan5554

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 10-April 09

# How to use divide by in java

Posted 13 April 2009 - 10:35 PM

I'm unsure how to use a divide by in java. I just want to do

800 divided by an int variable divided again by another int variable

Any ideas?

Thanks
Is This A Good Question/Topic? 0

## Replies To: How to use divide by in java

### #2 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,620
• Joined: 15-May 08

## Re: How to use divide by in java

Posted 13 April 2009 - 10:39 PM

simple

```int div_one = 2;
int div_two = 4;

int myEquals = (800 / div_one) / div_two;

// basically this means
// 800 divided by 2 which will = 400
// then 400 divided by 4 which will make myEquals = 100

```

Hope this helps
Was This Post Helpful? 0

### #3 virgul

• D.I.C Regular

Reputation: 44
• Posts: 269
• Joined: 18-March 09

## Re: How to use divide by in java

Posted 13 April 2009 - 10:41 PM

```double answer = 0;
int firstInt, secondInt;

//the *1.0 will make it so it gives you a double answer aka a decimal point, if you dont want this then change answer to an int
answer = 800 / firstInt / secondInt * 1.0;

System.out.println(answer);

```

Edit:
hmmm i seem to have been beaten by a few seconds... damn

This post has been edited by virgul: 13 April 2009 - 10:42 PM

Was This Post Helpful? 0

### #4 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: How to use divide by in java

Posted 14 April 2009 - 01:07 AM

Virgul, that's not quite right. Take a look at this:

```		System.out.println(3 / 2);       // Prints 1
System.out.println(3 / 2 / 1.0); // Prints 1.0

```

Java goes from left to right so first it does 3/2. They're both ints so it does integer devision resulting in 1. Then it does 1/1.0. It has to convert the first to a double so we get 1.0/1.0 = 1.0.

To get a fractional result ryan5554 would have to do:

```answer = 800.0 / firstInt / secondInt;
```

Was This Post Helpful? 0

### #5 virgul

• D.I.C Regular

Reputation: 44
• Posts: 269
• Joined: 18-March 09

## Re: How to use divide by in java

Posted 14 April 2009 - 10:59 AM

wow, ok that was a stupid error, thanks for the tip.

By the way, what if i were to do it this way

```double answer = 0;
int firstInt, secondInt;

//the *1.0 will make it so it gives you a double answer aka a decimal point, if you dont want this then change answer to an int
answer = 800 * 1.0 / firstInt / secondInt;

System.out.println(answer);

```

say that 800 is an unknown value so would multiplying by 1.0 first work?
Was This Post Helpful? 0

### #6 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: How to use divide by in java

Posted 14 April 2009 - 02:25 PM

Sure, that should be fine but don't take my word for it. Fire up the compiler and check it out.

However, in this case you're dealing with a literal. No reason not to go 800.0. If it was a variable I'd probably cast it for clarity.

answer = intVar * 1.0 / firstInt / secondInt;
vs
answer = (double)intVar / firstInt / secondInt;

Also, while I'm talking about clarity, it's best to use brackets for clarity, as in bbq's example. No offence intended to virgul but this shows how easy it is for even an experienced programmer to get operations orders and the like mixed up.

int myEquals = (800.0 / div_one) / div_two;
Was This Post Helpful? 0

### #7 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,620
• Joined: 15-May 08

## Re: How to use divide by in java

Posted 14 April 2009 - 08:21 PM

Yea cfoley is quite on the money, it may be quicker now not to utilise brackets, however if you have to return to an expression at a later time, it is far easier to understand if the programmer has clearly outlined what is actually happening, rather than you having to waste time decyphering some complex expression.... Take the time now and make it easier for others and yourself later down the track
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; }