aspose file tools*
The moose likes Beginning Java and the fly likes Only 2 decimal places!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Only 2 decimal places!!!!" Watch "Only 2 decimal places!!!!" New topic
Author

Only 2 decimal places!!!!

joe kane
Greenhorn

Joined: Jan 26, 2004
Posts: 29
I was wondering how do you, when returning a value that can have loads of decimal places how would you stop it at only two decimal places..
Thanks in advance joe kane
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
See the javadocs for this class
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I wrote an article about floating point numbers that shows some example of formatting decimals:
http://www.javaranch.com/newsletter/July2003/newsletterjuly2003.jsp#a4


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
joe kane
Greenhorn

Joined: Jan 26, 2004
Posts: 29
Thanks for the help, but im very new to java and im not too sure where the code goes for the decimal format...?

Here is the program I have, it works 100%, it just needs the decimal format on 3 numbers,
Thanks loads in advance if you can help.....
Thanks joe Kane
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Here is a simple program that shows how to format a float into a String with two deciamls:

import java.text.*;
public class JunkTest {

public static void main(String [] args) {
float f = 45.67f * 43.15f;
System.out.println(new DecimalFormat("0.00").format(f));
}
}

Notice that we need to import java.text as that is the package that the DecimalFormat class is in.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Only 2 decimal places!!!!
 
Similar Threads
rounding
double precision
double
String to Long
How to find number of decimal places?