PDA

View Full Version : DecimalFormat


UrbanTwitch
01-27-2010, 01:53 AM
I am stuck on this part of my assignment. The DecimalFormat is hard to understand.

My book says...

"Write an application that accepts a double number and then displays it in five different ways, using a combination of dollar signs, thousand seperators, decimal places, and other formats. Use the Java API to look up DecimalFormat and discover some formatting patterns that were not covered in this chapter. Use at least two patterns from the API."

Now here is what I have so far, to show you my progress:

import java.io.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class formatDan {

public static boolean done;

public static void main(String args[]) throws IOException
{



BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream);


while (!done) {

System.out.println("\n\nPlease type decimal number in:");
String strNum = bufRead.readLine();
try {
double intNumOne = Double.parseDouble(strNum);

NumberFormat formatter = new DecimalFormat("#0.000");
System.out.println("\n\nThe Decimal Value is:"+formatter.format(intNumOne));



}catch (NumberFormatException e)
{
System.out.println("\nWrong number syntax.");
}

}

} //public statc
} // public class

Can you lead me in the right direction? I tried reading my book but then again I'm not efficient in English.. maybe you can help me? :)

Thanks.

cs_student
01-27-2010, 02:02 AM
I tried reading my book but then again I'm not efficient in English..
Is English not your primary language?
Being able to read documentation is vital.


You should check out the DecimalFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html) class. The java api documentation provides a very detailed explanation of how it works. It even provides an example of a couple of ways you can use it.


Also, next time you post please be sure that your code is formated according to the Java Standard Code Convention (http://java.sun.com/docs/codeconv/) so we can read it with a fair amount of ease.

In your current code, when you pass the "#0.000", if you read the documentation I linked you to, you can easily find out what it means. The '#' signifies that the tens place will only be displayed if the numeral is not 0. The "0.000" shows that the number will have four significant figures which will be displayed regardless of rather they are 0 or not. (ie it will display 1.000 not just 1)

UrbanTwitch
01-27-2010, 02:21 AM
I'm sorry, Spanish is. I'll try it out, thanks.