@SuppressWarnings("squid:S2111")
public String format(Number num) {
+ final double d = num.doubleValue();;
+ try {
+ //this is the recommended way (in BigDecimal javadocs to create a decimal from a double)
+ return format(new BigDecimal(Double.toString(d)));
+ } catch (NumberFormatException nfe) {
+ //Double.toString can fail
+ return format(d);
+ }
+ }
- final BigDecimal doubleValue = new BigDecimal(num.doubleValue());
-
- final boolean isNeg = doubleValue.compareTo(BigDecimal.ZERO) < 0;
+ @SuppressWarnings("squid:S2111")
+ private String format(final BigDecimal decimal) {
+ final boolean isNeg = decimal.compareTo(BigDecimal.ZERO) < 0;
- final BigDecimal absValue = doubleValue.abs();
+ final BigDecimal absValue = decimal.abs();
final BigDecimal wholePart = new BigDecimal(absValue.toBigInteger());
final BigDecimal decPart = absValue.remainder(BigDecimal.ONE);
}
} catch (RuntimeException e){
LOGGER.atWarn().withThrowable(e).log("Can't format fraction");
- return Double.toString(doubleValue.doubleValue());
+ return Double.toString(decimal.doubleValue());
}
StringBuilder sb = new StringBuilder();
return sb.toString();
}
+ @Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
return toAppendTo.append(format((Number)obj));
}
+ @Override
public Object parseObject(String source, ParsePosition pos) {
throw new NotImplementedException("Reverse parsing not supported");
}