6 Replies - 59518 Views - Last Post: 14 April 2009 - 08:21 PM Rate Topic: -----

#1 ryan5554  Icon User is offline

  • New D.I.C Head

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

How to use divide by in java

Post icon  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  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • 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  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • 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  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1939
  • View blog
  • Posts: 4,027
  • 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  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • 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  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1939
  • View blog
  • Posts: 4,027
  • 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  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • 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