-2

As the question suggests, I'm using String.format("%.5e",someDouble) because I want the following format #.#####E+#.

I'm making a calculator for a tutorial. Let's say I'm typing in 333,333,333,333 * 333,333,333,333. The answer in the correct format should be "1.11111E+23". But instead it's giving me "1.1111111111089" no matter what number I put before the 'e' in the format argument of the String.format(String format, Object... args) method.

Community
  • 1
  • 1
user3376587
  • 124
  • 2
  • 12

2 Answers2

0

The problem must be somewhere else, because your formatting is correct. I've tried this:

public static void main (String[] args) throws java.lang.Exception
{
    double number = 333333333333l*(double)333333333333l;
    System.out.println(String.format("%.5e",number));
}

And the result is 1.11111e+23

...assuming it is Java. You can check here: https://ideone.com/VubwX7

NeplatnyUdaj
  • 5,471
  • 4
  • 38
  • 70
0

My problem was indeed not in the formatting of the String. There was a bit of code before the formatting that truncated the String at a certain length of characters if it was in decimal format already. What I did not realize was that the equation that I was using was already so large it actually NEEDED to be expressed in scientific notation. So when it was truncated, it deleted the "e+23", and left just a plain 'ol decimal number. It wasn't even reaching the code that formatted the String, ironically.

user3376587
  • 124
  • 2
  • 12