-
-
Save ksiwonia/5063253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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