• Post Reply Bookmark Topic Watch Topic
  • New Topic

double value round off  RSS feed

 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I wish to round off say 2.342344 to 2.34. Is this posible

Thank you advance
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Lohi wrote:Hi all,

I wish to round off say 2.342344 to 2.34. Is this posible

Thank you advance


A double doesn't have a concept of format, so you can't format the double. However, you can convert it to a string and format it -- using the DecimalFormat class.

Henry
 
Gangadhararao Bommasani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code

 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using the DecimalFormat class.

Also, Check out setRoundingMode method of the same class.
This method will help you to round of double value as you require (different from truncate). You will find this method, if you are using JDK 1.6.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find String#format(java.lang.String,%20java.lang.Object...) a useful alternative to DecimalFormat.
 
S Shehab
Ranch Hand
Posts: 492
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Lohi wrote:Hi all,

I wish to round off say 2.342344 to 2.34. Is this posible

Thank you advance

Sure it's possible ..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!