List of usage examples for java.text NumberFormat getNumberInstance
public static final NumberFormat getNumberInstance()
From source file:Main.java
/** * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java *//*from ww w. j a va 2 s .c o m*/ public static BigDecimal parseMoney(String money) { String sMoney = money; if (sMoney != null) { sMoney = sMoney.trim(); // to be safe try { return new BigDecimal(sMoney); } catch (NumberFormatException e) { /* there must be commas, etc in the number. Need to look for them * and remove them first, and then try BigDecimal again. If that * fails, then give up and use NumberFormat and scale it down * */ String[] split = MONEY_PREFIX_PATTERN.split(sMoney); if (split.length >= 2) { StringBuilder buf = new StringBuilder(); if (sMoney.startsWith("-")) { buf.append('-'); } for (int i = 0; i < split.length - 1; i++) { buf.append(split[i]); } buf.append('.'); buf.append(split[split.length - 1]); try { return new BigDecimal(buf.toString()); } catch (final NumberFormatException e2) { Log.e("QifUtils", "Second parse attempt failed, falling back to rounding"); } } NumberFormat formatter = NumberFormat.getNumberInstance(); try { Number num = formatter.parse(sMoney); return new BigDecimal(num.floatValue()); } catch (ParseException ignored) { } Log.e("QifUtils", "Could not parse money " + sMoney); } } return new BigDecimal(0); }
From source file:Main.java
/** * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java *//*from w ww.j av a2s . c o m*/ public static long parseMoney(String money) { if (money != null) { BigDecimal bdMoney; money = money.trim(); // to be safe try { bdMoney = new BigDecimal(money); return moneyAsLong(bdMoney); } catch (NumberFormatException e) { /* there must be commas, etc in the number. Need to look for them * and remove them first, and then try BigDecimal again. If that * fails, then give up and use NumberFormat and scale it down * */ String[] split = MONEY_PREFIX_PATTERN.split(money); if (split.length > 1) { StringBuilder buf = new StringBuilder(); if (money.startsWith("-")) { buf.append('-'); } for (int i = 0; i < split.length - 1; i++) { buf.append(split[i]); } buf.append('.'); buf.append(split[split.length - 1]); try { bdMoney = new BigDecimal(buf.toString()); return moneyAsLong(bdMoney); } catch (final NumberFormatException e2) { Log.e("QifUtils", "Second parse attempt failed, falling back to rounding"); } } NumberFormat formatter = NumberFormat.getNumberInstance(); try { Number num = formatter.parse(money); BigDecimal bd = new BigDecimal(num.floatValue()); if (bd.scale() > 6) { bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); } return moneyAsLong(bd); } catch (ParseException ignored) { } Log.e("QifUtils", "Could not parse money " + money); } } return 0; }
From source file:Main.java
/** * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java *//*w w w . j a v a 2 s . co m*/ public static long parseMoney(String money) { String sMoney = money; if (sMoney != null) { BigDecimal bdMoney; sMoney = sMoney.trim(); // to be safe try { bdMoney = new BigDecimal(sMoney); return moneyAsLong(bdMoney); } catch (NumberFormatException e) { /* there must be commas, etc in the number. Need to look for them * and remove them first, and then try BigDecimal again. If that * fails, then give up and use NumberFormat and scale it down * */ String[] split = MONEY_PREFIX_PATTERN.split(sMoney); if (split.length > 2) { StringBuilder buf = new StringBuilder(); if (sMoney.startsWith("-")) { buf.append('-'); } for (int i = 0; i < split.length - 1; i++) { buf.append(split[i]); } buf.append('.'); buf.append(split[split.length - 1]); try { bdMoney = new BigDecimal(buf.toString()); return moneyAsLong(bdMoney); } catch (final NumberFormatException e2) { Log.e("QifUtils", "Second parse attempt failed, falling back to rounding"); } } NumberFormat formatter = NumberFormat.getNumberInstance(); try { Number num = formatter.parse(sMoney); BigDecimal bd = new BigDecimal(num.floatValue()); if (bd.scale() > 6) { bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); } return moneyAsLong(bd); } catch (ParseException ignored) { } Log.e("QifUtils", "Could not parse money " + sMoney); } } return 0; }
From source file:org.totschnig.myexpenses.Utils.java
/** * <a href="http://www.ibm.com/developerworks/java/library/j-numberformat/">http://www.ibm.com/developerworks/java/library/j-numberformat/</a> * @param strFloat parsed as float with the number format defined in the locale * @return the float retrieved from the string or null if parse did not succeed */// w w w . j a va2 s .c o m public static Float validateNumber(String strFloat) { ParsePosition pp; NumberFormat nfDLocal = NumberFormat.getNumberInstance(); nfDLocal.setGroupingUsed(false); pp = new ParsePosition(0); pp.setIndex(0); Number n = nfDLocal.parse(strFloat, pp); if (strFloat.length() != pp.getIndex() || n == null) { return null; } else { return n.floatValue(); } }
From source file:picocash.model.impl.Money.java
public Money(String string) { try {/*w w w . j a va 2 s . c o m*/ this.value = (long) (NumberFormat.getNumberInstance().parse(string).doubleValue() * 100); } catch (ParseException ex) { log.debug("can't convert to long:", ex); } }
From source file:thymeleafsandbox.springjsp.web.conversion.NumberFormatter.java
public Number parse(final String text, final Locale locale) throws ParseException { final DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(); final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); symbols.setGroupingSeparator('*'); numberFormat.setDecimalFormatSymbols(symbols); return numberFormat.parse(text); }
From source file:com.aplikasi.penjualan.controller.DataBarangHtmlController.java
@InitBinder public void initBinder2(WebDataBinder binder) { NumberFormat numberFormat = NumberFormat.getNumberInstance(); numberFormat.setGroupingUsed(false); binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, numberFormat, true)); }
From source file:Main.java
/** * Normalize decimal string/*from w ww.j a v a 2s. co m*/ * exclude the Expo * @param numericStr * @param isDecimal * @param digits * @return */ public static String normalizeNumericString(String numericStr, boolean isDecimal, int digits) { if (isDecimal) { digits += 1; Double d = new Double(numericStr); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(7); numericStr = nf.format(d); int dotIndex = numericStr.indexOf("."); if (numericStr.length() <= (dotIndex + digits)) return numericStr; String result = numericStr.substring(0, dotIndex + digits); result = result.replace(",", ""); return result; } throw new RuntimeException(); }
From source file:org.apache.hadoop.hive.ql.udf.UDFNumberFormat.java
public String evaluate(DoubleWritable d, IntWritable fracMax, IntWritable fracMin, IntWritable intMax, IntWritable intMin) {/*w w w . ja va2 s . c o m*/ if (d == null || fracMax == null || intMax == null) { return null; } if (fracMin == null || intMin == null) { return null; } double ori = d.get(); try { NumberFormat nFormat = NumberFormat.getNumberInstance(); nFormat.setMaximumFractionDigits(fracMax.get()); nFormat.setMaximumIntegerDigits(intMax.get()); nFormat.setMinimumFractionDigits(fracMin.get()); nFormat.setMinimumIntegerDigits(intMin.get()); nFormat.setGroupingUsed(false); return nFormat.format(ori); } catch (Exception e) { LOG.error("can not format value: " + ori); return null; } }
From source file:org.pentaho.plugin.jfreereport.reportcharts.LogCategoryItemLabelGenerator.java
public static String formatValue(final Number number) { if (number == null) { return null; }/* w ww . j a v a 2s. c o m*/ final double rawValue = number.doubleValue(); final double value = Math.abs(rawValue); if (value < 1000.0) { return NumberFormat.getNumberInstance().format(rawValue); } else if (value < 1000000.0) { return NumberFormat.getNumberInstance().format(rawValue / 1000.0) + "K"; } else if (value < 1000000000.0) { return NumberFormat.getNumberInstance().format(rawValue / 1000000.0) + "M"; } else if (value < 1000000000000.0) { return NumberFormat.getNumberInstance().format(rawValue / 1000000000.0) + "B"; } else { return NumberFormat.getNumberInstance().format(rawValue / 1000000000000.0) + "T"; } }