File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes NumberFormat/DecimalFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NumberFormat/DecimalFormat" Watch "NumberFormat/DecimalFormat" New topic
Author

NumberFormat/DecimalFormat

Tobi McFarland
Greenhorn

Joined: Nov 26, 2001
Posts: 10
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


Tobi<p>Aspiring Java Jedi
Andrew Sweeney
Greenhorn

Joined: Jan 11, 2002
Posts: 26
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

Joined: Nov 26, 2001
Posts: 10
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

Joined: Nov 26, 2001
Posts: 10
I tried making the change to the String, and it compiled, but I got a : "Malformed pattern "####.#0" error.
Oy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NumberFormat/DecimalFormat