Java DecimalFormat creates an error when forcing an exponent

In Java, I'm trying to get DecimalFormat to provide an exponent sign. When it's positive, I need a plus sign. From what I read, it seems uninteresting, but for me it always causes an error. I appreciate that there may be other methods to achieve my goal, but I would like to understand why an error occurs in this particular method.

Double result = 123.456;
String sresult;

//This works
NumberFormat formatter = new DecimalFormat("0.00000E00");
        sresult = formatter.format(result); 
        System.out.println(sresult);        //1.23456E02

//This doesn't work
formatter = new DecimalFormat("0.00000E+00"); //Want to enforce the sign to appear
        sresult = formatter.format(result); 
        System.out.println(sresult);        //Expected 1.23456E+02 but error occurs

Error thrown:

Exception in thread "main" java.lang.IllegalArgumentException:
Malformed exponential pattern "0.00000E + 00"
    at java.text.DecimalFormat.applyPattern (Unknown Source)
    at java.text.DecimalFormat. (Unknown Source)
    at deccheck.main (deccheck.java:13)

I appreciate any insight. Thanks Mark

+2
4

:

formatter = new DecimalFormat("0.00000E00"); // Want to enforce the sign to appear
sresult = formatter.format(result);
if (!sresult.contains("E-")) { //don't blast a negative sign
    sresult = sresult.replace("E", "E+");
}
System.out.println(sresult);

1.23456E+02 .

, DecimalFormat. , , javadoc , .

: trashgod . , DecimalFormatSymbols, .

2: , E . , .

+3

j flemm. "E" "-" , DecimalFormatSymbols. :

public static String hoola(final String s, final DecimalFormatSymbols symbols) {
    String result;
    final String expo = symbols.getExponentSeparator();
    final char minus = symbols.getMinusSign();
    if (!s.contains(expo + minus)) { // don't blast a negative sign
    result = s.replace(expo, expo + '+');
    } else {result=s;}
    return result;
}

/**
 * @param args
 */
public static void main(final String[] args) {
    final DecimalFormat decForm = (DecimalFormat) NumberFormat
            .getInstance(Locale.GERMAN);
    final DecimalFormatSymbols newSymbols = new DecimalFormatSymbols(
            Locale.GERMAN);
    newSymbols.setExponentSeparator("*10^");
    newSymbols.setMinusSign('\u2212');
    decForm.setDecimalFormatSymbols(newSymbols);
    decForm.applyPattern("0.00000E00");
    System.out.println(hoola(decForm.format(1234.567), decForm
            .getDecimalFormatSymbols()));
    System.out.println(hoola(decForm.format(000.00567), decForm
            .getDecimalFormatSymbols()));
}

:

  • 1,23457 * 10 ^ + 03
  • 5,67000 * 10 ^ -03
+3

, "+" ( javadocs). si :

 Exponent:
         E MinimumExponent
 MinimumExponent:
         0 MinimumExponent(opt)

- , . , - ( "+" ), , "+" .

sresult = formatter.format(result);
sresult = sresult.replaceAll("E([^\\-]+)", "E+$1");

DecimalFormat

+1

@j flemm is attractive because "Negative metrics are formatted using the localized minus sign rather than the prefix and suffix from the template." - DecimalFormat.

+1
source

Source: https://habr.com/ru/post/1608548/


All Articles