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
posted Feb 09, 2004 09:05:00
0
See the javadocs for this class
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted Feb 09, 2004 10:12:00
0
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
posted Feb 10, 2004 10:20:00
0
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.
subject: Only 2 decimal places!!!!