Code example for DecimalFormat

Methods: format, setGroupingUsed, setMaximumFractionDigits

0
public class BigDecimalDataConverter implements DataConverter<BigDecimal> {
 
    public String valueToString(BigDecimal value) {
        if (value == null) return "";
        DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(LocaleContextHolder.getLocale());
        df.setGroupingUsed(false);
        df.setMaximumFractionDigits(Integer.MAX_VALUE);
        return df.format(value);
    } 
 
    @Override 
    public BigDecimal stringToValue(String rawData) throws ParseException{
        return rawData != null && !"".equals(rawData) ? (new BigDecimal(rawData.replace(",", "."))).stripTrailingZeros() : null;
    } 
}