List of usage examples for java.text NumberFormat format
public final String format(long number)
From source file:Main.java
public static String formatNumber(double amount, int precision, Locale locale) { NumberFormat nf = NumberFormat.getNumberInstance(locale); nf.setMinimumFractionDigits(precision); nf.setMaximumFractionDigits(precision); return nf.format(amount); }
From source file:Main.java
/** * Formats decimal number for specified locale * * @param v Value to format/*from ww w . ja va 2s .co m*/ * @param locale Locale * @return Formatted string */ public static String formatBigDecimal(BigDecimal v, Locale locale) { NumberFormat numberFormat = NumberFormat.getInstance(locale); int scale = 2; if (v.intValue() >= 100) { scale = 1; } return numberFormat.format(v.setScale(scale, RoundingMode.UP)); }
From source file:Main.java
public static String formatCurrency(double amount, int precision, Locale locale) { NumberFormat nf = NumberFormat.getCurrencyInstance(locale); nf.setMinimumFractionDigits(precision); nf.setMaximumFractionDigits(precision); return nf.format(amount); }
From source file:NumberFormatDemo.java
static public void displayNumber(Locale currentLocale) { Integer quantity = new Integer(123456); Double amount = new Double(345987.246); NumberFormat numberFormatter; String quantityOut;/*from w ww. j a va2s .com*/ String amountOut; numberFormatter = NumberFormat.getNumberInstance(currentLocale); quantityOut = numberFormatter.format(quantity); amountOut = numberFormatter.format(amount); System.out.println(quantityOut + " " + currentLocale.toString()); System.out.println(amountOut + " " + currentLocale.toString()); }
From source file:Main.java
public static String formatMoneyAmountOne(double amount) { Locale locale = new Locale("ru", "RU"); NumberFormat format = NumberFormat.getCurrencyInstance(locale); format.setMinimumFractionDigits(0);//from www . java 2 s .c o m format.setMaximumFractionDigits(1); String amountTxt = format.format(amount); String amountTxtValue = ""; for (int i = 0; i < amountTxt.length(); i++) { if (Character.isDigit(amountTxt.charAt(i)) || amountTxt.charAt(i) == '.' || amountTxt.charAt(i) == ',') amountTxtValue = amountTxtValue + amountTxt.charAt(i); } amountTxt = amountTxtValue; if (amountTxt.endsWith(",00")) return (amountTxt.replace(",00", "")); else if (amountTxt.endsWith("0") && amountTxt.contains(",")) { return (amountTxt.substring(0, amountTxt.length() - 1)); } else return (amountTxt); }
From source file:Main.java
public static String formatValue(double value) { if (value > 0) { int power; String suffix = " kmbt"; String formattedNumber = ""; NumberFormat formatter = new DecimalFormat("#,###.#"); power = (int) StrictMath.log10(value); value = value / (Math.pow(10, (power / 3) * 3)); formattedNumber = formatter.format(value); formattedNumber = formattedNumber + suffix.charAt(power / 3); return formattedNumber.length() > 4 ? formattedNumber.replaceAll("\\.[0-9]+", "") : formattedNumber; } else {// w ww. ja va 2 s. c om return "0"; } }
From source file:Main.java
public static String doubleToString(double d, int df) { String res = ""; NumberFormat form; form = NumberFormat.getInstance(Locale.US); form.setMaximumFractionDigits(df);//w ww. j a va 2s. com form.setGroupingUsed(false); try { res = form.format(d); } catch (IllegalArgumentException e) { res = ""; } return res; }
From source file:Main.java
public static String formatMoneyAmountItemised(double amount, Locale locale) { NumberFormat format = NumberFormat.getCurrencyInstance(locale); format.setMinimumFractionDigits(0);/*from w w w . j a v a 2 s . com*/ format.setMaximumFractionDigits(3); String amountTxt = format.format(amount); String amountTxtValue = ""; for (int i = 0; i < amountTxt.length(); i++) { if (Character.isDigit(amountTxt.charAt(i)) || amountTxt.charAt(i) == '.' || amountTxt.charAt(i) == ',') amountTxtValue = amountTxtValue + amountTxt.charAt(i); } amountTxt = amountTxtValue; if (amountTxt.endsWith(",0")) return (amountTxt.replace(",0", "")); else if (amountTxt.endsWith(",00")) return (amountTxt.replace(",00", "")); else if (amountTxt.endsWith(",000")) return (amountTxt.replace(",000", "")); else return (amountTxt); }
From source file:Main.java
/** * Normalize decimal string//from w ww. j a v a 2s . c o 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:MemoryUtils.java
public static StringBuffer getMemoryInfo() { StringBuffer buffer = new StringBuffer(); freeMemory();//from w w w.java 2 s.co m Runtime runtime = Runtime.getRuntime(); double usedMemory = usedMemory(runtime); double maxMemory = maxMemory(runtime); NumberFormat f = new DecimalFormat("###,##0.0"); String lineSeparator = System.getProperty("line.separator"); buffer.append("Used memory: " + f.format(usedMemory) + "MB").append(lineSeparator); buffer.append("Max available memory: " + f.format(maxMemory) + "MB").append(lineSeparator); return buffer; }