in src/main/java/org/apache/xmlgraphics/util/DoubleFormatUtil.java [279:330]
public static void formatDoubleFast(double source, int decimals, int precision, StringBuffer target) {
if (isRoundedToZero(source, decimals, precision)) {
// Will always be rounded to 0
target.append('0');
return;
} else if (Double.isNaN(source) || Double.isInfinite(source)) {
// Cannot be formated
target.append(Double.toString(source));
return;
}
boolean isPositive = source >= 0.0;
source = Math.abs(source);
int scale = (source >= 1.0) ? decimals : precision;
long intPart = (long) Math.floor(source);
double tenScale = tenPowDouble(scale);
double fracUnroundedPart = (source - intPart) * tenScale;
long fracPart = Math.round(fracUnroundedPart);
if (fracPart >= tenScale) {
intPart++;
fracPart = Math.round(fracPart - tenScale);
}
if (fracPart != 0L) {
// Remove trailing zeroes
while (fracPart % 10L == 0L) {
fracPart = fracPart / 10L;
scale--;
}
}
if (intPart != 0L || fracPart != 0L) {
// non-zero value
if (!isPositive) {
// negative value, insert sign
target.append('-');
}
// append integer part
target.append(intPart);
if (fracPart != 0L) {
// append fractional part
target.append('.');
// insert leading zeroes
while (scale > 0 && fracPart < tenPowDouble(--scale)) {
target.append('0');
}
target.append(fracPart);
}
} else {
target.append('0');
}
}