views:

1875

answers:

2

Hello.

I'm using Java's DecimalFormat class to print out numbers in Scientific Notation. However, there is one problem that I have. I need the strings to be of fixed length regardless of the value, and the sign on the power of ten is throwing it off. Currently, this is what my format looks like:

DecimalFormat format = new DecimalFormat("0.0E0");

This gives me the following combinations: 1.0E1, 1.0E-1, -1.0E1, and -1.0E-1.

I can use setPositivePrefix to get: +1.0E1, +1.0E-1, -1.0E1, and -1.0E-1, or whatever I like, but it doesn't affect the sign of the power!

Is there any way to do this so that I can have fixed length strings? Thanks!

Edit: Ah, so there's no way to do it using Java's existing DecimalFormat API? Thanks for the suggestions! I think I may have to subclass DecimalFormat because I am limited by the interface that is already in place.

+1  A: 

Could you use printf() instead:

Format format = new DecimalFormat("0.0E0");
Double d = new Double(.01);
System.out.println(format.format(d));
System.out.printf("%1.1E\n", d);
d = new Double(100);
System.out.println(format.format(d));
System.out.printf("%1.1E\n", d);

Output:

1.0E-2
1.0E-02
1.0E2
1.0E+02

If you need to output to a String instead, you can use the information provided at Formatted Printing for Java (sprintf) to do that.

EDIT: Wow, that PrintfFormat() thing is huge and seems to be unnecessary:

OutputStream b = new ByteArrayOutputStream();
PrintStream p = new PrintStream(b);
p.printf("%1.1E", d);
System.out.println(b.toString());

I got the idea for the above code from Get an OutputStream into a String.

Grant Wagner
+2  A: 

Here's one way. Hokey, perhaps, but it works...

public class DecimalFormatTest extends TestCase {
    private static class MyFormat extends NumberFormat {
     private final DecimalFormat decimal;

     public MyFormat(String pattern) {
      decimal = new DecimalFormat(pattern);
     }

     public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
      StringBuffer sb = new StringBuffer();
      sb.append(modified(Math.abs(number) > 1.0, decimal.format(number, toAppendTo, pos).toString()));
      return sb;
     }

     private String modified(boolean large, String s) {
      return large ? s.replace("E", "E+") : s;
     }

     public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
      StringBuffer sb = new StringBuffer();
      sb.append(modified(true, decimal.format(number, toAppendTo, pos).toString()));
      return sb;
     }

     public Number parse(String source, ParsePosition parsePosition) {
      return decimal.parse(source, parsePosition);
     }

     public void setPositivePrefix(String newValue) {
      decimal.setPositivePrefix(newValue);
     }
    }
    private MyFormat format;

    protected void setUp() throws Exception {
     format = new MyFormat("0.0E0");
     format.setPositivePrefix("+");
    }

    public void testPositiveLargeNumber() throws Exception {
     assertEquals("+1.0E+2", format.format(100.0));
    }

    public void testPositiveSmallNumber() throws Exception {
     assertEquals("+1.0E-2", format.format(0.01));
    }

    public void testNegativeLargeNumber() throws Exception {
     assertEquals("-1.0E+2", format.format(-100.0));
    }

    public void testNegativeSmallNumber() throws Exception {
     assertEquals("-1.0E-2", format.format(-0.01));
    }
}

Alternatively you could subclass DecimalFormat, but I find it generally cleaner not to subclass from concrete classes.

Carl Manaster