Java prints double numbers in exponential format

I have several double numbers that are output in this format:

Format.String("%1.4e",doubleNumber);

Result 1.123456e+03. How to set the number of exponent ciphers to get this format:

1.123456e+003

I would always have 3 characters after the character e. thank you


UPDATE 1:

I partially allowed:

  DecimalFormat formatter = new DecimalFormat("0.000000E000");

System.out.println (formatter.format (doubleNumber));

Now the number always has the format

1.123456e0xx

or

1.123456e-0xx

But this is not all resolved. I would always print a sign:

1.123456e+0xx or 1.123456e-0xx

How can i do this? http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

+4
source share
3 answers

, .

- , , , .


, String.format, Format.String "%1.4e", , 1.1235e+03, 1.123456e+03.

0

Java- String Formatter, . , .

long exponentMask = 0x7ff0000000000000L; // per JavaDoc for Double
// the exponent is in bits 62-52, so I'm thinking a right-shift by 52
// bits would work. But I could be off by one.
int shiftExponentBy = 52;
long myDoubleAsLong = Double.doubleToLongBits(doubleNumber);
long rawExponent = myDoubleAsLong & exponentMask;
long shiftedExponent = rawExponent >> shiftExponentBy;

. , .

1

, 1 , -. . DecimalFormat , . , . . .

0

Thanks @TDG

  private String formatter(double number){
    DecimalFormat formatter = new DecimalFormat("0.000000E000");
    String fnumber = formatter.format(number);
    if (!fnumber.contains("E-")) { //don't blast a negative sign
        fnumber = fnumber.replace("E", "E+");
    }
    return fnumber;
}
0
source

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


All Articles