• Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 21313
87
Chrome Eclipse IDE Java 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!