• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java (Precision Problem)

 
Pulkit Malhotra
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I'm having a float variable which displays value as 12.3333333333 i want it to show only upto 2 decimal places like 12.33.

Can you please tell me is there any direct function for this in math or util package ?

Thanks in advance.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll find interesting things in the BigDecimal class.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to format a number to show only 2 decimal places. You could do that like this:

Or you could use java.text.DecimalFormat:

[ May 25, 2007: Message edited by: Jesper Young ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic