Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# Double division stay 0.0

Ranch Hand
Posts: 94
1
Hi all,
This is my first post in the ranch and hope it's the right place to do it.

Here's the strange behavior I would ask for:

the output expected is 0.02
the real output is 0.0!
Why is that?

Sheriff
Posts: 12339
201
Numeric literals that do not have a decimal point are treated as int values thus the expression 2 / 100 divides two int values. Integer division will produce an integer value and in this case it will be 0. That is the value that gets assigned to your double result variable.

Junilu Lacar
Sheriff
Posts: 12339
201
Try this expression instead: 2.0 / 100.0

Daniele Barell
Ranch Hand
Posts: 94
1

Junilu Lacar wrote:Numeric literals that do not have a decimal point are treated as int values thus the expression 2 / 100 divides two int values. Integer division will produce an integer value and in this case it will be 0. That is the value that gets assigned to your double result variable.

Thank Junilu for the quick answer!
So the correct literals to use must be double as well:

And it actually gives the expected 0.02 result.

Thanks again!

dan

Junilu Lacar
Sheriff
Posts: 12339
201
Technically, only one of the literals needs to be a double. That is, 2 / 100.0 or 2.0 / 100 would also work. In these cases, the int value would get promoted to a double before the division operation is evaluated. Read up on value promotions for arithmetic operations in Java.

Ranch Hand
Posts: 67
2
• 2

Daniele Barell wrote:Hi all,
This is my first post in the ranch and hope it's the right place to do it.

Here's the strange behavior I would ask for:

the output expected is 0.02
the real output is 0.0!
Why is that?

Your code can rewrite like this:

division between 2 to 100 (2/100) equals to 0 because the result of that division is an integer.
After division, casting the result of division is performed (double(0))
And the result is : 0.0

Marshal
Posts: 6008
415
Duc Ta, OP,

It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.

Master Rancher
Posts: 3629
40
• 2

Liutauras Vilda wrote:
It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.

en_GB gives "0.0".

Liutauras Vilda
Marshal
Posts: 6008
415

Dave Tolls wrote:

Liutauras Vilda wrote:
It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.