# DecimalFormat

Page 1 of 1

## 1 Replies - 602 Views - Last Post: 26 January 2010 - 07:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=152176&amp;s=9756ae43c4122199be30fca7eb842b11&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 UrbanTwitch

Reputation: 9
• 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
{

while (!done) {

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

Reputation: 10
• 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.