mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8318915: Enhance checks in BigDecimal.toPlainString()
Reviewed-by: rriggs, bpb
This commit is contained in:
parent
7d25f1c6cb
commit
a6785e4d63
2 changed files with 57 additions and 20 deletions
|
@ -3503,21 +3503,19 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> {
|
|||
return "0";
|
||||
}
|
||||
int trailingZeros = checkScaleNonZero((-(long)scale));
|
||||
StringBuilder buf;
|
||||
if(intCompact!=INFLATED) {
|
||||
buf = new StringBuilder(20+trailingZeros);
|
||||
buf.append(intCompact);
|
||||
} else {
|
||||
String str = intVal.toString();
|
||||
buf = new StringBuilder(str.length()+trailingZeros);
|
||||
buf.append(str);
|
||||
}
|
||||
for (int i = 0; i < trailingZeros; i++) {
|
||||
buf.append('0');
|
||||
String str = intCompact != INFLATED
|
||||
? Long.toString(intCompact)
|
||||
: intVal.toString();
|
||||
int len = str.length() + trailingZeros;
|
||||
if (len < 0) {
|
||||
throw new OutOfMemoryError("too large to fit in a String");
|
||||
}
|
||||
StringBuilder buf = new StringBuilder(len);
|
||||
buf.append(str);
|
||||
buf.repeat('0', trailingZeros);
|
||||
return buf.toString();
|
||||
}
|
||||
String str ;
|
||||
String str;
|
||||
if(intCompact!=INFLATED) {
|
||||
str = Long.toString(Math.abs(intCompact));
|
||||
} else {
|
||||
|
@ -3527,11 +3525,11 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> {
|
|||
}
|
||||
|
||||
/* Returns a digit.digit string */
|
||||
private String getValueString(int signum, String intString, int scale) {
|
||||
private static String getValueString(int signum, String intString, int scale) {
|
||||
/* Insert decimal point */
|
||||
StringBuilder buf;
|
||||
int insertionPoint = intString.length() - scale;
|
||||
if (insertionPoint == 0) { /* Point goes right before intVal */
|
||||
if (insertionPoint == 0) { /* Point goes just before intVal */
|
||||
return (signum<0 ? "-0." : "0.") + intString;
|
||||
} else if (insertionPoint > 0) { /* Point goes inside intVal */
|
||||
buf = new StringBuilder(intString);
|
||||
|
@ -3539,11 +3537,13 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> {
|
|||
if (signum < 0)
|
||||
buf.insert(0, '-');
|
||||
} else { /* We must insert zeros between point and intVal */
|
||||
buf = new StringBuilder(3-insertionPoint + intString.length());
|
||||
buf.append(signum<0 ? "-0." : "0.");
|
||||
for (int i=0; i<-insertionPoint; i++) {
|
||||
buf.append('0');
|
||||
int len = (signum < 0 ? 3 : 2) + scale;
|
||||
if (len < 0) {
|
||||
throw new OutOfMemoryError("too large to fit in a String");
|
||||
}
|
||||
buf = new StringBuilder(len);
|
||||
buf.append(signum<0 ? "-0." : "0.");
|
||||
buf.repeat('0', -insertionPoint); // insertionPoint != MIN_VALUE
|
||||
buf.append(intString);
|
||||
}
|
||||
return buf.toString();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue