0

a partial string that begins at input parameter index position of the String str (instance variable) Returns empty string if the input parameter index position is larger than or equal to the string length

1 Answers1

1

The easiest way to get the "decimals" of a double value, meaning the fractional digits of the number, is to use the % remainder operator with a divisor of 1, i.e.

public double drawDecimal() {
    return dD % 1;
}

That has the same problem with returning 0.11000000000000032, which is an effect of the inherent inaccuracy of floating-point numbers. See: Is floating point math broken?

To get around that, you could do the same calculation using BigDecimal, i.e.

public double drawDecimal() {
    return BigDecimal.valueOf(dD).remainder(BigDecimal.ONE).doubleValue();
}

The result is 0.11, keeping the "decimal precision" of the original value.

Andreas
  • 154,647
  • 11
  • 152
  • 247