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.
Hello. I am using a decimal formatter to format values to "0.00". The problem is, when I have a value that is 3.00, it shows up as 3. Same thing happens if it's 3.50, shows up as 3.5. What do I need to do to make it not drop the last zero? Thanks!
You need to set the maximum and/or minimum integer and fraction digits like this:
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joined: Feb 28, 2001
Thanks for the reply. I tried your suggestion and it did not work for what I am doing. I am placing a value into a table as a Double object. what I am doing is :
This did not change my value. When I created the object into the table as a String using the formatter, it worked. How can I format the string, and display it as a Double in the table with the trailing zero?? Thanks again!
A Double (or double) does not know or care how many digits you want to display. Once its value is set, it doesn't matter whether you created it using 3.5 or 3.50 - it's exactly the same as far as Double and double are concerned. The way to control how it's displayed is to format as (just before) you display it), e.g. System.out.println(df.format(rowData[x].doubleValue())); Or you could save the String representation of the number, rather than the Double or double version. But for most applications that's less convenient. Incidentally, your code can be streamlined in several ways: rowData[x] = new Double(df.format(getCostNorm()); or rowData[x] = new Double(getCostNorm()); The former is slightly different in that formatting and parsing the number has the effect of rounding off an info after the second digit - e.g. 3.504 becomes 3.5. But there's still no difference between 3.5 and 3.50, except when formatted as a String.