mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2025-01-09 08:24:10 -08:00
Fix some cases of formatting numbers
This commit is contained in:
parent
a66faba99c
commit
c4f6aed639
|
@ -241,8 +241,8 @@ public class TDecimalFormat extends TNumberFormat {
|
||||||
buffer.append(positive ? positivePrefix : negativePrefix);
|
buffer.append(positive ? positivePrefix : negativePrefix);
|
||||||
|
|
||||||
// Add insignificant integer zeros
|
// Add insignificant integer zeros
|
||||||
int digitPos = exponent;
|
|
||||||
int intLength = Math.max(0, exponent);
|
int intLength = Math.max(0, exponent);
|
||||||
|
int digitPos = Math.max(intLength, getMinimumIntegerDigits()) - 1;
|
||||||
for (int i = getMinimumIntegerDigits() - 1; i >= intLength; --i) {
|
for (int i = getMinimumIntegerDigits() - 1; i >= intLength; --i) {
|
||||||
buffer.append('0');
|
buffer.append('0');
|
||||||
if (groupingSize > 0 && digitPos % groupingSize == 0 && digitPos > 0) {
|
if (groupingSize > 0 && digitPos % groupingSize == 0 && digitPos > 0) {
|
||||||
|
@ -275,20 +275,24 @@ public class TDecimalFormat extends TNumberFormat {
|
||||||
--digitPos;
|
--digitPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (digitPos == 0 && getMinimumFractionDigits() == 0) {
|
if (mantissa == 0) {
|
||||||
if (isDecimalSeparatorAlwaysShown()) {
|
if (getMinimumFractionDigits() == 0) {
|
||||||
|
if (isDecimalSeparatorAlwaysShown()) {
|
||||||
|
buffer.append(symbols.getDecimalSeparator());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
buffer.append(symbols.getDecimalSeparator());
|
buffer.append(symbols.getDecimalSeparator());
|
||||||
|
for (int i = 0; i < getMinimumFractionDigits(); ++i) {
|
||||||
|
buffer.append('0');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
buffer.append(symbols.getDecimalSeparator());
|
buffer.append(symbols.getDecimalSeparator());
|
||||||
|
|
||||||
// Add significant fractional zeros
|
// Add significant fractional zeros
|
||||||
int fracZeros = Math.min(getMinimumFractionDigits(), Math.max(0, -exponent));
|
int fracZeros = Math.min(getMaximumFractionDigits(), Math.max(0, -exponent));
|
||||||
digitPos = 0;
|
digitPos = 0;
|
||||||
for (int i = 0; i < fracZeros; ++i) {
|
for (int i = 0; i < fracZeros; ++i) {
|
||||||
if (groupingSize > 0 && digitPos % groupingSize == 0 && digitPos > 0) {
|
|
||||||
buffer.append(symbols.getGroupingSeparator());
|
|
||||||
}
|
|
||||||
++digitPos;
|
++digitPos;
|
||||||
buffer.append('0');
|
buffer.append('0');
|
||||||
}
|
}
|
||||||
|
@ -299,9 +303,6 @@ public class TDecimalFormat extends TNumberFormat {
|
||||||
if (mantissa == 0) {
|
if (mantissa == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (groupingSize > 0 && digitPos % groupingSize == 0 && digitPos > 0) {
|
|
||||||
buffer.append(symbols.getGroupingSeparator());
|
|
||||||
}
|
|
||||||
++digitPos;
|
++digitPos;
|
||||||
long mantissaDigitMask = POW10_ARRAY[mantissaDigit];
|
long mantissaDigitMask = POW10_ARRAY[mantissaDigit];
|
||||||
buffer.append(Character.forDigit((int)(mantissa / mantissaDigitMask), 10));
|
buffer.append(Character.forDigit((int)(mantissa / mantissaDigitMask), 10));
|
||||||
|
@ -311,9 +312,6 @@ public class TDecimalFormat extends TNumberFormat {
|
||||||
|
|
||||||
// Add insignificant fractional zeros
|
// Add insignificant fractional zeros
|
||||||
for (int i = digitPos; i < getMinimumFractionDigits(); ++i) {
|
for (int i = digitPos; i < getMinimumFractionDigits(); ++i) {
|
||||||
if (groupingSize > 0 && digitPos % groupingSize == 0 && digitPos > 0) {
|
|
||||||
buffer.append(symbols.getGroupingSeparator());
|
|
||||||
}
|
|
||||||
++digitPos;
|
++digitPos;
|
||||||
buffer.append('0');
|
buffer.append('0');
|
||||||
}
|
}
|
||||||
|
@ -395,6 +393,9 @@ public class TDecimalFormat extends TNumberFormat {
|
||||||
}
|
}
|
||||||
|
|
||||||
private long normalize(long value) {
|
private long normalize(long value) {
|
||||||
|
if (value >= MANTISSA_PATTERN * 10) {
|
||||||
|
value /= 10;
|
||||||
|
}
|
||||||
if (value < MANTISSA_PATTERN / 1_000_0000_0000_0000L) {
|
if (value < MANTISSA_PATTERN / 1_000_0000_0000_0000L) {
|
||||||
value *= 1_0000_0000_0000_0000L;
|
value *= 1_0000_0000_0000_0000L;
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,6 +113,15 @@ public class DecimalFormatTest {
|
||||||
assertEquals(0, format.getMaximumFractionDigits());
|
assertEquals(0, format.getMaximumFractionDigits());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void formatsIntegerPart() {
|
||||||
|
DecimalFormat format = createFormat("00");
|
||||||
|
assertEquals("02", format.format(2));
|
||||||
|
assertEquals("23", format.format(23));
|
||||||
|
assertEquals("23", format.format(23.2));
|
||||||
|
assertEquals("24", format.format(23.7));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatsNumber() {
|
public void formatsNumber() {
|
||||||
DecimalFormat format = createFormat("0.0");
|
DecimalFormat format = createFormat("0.0");
|
||||||
|
@ -138,7 +147,6 @@ public class DecimalFormatTest {
|
||||||
@Test
|
@Test
|
||||||
public void formatsFractionalPart() {
|
public void formatsFractionalPart() {
|
||||||
DecimalFormat format = createFormat("0.0000####");
|
DecimalFormat format = createFormat("0.0000####");
|
||||||
|
|
||||||
assertEquals("0.00001235", format.format(0.0000123456));
|
assertEquals("0.00001235", format.format(0.0000123456));
|
||||||
assertEquals("0.00012346", format.format(0.000123456));
|
assertEquals("0.00012346", format.format(0.000123456));
|
||||||
assertEquals("0.00123456", format.format(0.00123456));
|
assertEquals("0.00123456", format.format(0.00123456));
|
||||||
|
@ -147,6 +155,12 @@ public class DecimalFormatTest {
|
||||||
assertEquals("0.1230", format.format(0.123));
|
assertEquals("0.1230", format.format(0.123));
|
||||||
assertEquals("0.1234", format.format(0.1234));
|
assertEquals("0.1234", format.format(0.1234));
|
||||||
assertEquals("0.12345", format.format(0.12345));
|
assertEquals("0.12345", format.format(0.12345));
|
||||||
|
|
||||||
|
format = createFormat("0.##");
|
||||||
|
assertEquals("23", format.format(23));
|
||||||
|
assertEquals("2.3", format.format(2.3));
|
||||||
|
assertEquals("0.23", format.format(0.23));
|
||||||
|
assertEquals("0.02", format.format(0.023));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -202,6 +216,24 @@ public class DecimalFormatTest {
|
||||||
assertEquals("-4", format.format(-3.5));
|
assertEquals("-4", format.format(-3.5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void formatsWithGroups() {
|
||||||
|
DecimalFormat format = createFormat("#,###.0");
|
||||||
|
assertEquals("23.0", format.format(23));
|
||||||
|
assertEquals("2,300.0", format.format(2300));
|
||||||
|
assertEquals("2,300,000,000,000,000,000,000.0", format.format(23E20));
|
||||||
|
assertEquals("23,000,000,000,000,000,000,000,000.0", format.format(23E24));
|
||||||
|
|
||||||
|
format = createFormat("000,000,000,000,000,000,000");
|
||||||
|
assertEquals("000,000,000,000,000,000,023", format.format(23));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void formatsLargeValues() {
|
||||||
|
DecimalFormat format = createFormat("0");
|
||||||
|
assertEquals("9223372036854775807", format.format(9223372036854775807L));
|
||||||
|
}
|
||||||
|
|
||||||
private DecimalFormat createFormat(String format) {
|
private DecimalFormat createFormat(String format) {
|
||||||
return new DecimalFormat(format, symbols);
|
return new DecimalFormat(format, symbols);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user