This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
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?
Joined: Jun 15, 2006
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
Joined: Jun 15, 2006
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
Joined: Oct 13, 2005
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.