Guest User

GarciaPL

a guest
Jan 23rd, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package pl.garciapl.test;
  2.  
  3. import org.springframework.format.Formatter;
  4.  
  5. import java.math.BigDecimal;
  6. import java.text.DecimalFormat;
  7. import java.text.NumberFormat;
  8. import java.text.ParseException;
  9. import java.util.Locale;
  10.  
  11. public class CurrencyLocaleBigDecimalFormatter implements Formatter<BigDecimal> {
  12.  
  13.     @Override
  14.     public BigDecimal parse(String text, Locale locale) throws ParseException {
  15.         DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH);
  16.         format.applyPattern("#,##0.00");
  17.         format.setParseBigDecimal(true);
  18.         format.setGroupingUsed(true);
  19.         return (BigDecimal) format.parse(text);
  20.     }
  21.  
  22.     @Override
  23.     public String print(BigDecimal object, Locale locale) {
  24.         DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(locale);
  25.         format.applyPattern("#,##0.00");
  26.         format.setParseBigDecimal(true);
  27.         format.setGroupingUsed(false);
  28.         return format.format(object);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment