# DecimalFormat

Page 1 of 1

## 2 Replies - 5413 Views - Last Post: 07 April 2007 - 01:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26077&amp;s=2cd6f345a8547e7dfa4395aafb09386d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 austinious

Reputation: 0
• Posts: 9
• Joined: 28-September 06

# DecimalFormat

Posted 07 April 2007 - 07:04 AM

```		public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat();
fmt.toPattern("##.##.##");

System.out.print("Enter a Time, no decimals");
int time = scan.nextInt();

System.out.println("Time: " + fmt.applyPattern(time));

}

```

one of my lame efforts to get the following to work.

I'm writing a program for the Track coach to sort runners by time, and then assign them to a specific lane on the track. Easy peasy...

Problem. They need to give me up to a 6 digit number, and would prefer to get the number back in the following format "##:##.##"
I had thought of having them enter the 2 digit groups separately, but that would entail 3 sorts. I thought that them entering the time as a 6 digit number would let me do 1 sort, then I could reformat the number. I'm trying to find a method in Java, but no luck so far. As you can see, I'm spending more time on this, instead of the actual real program. Of course this is a sticking point, because if I take the easy way out, and have them enter 3 groups of 2 digits, I have to do a sort, then up to 10 subsort, and then...even more....

Hey, I could always send the number to a string and break it up and concatinate to get the right symbols....wow, that would be almost as painful.

I hope someone has a bright idea, I'm feeling rather stupid...

austinious

Is This A Good Question/Topic? 0

## Replies To: DecimalFormat

### #2 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

## Re: DecimalFormat

Posted 07 April 2007 - 11:55 AM

a simple modification of: http://www.dreaminco.../snippet399.htm

DecimalFormat fmt = new DecimalFormat("00.00.00");
then to display it i believe you want the format option not applypattern:
fmt.format(time)
note that this method will likely insert leading zeros, if this is not wanted, use # instead.

### #3 austinious

Reputation: 0
• Posts: 9
• Joined: 28-September 06

## Re: DecimalFormat

Posted 07 April 2007 - 01:15 PM

I actually already tried this in ignorance and got...

Exception in thread "main" java.lang.IllegalArgumentException: Multiple decimal separators in pattern "00.00.00"
at java.text.DecimalFormat.applyPattern(DecimalFormat.java:2520)
at java.text.DecimalFormat.<init>(DecimalFormat.java:417)
at Test.main(Test.java:22)

Process completed.

Thanks anyway...