wood burning stoves 2.0*
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
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: 14074
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java (Precision Problem)