Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Formatting String as double values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Formatting String as double values" Watch "Formatting String as double values" New topic

Formatting String as double values

Chidananda Sulur

Joined: May 10, 2000
Posts: 14

This may be a simple one but need help. I want to convert a string to a double value and return the same without losing the formatting. The sample code is as enclosed.

public class TestDouble {
private static final String AMT = "100000000.376";
public static void main(String[] args) {
System.out.println("amount is "+AMT);
System.out.println("amount in double is "+Double.valueOf(AMT));

The output is
amount is 100000000.376
amount in double is 1.00000000376E8

I want the second value also to be displayed as 100000000.376 stored as a double without the exponential formatting. Please let me know any thoughts.


Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673

[ October 08, 2004: Message edited by: Nigel Browne ]
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

Could you use the BigDecimal class to display the correct format?
Like this:

Chidananda Sulur

Joined: May 10, 2000
Posts: 14
Thanks, The decimalformat api seems to resolve the issue.
Have you checked out Aspose?
subject: Formatting String as double values
It's not a secret anymore!