in metrics-core/src/benchmarks/java/com/amazon/metrics/DoubleFormat.java [72:124]
private static void format_exception(Appendable sb, double value) throws IOException {
// store the sign of the input, get the input as a positive number w/o sign
boolean negative = (value < 0);
double positiveValue = Math.abs(value);
// if value is too large for us to handle, revert to builtin formatter
if (positiveValue > TOO_BIG_THRESHOLD) {
handleInputTooBig(sb, value);
return;
}
// Separate the number into whole number and fraction components.
// Scale the fraction component based upon the max number of digits to display.
// Additionally, store remaining (unprinted) fraction amount for use in rounding
// decisions.
int whole = (int) positiveValue;
double tmp = (positiveValue - whole) * MAX;
int fraction = Math.abs((int) tmp);
double diff = tmp - fraction;
int comp = Double.compare(diff, 0.5);
if(comp > 0) {
// round up
fraction++;
} else if(comp == 0 && ((fraction == 0) || (fraction & 1) != 0)) {
// round up
fraction++;
}
// if rounding has pushed us up to the next whole number, adjust:
// clear fraction amount and increment whole number
if(fraction >= MAX) {
fraction = 0;
whole++;
}
int leadingZeros = countLeadingZeros(fraction);
fraction = trimTrailingZeros(fraction);
// Once the double value has been appropriately split and rounded, write as a string
if(negative) {
sb.append('-');
}
sb.append(Integer.toString(whole));
if(fraction != 0) {
sb.append('.');
for(int i = 0; i < leadingZeros; i++) {
sb.append('0');
}
sb.append(Integer.toString(fraction));
}
}