Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormat/DecimalFormat

 
Tobi McFarland
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello-
I have double that I'm trying to format as ####.##, that also adds a zero in the least significant digit position-if none is present-so that it is in a currency format.
here's the code snip:
Double amt = new Double(amount);
String pattern = "####.##";
DecimalFormat formatter = new DecimalFormat();
String output = formatter.format(amt);
when it prints out, it isn't rounding appropriately (still showing 1234.63223 for example) and it isn't putting in a zero at the end if the number is like 1234.3.
What am I missing?
Thanks-
Tobi
 
Andrew Sweeney
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully it is just a typo, but I notice that you aren't passing in the pattern String when you instantiate the Format object... Could that be it?
DecimalFormat formatter = new DecimalFormat( pattern );
Also, to add zeroes if no value is present, (like 1.2 showing as 1.20) use the "0" character in the pattern string, rather than the "#".

-Andrew
[ January 22, 2002: Message edited by: Andrew Sweeney ]
[ January 22, 2002: Message edited by: Andrew Sweeney ]
 
Tobi McFarland
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it to round up, so it's 1/2 way there. Forgot to pass in pattern (Duh).
it's still not adding a zero to the end of a number like this: 1111.1
Double amt = new Double(amount);
String pattern = "####.##";
DecimalFormat formatter = new DecimalFormat(pattern);
String output = formatter.format(amt);
 
Tobi McFarland
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried making the change to the String, and it compiled, but I got a : "Malformed pattern "####.#0" error.
Oy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic