Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The comparison unexpectedly fails because s contains the string "0.0010", which is not equal to "0.001".

...

This noncompliant code example attempts to mitigate the extra trailing zero by using a regular expression on the string before comparing it.:

Code Block
bgColor#FFCCCC
int i = 1;
String s = Double.valueOf(i / 1000.0).toString();
s = s.replaceFirst("[.0]*$", "");
if (s.equals("0.001")) {
  // ...
}

While Although the comparison does succeed succeeds on the preceding code above, it fails on the following similar code below, which uses 1/10000.0 instead of 1/1000.0. The string produced is not 0.00010 but rather 1.0E-4.

...