1 Replies - 423 Views - Last Post: 26 January 2010 - 07:14 PM Rate Topic: -----

#1 UrbanTwitch  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 233
  • Joined: 27-September 09

DecimalFormat

Posted 26 January 2010 - 06:55 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: DecimalFormat

#2 cmh0114  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 143
  • Joined: 03-January 10

Re: DecimalFormat

Posted 26 January 2010 - 07:14 PM

It looks to me like you have most of the code that you want, you're just looking for more ways to output it. When you use
NumberFormat formatter = new DecimalFormat("#0.000");

, you create a format to output the number in. There will be a digit in the tens place (if the number is equal to or larger than 10), and there will be four other digits.
The symbols (for example, you used '0' and '#'), are called flags, I believe. I'm not great with terminology, so if I'm wrong, someone posting after me will correct me. You can also output it using "00.###", which would output at least two integers before the decimal point, and possibly three afterwards, if the last is not zero. Try playing around with formatting like that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1