This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Java (Precision Problem) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java (Precision Problem)" Watch "Java (Precision Problem)" New topic
Author

Java (Precision Problem)

Pulkit Malhotra
Ranch Hand

Joined: Apr 15, 2005
Posts: 30
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.


Regards,<br />Pul_Mal
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You'll find interesting things in the BigDecimal class.


[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java (Precision Problem)