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
Asked
Active
Viewed 148 times
0
-
"I don't know what I'm doing wrong please help me." That's because you aren't doing anything. Where are you printing your result? Your main is empty. – Spectric Sep 21 '20 at 20:11
-
Please properly format the code in your question. – WJS Sep 21 '20 at 20:14
-
so im using bluej and i make an object and fill in the parameters for Testing then i right lcick on the object and click on double drawDecimal() – asdaszxcasd Sep 21 '20 at 20:17
-
1"... will return 0.11000000000000032 instead of 0.11 ..." - what makes you think that behaviour's wrong? There's no `double` that's exactly equal to `0.11`. – Dawood ibn Kareem Sep 21 '20 at 20:26
-
so then what can I do to fix it? – asdaszxcasd Sep 21 '20 at 20:44
1 Answers
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