Skip to content

Commit

Permalink
Java: Format string in the same way as Double.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
agdavydov81 committed Sep 11, 2023
1 parent 1930b48 commit 49cef98
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 118 deletions.
47 changes: 28 additions & 19 deletions java/dfp/src/main/java/com/epam/deltix/dfp/JavaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public static Appendable appendToRefImpl(final long value, final Appendable appe
}

if (0 == partsCoefficient)
return appendable.append('0');
return appendable.append("0.0");

if (value < 0)
appendable.append('-');
Expand All @@ -432,6 +432,7 @@ public static Appendable appendToRefImpl(final long value, final Appendable appe
appendLongTo(partsCoefficient, appendable, digits);
for (int i = 0; i < exponent; i += 1)
appendable.append('0');
appendable.append(".0");
} else if (digits + exponent > 0) {
final long integralPart = partsCoefficient / POWERS_OF_TEN[-exponent];
final long fractionalPart = partsCoefficient % POWERS_OF_TEN[-exponent];
Expand All @@ -441,6 +442,8 @@ public static Appendable appendToRefImpl(final long value, final Appendable appe
for (int i = numberOfDigits(fractionalPart); i < -exponent; i += 1)
appendable.append('0');
appendLongTo(dropTrailingZeros(fractionalPart), appendable);
} else {
appendable.append(".0");
}
} else {
appendable.append("0.");
Expand Down Expand Up @@ -546,16 +549,18 @@ public static String fastToString(final long value) {
}

if (partsCoefficient == 0)
return "0";
return "0.0";

int exponent = partsExponent - EXPONENT_BIAS;

final char[] buffer = CHAR_BUFFER.get();

if (exponent >= 0) {
int bi = buffer.length - exponent;
for (int i = buffer.length - exponent; i < buffer.length; ++i)
buffer[i] = '0';
int bi = buffer.length;
buffer[--bi] = '0';
buffer[--bi] = '.';
for (int i = 0; i < exponent; ++i)
buffer[--bi] = '0';

while (partsCoefficient > 0) {
bi = formatUIntFromBcdTable((int) (partsCoefficient % BCD_DIVIDER), buffer, bi);
Expand Down Expand Up @@ -608,8 +613,8 @@ public static String fastToString(final long value) {
while (buffer[be - 1] == '0')
--be;

if (buffer[be - 1] == '.')
--be;
if (buffer[be - 1] == '.' && be < buffer.length)
buffer[be++] = '0';

return new String(buffer, bi, be - bi);

Expand Down Expand Up @@ -786,16 +791,18 @@ public static StringBuilder fastAppendToStringBuilder(final long value, final St
}

if (partsCoefficient == 0)
return stringBuilder.append('0');
return stringBuilder.append("0.0");

int exponent = partsExponent - EXPONENT_BIAS;

final char[] buffer = CHAR_BUFFER.get();

if (exponent >= 0) {
int bi = buffer.length - exponent;
for (int i = buffer.length - exponent; i < buffer.length; ++i)
buffer[i] = '0';
int bi = buffer.length;
buffer[--bi] = '0';
buffer[--bi] = '.';
for (int i = 0; i < exponent; ++i)
buffer[--bi] = '0';

while (partsCoefficient > 0) {
bi = formatUIntFromBcdTable((int) (partsCoefficient % BCD_DIVIDER), buffer, bi);
Expand Down Expand Up @@ -848,8 +855,8 @@ public static StringBuilder fastAppendToStringBuilder(final long value, final St
while (buffer[be - 1] == '0')
--be;

if (buffer[be - 1] == '.')
--be;
if (buffer[be - 1] == '.' && be < buffer.length)
buffer[be++] = '0';

return stringBuilder.append(buffer, bi, be - bi);

Expand Down Expand Up @@ -1083,17 +1090,19 @@ public static Appendable fastAppendToAppendable(final long value, final Appendab
}

if (partsCoefficient == 0)
return appendable.append('0');
return appendable.append("0.0");

int exponent = partsExponent - EXPONENT_BIAS;

final MutableCharBuffer charBuffer = MUTABLE_CHAR_BUFFER.get();
final char[] buffer = charBuffer.buffer;

if (exponent >= 0) {
int bi = buffer.length - exponent;
for (int i = buffer.length - exponent; i < buffer.length; ++i)
buffer[i] = '0';
int bi = buffer.length;
buffer[--bi] = '0';
buffer[--bi] = '.';
for (int i = 0; i < exponent; ++i)
buffer[--bi] = '0';

while (partsCoefficient > 0) {
bi = formatUIntFromBcdTable((int) (partsCoefficient % BCD_DIVIDER), buffer, bi);
Expand Down Expand Up @@ -1146,8 +1155,8 @@ public static Appendable fastAppendToAppendable(final long value, final Appendab
while (buffer[be - 1] == '0')
--be;

if (buffer[be - 1] == '.')
--be;
if (buffer[be - 1] == '.' && be < buffer.length)
buffer[be++] = '0';

return appendable.append(charBuffer.setRange(bi, be - bi));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public void conversionToBigDecimalAndBack() {
}

private static void toBigDecimalAndBack(@Decimal final long aD64) {
final String aStr = Decimal64Utils.toString(aD64);
String aStr = Decimal64Utils.toString(aD64);
if (aStr.endsWith(".0"))
aStr = aStr.substring(0, aStr.length() - 2);

final BigDecimal big = Decimal64Utils.toBigDecimal(aD64);

Expand Down Expand Up @@ -94,10 +96,10 @@ private static void toDecimal64(final BigDecimal a) {

final String aStr = a.toPlainString();
String bStr = Decimal64Utils.toString(b);
if (!aStr.contains(".") && aStr.length() != bStr.length())
if (!aStr.contains(".") && (aStr.length() + 2) != bStr.length())
throw new RuntimeException("BigDecimal(=" + a + ") conversion to Decimal64 order mismatch.");

bStr = trimBackZerosAndDot(Decimal64Utils.toString(b));
bStr = trimBackZerosAndDot(bStr.endsWith(".0") ? bStr.substring(0, bStr.length() - 2) : bStr);
if (!bStr.isEmpty())
bStr = bStr.substring(0, bStr.length() - 1); // Remove rounded symbol

Expand Down
20 changes: 10 additions & 10 deletions java/dfp/src/test/java/com/epam/deltix/dfp/Decimal64Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ public void numberConversionTest() {
@Test
public void toStringTest() {

Assert.assertEquals("0", Decimal64.fromLong(0).toString());
Assert.assertEquals("42", Decimal64.fromLong(42).toString());
Assert.assertEquals(String.valueOf(Integer.MAX_VALUE), Decimal64.fromInt(Integer.MAX_VALUE).toString());
Assert.assertEquals(String.valueOf(Integer.MIN_VALUE), Decimal64.fromInt(Integer.MIN_VALUE).toString());
Assert.assertEquals(String.valueOf(Integer.MAX_VALUE), Decimal64.fromLong(Integer.MAX_VALUE).toString());
Assert.assertEquals(String.valueOf(Integer.MIN_VALUE), Decimal64.fromLong(Integer.MIN_VALUE).toString());
Assert.assertEquals(String.valueOf(Integer.MAX_VALUE), Decimal64.fromFixedPoint(Integer.MAX_VALUE, 0).toString());
Assert.assertEquals(String.valueOf(Integer.MIN_VALUE), Decimal64.fromFixedPoint(Integer.MIN_VALUE, 0).toString());
Assert.assertEquals("0.0", Decimal64.fromLong(0).toString());
Assert.assertEquals("42.0", Decimal64.fromLong(42).toString());
Assert.assertEquals(Integer.MAX_VALUE + ".0", Decimal64.fromInt(Integer.MAX_VALUE).toString());
Assert.assertEquals(Integer.MIN_VALUE + ".0", Decimal64.fromInt(Integer.MIN_VALUE).toString());
Assert.assertEquals(Integer.MAX_VALUE + ".0", Decimal64.fromLong(Integer.MAX_VALUE).toString());
Assert.assertEquals(Integer.MIN_VALUE + ".0", Decimal64.fromLong(Integer.MIN_VALUE).toString());
Assert.assertEquals(Integer.MAX_VALUE + ".0", Decimal64.fromFixedPoint(Integer.MAX_VALUE, 0).toString());
Assert.assertEquals(Integer.MIN_VALUE + ".0", Decimal64.fromFixedPoint(Integer.MIN_VALUE, 0).toString());
Assert.assertEquals("123.456", Decimal64.fromDouble(123.456).toString());
Assert.assertEquals("123.4567", Decimal64.fromFixedPoint(1234567, 4).toString());

Expand All @@ -282,8 +282,8 @@ public void toStringTest() {
Assert.assertEquals("Infinity", Decimal64.toString(Decimal64.POSITIVE_INFINITY));
Assert.assertEquals("-Infinity", Decimal64.toString(Decimal64.NEGATIVE_INFINITY));

Assert.assertEquals("0", Decimal64.toString(Decimal64.ZERO));
Assert.assertEquals("1000000", Decimal64.toString(Decimal64.MILLION));
Assert.assertEquals("0.0", Decimal64.toString(Decimal64.ZERO));
Assert.assertEquals("1000000.0", Decimal64.toString(Decimal64.MILLION));
Assert.assertEquals("0.01", Decimal64.toString(Decimal64.ONE_HUNDREDTH));
}
}
Loading

0 comments on commit 49cef98

Please sign in to comment.