aspose file tools*
The moose likes Beginning Java and the fly likes Help Regarding Decimal Format Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help Regarding Decimal Format Class" Watch "Help Regarding Decimal Format Class" New topic
Author

Help Regarding Decimal Format Class

Himateja Madala
Greenhorn

Joined: Jun 15, 2006
Posts: 4
Hi,

I am using the Decimal Format class as follows...
I have a double value which i parse this way....
DecimalFormat d=DecimalFormat.getNumberInstance(Locale l)
d.format(double value);

However when i enter a number with ten digits it shows me the number in exponential format.But i dont want this to happen.The number should show up as is..

How can i prevent the exponential format.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

First, you have to understand that (1) a number and (2) the text representation of a number are two separate things.

A number itself does not have an intrinsic format. So your remark "The number should show up as is" is not valid, because the number does not have a format by itself.

You convert a number to a text representation of a number (so that the number can be displayed on screen) by formatting it, and you have to specify the format one way or another.

By using DecimalFormat.getNumberInstance(...) in your code, you are using the default format provided by the DecimalFormat class for the specified locale. If you want a different format, you should create the DecimalFormat object differently.

Look at the API documentation of class DecimalFormat. Instead of getting a DecimalFormat object with getNumberInstance(...), use one of the constructors that allows you to specify a format pattern.
[ June 16, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to the Ranch.

I agree with Jesper Young. But have you tried the formatted printing methods (Formatter.format(), String.format(), PrintStream.printf() [see System.out.printf()]) which are available in J2SE5.0, but not in earlier versions?

CR
Himateja Madala
Greenhorn

Joined: Jun 15, 2006
Posts: 4
Yes what you said is true...but if u r specifying a format means you are specifying the exponential format ... but i dont want scientific notation at all....The number has to be displayed the way it is entered..
For eg if i enter 123456789 it should nopt be shown as 1.234e8
Himateja Madala
Greenhorn

Joined: Jun 15, 2006
Posts: 4
Hi CR,
We have a constraint on the JDK version..we are using the 1.4.2.
Also the number is not for prinitng to the console.It is used in our custom TextField class
Regards
Himateja
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please don't use "text language;" there are people in the Ranch who didn't grow up speaking English and who might find "u" difficult to understand.

Both the formatted printing methods, and the NumberFormat class can be set up to print 123456789 rather than 1.23E+08. I prefer formatted printing; it is easier to use.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
If you use J2SE1.4, you will ahve to go through the API specification and find DecimalFormat, etc, or NumberFormat.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Originally posted by Himateja Madala:
Yes what you said is true...but if u r specifying a format means you are specifying the exponential format ...

If you don't want exponential format, then don't specify exponential format! You can specify any format you like.

When are you going to look at the API documentation, as Campbell and I suggested?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Regarding Decimal Format Class