Skip to content

Instantly share code, notes, and snippets.

@ksiwonia
Created March 1, 2013 08:28
Show Gist options
  • Save ksiwonia/5063253 to your computer and use it in GitHub Desktop.
Save ksiwonia/5063253 to your computer and use it in GitHub Desktop.
public final class BigDecimalUtils {
private static BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100L);
private BigDecimalUtils() {
}
public static BigDecimal convertNullToZero(final Object value) {
if (value == null) {
return BigDecimal.ZERO;
} else if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
return BigDecimal.valueOf(Double.valueOf(value.toString()));
}
public static BigDecimal convertNullToOne(final Object value) {
if (value == null) {
return BigDecimal.ONE;
} else if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
return BigDecimal.valueOf(Double.valueOf(value.toString()));
}
public static BigDecimal toPercent(final BigDecimal decimalValue, final MathContext mathCntext) {
return convertNullToZero(decimalValue).divide(ONE_HUNDRED, mathCntext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment