This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java (Precision Problem) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 14672
    
  11

You'll find interesting things in the BigDecimal class.


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

Joined: Aug 16, 2005
Posts: 12924
    
    3

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Java (Precision Problem)