[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug classpath/90755] New: BigDecimal.stripTrailingZero throws exception
From: |
guillerodriguez.dev at gmail dot com |
Subject: |
[Bug classpath/90755] New: BigDecimal.stripTrailingZero throws exception if value is zero |
Date: |
Wed, 05 Jun 2019 09:55:20 +0000 |
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90755
Bug ID: 90755
Summary: BigDecimal.stripTrailingZero throws exception if value
is zero
Product: classpath
Version: 0.99
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: classpath
Assignee: unassigned at gcc dot gnu.org
Reporter: guillerodriguez.dev at gmail dot com
Target Milestone: ---
Here's a simple test case:
BigDecimal bd1 = new BigDecimal("0.001");
bd1 = bd1.setScale(2, BigDecimal.ROUND_HALF_UP);
bd1 = bd1.stripTrailingZeros();
This results in:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
index out of range: -1
at java.lang.String.charAt(String.java:692)
at java.math.BigDecimal.stripTrailingZeros(BigDecimal.java:1340)
[...]
Here's the code that throws the exception:
public BigDecimal stripTrailingZeros()
{
String intValStr = intVal.toString();
int newScale = scale;
int pointer = intValStr.length() - 1;
// This loop adjusts pointer which will be used to give us the substring
// of intValStr to use in our new BigDecimal, and also accordingly
// adjusts the scale of our new BigDecimal.
while (intValStr.charAt(pointer) == '0')
{
pointer --;
newScale --;
}
The above loop will happily walk beyond the end of the string if all digits are
'0'.
- [Bug classpath/90755] New: BigDecimal.stripTrailingZero throws exception if value is zero,
guillerodriguez.dev at gmail dot com <=