We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
Very simple Java help please

tincat
Posts: 935 Forumite
in Techie Stuff
Hi all,
I'm working through a Java tutorial, and the tutorial says that
double num = 5 / 3;
should equal 1.66
however, I've tried in both Eclipse and NetBeans, and I get the answer 1.0
Can anyone please shed some light? I've done a google search and can't see anything about it.
Thanks
I'm working through a Java tutorial, and the tutorial says that
double num = 5 / 3;
should equal 1.66
however, I've tried in both Eclipse and NetBeans, and I get the answer 1.0
Can anyone please shed some light? I've done a google search and can't see anything about it.
Thanks
0
Comments
-
I don't know anything much about Java, so this is just a stab in the dark... but... it sounds like the 1.67 result is being truncated or stored as an integer... I don't know if Java would generate an error like some languages or whether it would try to convert one data type to another without warning.
Also, I remember in some languages "/" means "divide by" whereas "\" means "divide by and discard any non-integer remainder"... Not sure if that applies to Java...0 -
Thanks for your reply, but \ produces an error so doesn't look like it's accepted as code.
I've tried searching for a solution, but can't come up with a good search term to show comparable searches.
It's annoying because it should work dammit0 -
How are you outputting the result to know that the value you are seeing is 1.0?0
-
public class HelloWorld {
public static void main(String[] args) {
double num1 = 5 / 3;
double num2 = 7;
double num3 = num1 + num2;
System.out.println(num1);
System.out.println(num2);
System.out.print("=");
System.out.println(num3);
System.out.println();
double i = 13;
double j = 12;
System.out.println("i is " + i);
System.out.println("j is " + j);
double k = i/j;
System.out.println("i/j is " + k);
k = i * j;
System.out.println("i * j is " + k);
}
}1.0
7.0
=9.0
i is 13.0
j is 12.0
i/j is 1.0833333333333333
i * j is 156.0
It's all compiled together in one bit of code, so I don't get how the first doesn't work and yet the second does,.
Thanks0 -
The above code gives the output as follows
Notice how the first calculation (num1) gives the answer of 1.0, yet when dealing with double k = i/j; I get the full answer.
It's all compiled together in one bit of code, so I don't get how the first doesn't work and yet the second does,.
Thanks
double num1 = 5 / 3
replace with
double five = 5
double three = 3
double num1 = five/three
4.8kWp 12x400W Longhi 9.6 kWh battery Giv-hy 5.0 Inverter, WSW facing Essex . Aint no sunshine ☀️ Octopus gas fixed dec 24 @ 5.74 tracker again+ Octopus Intelligent Flux leccy0 -
You might want to read up on how Java interprets literal numbers.
Numbers without a floating point are integer literals.
7 (int) / 9 (int) = int result, thus it gets truncated it doesn't matter that you are storing the final in a double, the calculation is made then stored (note this is for clarification the compiler in reality probably does something different but with the same results.)
however
double num1 = 5.0 / 3.0;
sop(num1); // prints 1.666666667
This is because these tokens with floating points are automatically considered doubles (a floating point literal)
You can explicitly say an integer (without a floating point) is a double like so
double num1 = 5d / 3d;
sop(num1); // prints 1.666666667
Your second example with I and j work because the compiler is operating on 2 doubles not 2 integers
I ran your code and I don't get 9 for num1+num2, I get 8 which is what it should be0 -
I ran your code and I don't get 9 for num1+num2, I get 8 which is what it should be
Sorry that's my fault. I pasted in the code as [num1 = 5/2] and got [2.0
7.0
=9.0] I changed the 2.0 to a 1.0 manually as when I first posted I said I'd put [num1 = 5/3]. I forgot to change the 9.0 to an 8.0.
0 -
debitcardmayhem wrote: »Ahhh not a java expert but
double num1 = 5 / 3
replace with
double five = 5
double three = 3
double num1 = five/three
Thanks, that works, although I'm still surprised that it doesn't work when putting it in normally. The tutorial said that using 'double' would allow decimal places.
However, at least I have a workaround.0 -
5 and 3 are both integers, not doubles. The division "5 / 3" is therefore an integer divide and returns an integer result - in this case 1 (the remainder is simply discarded). You then assign the integer value 1 to a double and get the value 1.0.
If you want divide to return a double, you need to convert one or both numbers to doubles before you do the divide.
Your workaround does this, though there are other ways.If it sticks, force it.
If it breaks, well it wasn't working right anyway.0
This discussion has been closed.
Confirm your email address to Create Threads and Reply

Categories
- All Categories
- 351.7K Banking & Borrowing
- 253.4K Reduce Debt & Boost Income
- 454K Spending & Discounts
- 244.7K Work, Benefits & Business
- 600.1K Mortgages, Homes & Bills
- 177.3K Life & Family
- 258.4K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.2K Discuss & Feedback
- 37.6K Read-Only Boards