• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

round()

 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use the round method in a println statement as follows:

...it compiles and runs.
I get a precision compile error when I try to define the integer value for the round method in a variable

....can someone explain why? I've reviewed the API and couldn't find explicit info on the overridden plus (+) operator.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
random() returns a double
round( double ) returns a long
You are trying to assign a long to an int.
Try

and it will work.

What overridden plus (+) operator are you referring to? The one used for String concatenation?
 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marilyn, thanks for the info. I was coding with the blinders on.
For the println statement, I guess the + operator doesn't convert the math return value until that value is computed. And at that point, it is converted to a string. If the println statement was to read:

...then it should multiple the rounded # by 10 before converting it to a string for Output1 and for Output2 it will convert the rounded value to a string and then append the value of 10 as a string to the end. The problem I was having on the println portion was realizing that the type of operator precedence makes a difference in how the overridden + operator treats values after a string. If I'm off base here...please correct me.
Thanks.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the println statement, I guess the + operator doesn't convert the math return value until that value is computed. And at that point, it is converted to a string.

If the println statement was to read:

<pre>class Abstest {
public static void main(String[] args) {
System.out.println("Output1: " + Math.round(Math.random())*(10));
System.out.println("Output2: " + Math.round(Math.random())+(10));
}
}</pre>

...then it should multiply the rounded # by 10 before converting it to a string for Output1 and for Output2 it will convert the rounded value to a string and then append the value of 10 as a string to the end.


All this is true.

The problem I was having on the println portion was realizing that the type of operator precedence makes a difference in how the overridden + operator treats values after a string. If I'm off base here...please correct me.


"the type of operator precedence"? Perhaps I'm just too tired, but I'm not following you.
Java evaluates each expression between the '+' signs before it calls toString() on any of them. Then it goes left to right.

"Output1: " + Math.round(Math.random())*(10)
String + number.toString()

"Output2: " + Math.round(Math.random())+(10)
String + number.toString() + number.toString()

is not the same as

String + (number + number).toString()

In the second case the numbers are added together first because the parens have higher precedence.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic