zizi21
asked on
Java: Output 10%3...
Hi,
I have 2 questions like this:
System.out.println(10.0/3. 0+5.0*2.0) where the answer is 13.33333......4
My question is how long would 33333333 be and why? coz i printed out and it was pretty long without truncating..
My second question is that :
System.out.println(10%3+5% 2) where the answer printed was 2.0
I thought it should be 2 instead of 2.0 as both are integers and there is no floats or doubles that allowed it to be promoted.
Pls clarify. thanks
I have 2 questions like this:
System.out.println(10.0/3.
My question is how long would 33333333 be and why? coz i printed out and it was pretty long without truncating..
My second question is that :
System.out.println(10%3+5%
I thought it should be 2 instead of 2.0 as both are integers and there is no floats or doubles that allowed it to be promoted.
Pls clarify. thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
could she be wrong ?
>>could she be wrong ?
Could be a misprint. Any of those numbers written as a decimal would produce 2.0 for instance
Could be a misprint. Any of those numbers written as a decimal would produce 2.0 for instance
Well, if you execute this testing code you'll see by yourself that System.out.println(10 % 3 + 5 % 2); prints 2, while System.out.println(10.0 % 3.0 + 5.0 % 2.0); prints 2.0. That's because the first numbers are integers, so the result is an integer, while the second option has double, so the result is a double
package test;
public class Test {
public static void main(String[] args) {
System.out.println(10 % 3 + 5 % 2);
System.out.println(10.0 % 3.0 + 5.0 % 2.0);
}
}
package test;
public class Test {
public static void main(String[] args) {
System.out.println(10 % 3 + 5 % 2);
System.out.println(10.0 % 3.0 + 5.0 % 2.0);
}
}
ASKER
thank you..btw, what is the precision for double ?
:-)
ASKER
System.out.println(10 % 3 + 5 % 2); ................. 2.0