File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java (Precision Problem)" Watch "Java (Precision Problem)" New topic

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é

Joined: Nov 24, 2005
Posts: 14688

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: 15042

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 8 API documentation
I agree. Here's the link:
subject: Java (Precision Problem)
It's not a secret anymore!