List of usage examples for java.text NumberFormat setMinimumFractionDigits
public void setMinimumFractionDigits(int newValue)
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); format.setMaximumFractionDigits(1);/*from ww w.j a v a 2 s . c om*/ 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 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:Main.java
public static String getNumberWithFractionDigit(double value, int fracLength) { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(fracLength); nf.setMinimumFractionDigits(fracLength); return nf.format(value); }
From source file:Main.java
public static String formatMoneyAmountItemised(double amount, Locale locale) { NumberFormat format = NumberFormat.getCurrencyInstance(locale); format.setMinimumFractionDigits(0); format.setMaximumFractionDigits(3);/*from w ww . ja v a 2 s.c o m*/ 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
/** * /*w ww. j av a2s.c o m*/ * @param value * @param min_fraction_digit * @param max_fraction_digit * @return */ public static String formatNumber(double value, int min_fraction_digit, int max_fraction_digit) { NumberFormat nf; nf = NumberFormat.getInstance(); if (min_fraction_digit != 0) { nf.setMinimumFractionDigits(min_fraction_digit); } if (max_fraction_digit != 0) { nf.setMaximumFractionDigits(max_fraction_digit); } return nf.format(value); }
From source file:org.jpos.util.QIUtils.java
public static Renderer createAmountRenderer() { NumberFormat amountFormat = NumberFormat.getInstance(); amountFormat.setGroupingUsed(true);/*w ww .j av a 2s . co m*/ amountFormat.setMinimumFractionDigits(2); return new NumberRenderer(amountFormat); }
From source file:Main.java
public static String sGetDecimalStringAnyLocaleAs1Pt5LocalisedString(String value) { Locale theLocale = Locale.getDefault(); NumberFormat numberFormat = DecimalFormat.getInstance(theLocale); Number theNumber;/*from w w w. j ava2 s . c o m*/ try { theNumber = numberFormat.parse(value); } catch (ParseException e) { //value = sConvertDigits(value); String valueWithDot = value.replaceAll(",", "."); theNumber = Double.valueOf(valueWithDot); } // String.format uses the JVM's default locale. //String s = String.format(Locale.US, "%.2f", price); NumberFormat outputFormat = DecimalFormat.getInstance(theLocale); outputFormat.setMinimumIntegerDigits(1); outputFormat.setMinimumFractionDigits(5); outputFormat.setMaximumFractionDigits(5); String theStringResult = outputFormat.format(theNumber); //String theStringResult = String.format("%1.5f", theDoubleValue.doubleValue()); return theStringResult; }
From source file:Main.java
/** * Normalize decimal string//from w w w . j a v a 2 s.com * 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:com.actinarium.kinetic.pipeline.CodeGenerator.java
/** * Generates Java code for a table lookup interpolator based on provided values * * @param packageName package name to write into the template * @param className class name to write into the template * @param sourceName title of the measurement the values were taken from * @param values an array of float values that must be recorded at equal intervals * @return generated drop-in Java code/*w w w.jav a2 s . c o m*/ */ public static String generateInterpolatorCode(String packageName, String className, String sourceName, float[] values) { NumberFormat format = DecimalFormat.getNumberInstance(Locale.ROOT); format.setMinimumFractionDigits(4); format.setMaximumFractionDigits(4); StringBuilder valuesBuilder = new StringBuilder(CHARS_PER_LINE * (values.length / VALUES_PER_ROW + 1)); // Append all values but the last one final int lengthMinusOne = values.length - 1; for (int i = 0; i < lengthMinusOne; /* incremented in loop body */) { if (values[i] > 0) { // Append space before positive numbers to align with those having minus sign valuesBuilder.append(' '); } valuesBuilder.append(format.format(values[i])).append('f').append(','); if (++i % VALUES_PER_ROW == 0) { valuesBuilder.append("\n "); } else { valuesBuilder.append(' '); } } // Append last value valuesBuilder.append(format.format(values[lengthMinusOne])).append('f'); // and generate Java code out of the given template return String.format(TABLE_LOOKUP_TEMPLATE, packageName, className, sourceName, valuesBuilder.toString()); }
From source file:com.norbl.util.StringUtil.java
public static String f1(double x) { NumberFormat f = NumberFormat.getInstance(); f.setMaximumFractionDigits(1);//from w w w .ja v a 2 s .com f.setMinimumFractionDigits(1); return (f.format(x)); }