Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > clarification regarding DecimalFormat formatting..

Reply
Thread Tools

clarification regarding DecimalFormat formatting..

 
 
Rakesh
Guest
Posts: n/a
 
      07-07-2008
For a particular application - I need to format decimals with strictly
6 digits , after the decimal separator (even if they are not
significant ).

More specifically I would like the following test case to succeed.

public void testZeroFormatting() {
DecimalFormat myformat = new DecimalFormat("#############.######");
assertEquals("Zero ", "0.000000", myformat.format(0.0f));
}


For now , the function returns 0, instead of 0.000000 (as I want it
to). Any idea what I am missing here.

 
Reply With Quote
 
 
 
 
Martin Gregorie
Guest
Posts: n/a
 
      07-07-2008
On Mon, 07 Jul 2008 14:26:32 -0700, Rakesh wrote:

> For now , the function returns 0, instead of 0.000000 (as I want it to).
> Any idea what I am missing here.


DecimalFormat myformat = new DecimalFormat("############0.000000");

....as it says quite clearly in the DecimalFormat documentation.

If for some reason you don't have the JDK documentation installed,
you need to rectify that omission ASAP.


--
martin@ | Martin Gregorie
gregorie. |
org | Zappa fan & glider pilot


 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-07-2008
Rakesh <(E-Mail Removed)> writes:
>I need to format decimals with strictly 6 digits , after
>the decimal separator (even if they are not significant ).


public class Main
{
public static void main( final java.lang.String[] args )
{
for( double x = 0; x < 10; ++x )
{
java.lang.System.out.printf( "%.6f%n", java.lang.Math.pow( 263.1, x ));
java.lang.System.out.printf( "%.6f%n", java.lang.Math.pow( 263.1, -x ));
}}}

1,000000
1,000000
263,100000
0,003801
69221,610000
0,000014
18212205,591000
0,000000
4791631290,992102
0,000000
1260678192660,022000
0,000000
331684432488851,800000
0,000000
87266174187816928,000000
0,000000
22959730428814635000,000000
0,000000
6040705075821131000000,000000
0,000000

 
Reply With Quote
 
GArlington
Guest
Posts: n/a
 
      07-08-2008
On Jul 7, 10:26 pm, Rakesh <(E-Mail Removed)> wrote:
> For a particular application - I need to format decimals with strictly
> 6 digits , after the decimal separator (even if they are not
> significant ).
>
> More specifically I would like the following test case to succeed.
>
> public void testZeroFormatting() {
> DecimalFormat myformat = new DecimalFormat("#############.######");
> assertEquals("Zero ", "0.000000", myformat.format(0.0f));
> }
>
> For now , the function returns 0, instead of 0.000000 (as I want it
> to). Any idea what I am missing here.


You need to RTFM.
http://java.sun.com/j2se/1.4.2/docs/...malFormat.html
(Special Pattern Characters)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting decimal sign ("+" AND "-") in DecimalFormat ? Mark Sizzler Java 1 02-21-2005 08:01 PM
How to suppress final 0 with DecimalFormat? Adam Lipscombe Java 2 11-29-2004 08:51 PM
Weird Problem w/ DecimalFormat Cliff Java 5 05-12-2004 02:59 AM
Padding for DecimalFormat? Timo Nentwig Java 3 02-12-2004 05:06 AM
DecimalFormat Johnny Java 10 10-27-2003 05:29 AM



Advertisments