This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to round up double value in java?  RSS feed

 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Please, help me to solve this problem.

What is the method to round 1.5 to 2 or 2.5 to 3 in java?
I tried methods of Math class - nothing helps!

Thanks!
 
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably missed the ceil method, because that certainly does what you want. You need to cast the results to int/long though.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried Math.round(<double value>) and it seems to work. Have you tried it?
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replies!

I haven't provided the code. I want to round up result of division but not just double value.
Try this:
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression 3/2 is evaluated as integer 3 divided by integer 2 which results in integer 1. When assigned to a double this is promoted to 1.0 and NOT 1.5 as you seem to expect.
Try

The problem you are having is nothing to do with rounding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!