in src/com/amazon/ion/impl/_Private_IonTextAppender.java [719:798]
public void printDecimal(_Private_IonTextWriterBuilder _options,
BigDecimal value)
throws IOException
{
if (value == null)
{
appendAscii("null.decimal");
return;
}
BigInteger unscaled = value.unscaledValue();
int signum = value.signum();
if (signum < 0)
{
appendAscii('-');
unscaled = unscaled.negate();
}
else if (value instanceof Decimal
&& ((Decimal)value).isNegativeZero())
{
// for the various forms of negative zero we have to
// write the sign ourselves, since neither BigInteger
// nor BigDecimal recognize negative zero, but Ion does.
appendAscii('-');
}
final String unscaledText = unscaled.toString();
final int significantDigits = unscaledText.length();
final int scale = value.scale();
final int exponent = -scale;
if (_options._decimal_as_float)
{
appendAscii(unscaledText);
appendAscii('e');
appendAscii(Integer.toString(exponent));
}
else if (exponent == 0)
{
appendAscii(unscaledText);
appendAscii('.');
}
else if (exponent < 0)
{
// Avoid printing small negative exponents using a heuristic
// adapted from http://speleotrove.com/decimal/daconvs.html
final int adjustedExponent = significantDigits - 1 - scale;
if (adjustedExponent >= 0)
{
int wholeDigits = significantDigits - scale;
appendAscii(unscaledText, 0, wholeDigits);
appendAscii('.');
appendAscii(unscaledText, wholeDigits,
significantDigits);
}
else if (adjustedExponent >= -6)
{
appendAscii("0.");
appendAscii("00000", 0, scale - significantDigits);
appendAscii(unscaledText);
}
else
{
appendAscii(unscaledText);
appendAscii("d-");
appendAscii(Integer.toString(scale));
}
}
else // (exponent > 0)
{
// We cannot move the decimal point to the right, adding
// rightmost zeros, because that would alter the precision.
appendAscii(unscaledText);
appendAscii('d');
appendAscii(Integer.toString(exponent));
}
}