Java Code Examples for java.text.DecimalFormat.setMinimumFractionDigits()

The following are Jave code examples for showing how to use setMinimumFractionDigits() of the java.text.DecimalFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: angel   File: StringUtils.java   Source Code and License Vote up 7 votes
public static String formatPercent(double done, int digits) {
  DecimalFormat percentFormat = new DecimalFormat("0.00%");
  double scale = Math.pow(10.0D, digits + 2);
  double rounded = Math.floor(done * scale);
  percentFormat.setDecimalSeparatorAlwaysShown(false);
  percentFormat.setMinimumFractionDigits(digits);
  percentFormat.setMaximumFractionDigits(digits);
  return percentFormat.format(rounded / scale);
}
 
Example 2
Project: jdk8u-jdk   File: NumberFormatProviderImpl.java   Source Code and License Vote up 6 votes
/**
 * Adjusts the minimum and maximum fraction digits to values that
 * are reasonable for the currency's default fraction digits.
 */
private static void adjustForCurrencyDefaultFractionDigits(
        DecimalFormat format, DecimalFormatSymbols symbols) {
    Currency currency = symbols.getCurrency();
    if (currency == null) {
        try {
            currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
        } catch (IllegalArgumentException e) {
        }
    }
    if (currency != null) {
        int digits = currency.getDefaultFractionDigits();
        if (digits != -1) {
            int oldMinDigits = format.getMinimumFractionDigits();
            // Common patterns are "#.##", "#.00", "#".
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == format.getMaximumFractionDigits()) {
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            } else {
                format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                format.setMaximumFractionDigits(digits);
            }
        }
    }
}
 
Example 3
Project: util4j   File: TestDecimalFormat.java   Source Code and License Vote up 6 votes
public static void test1(DecimalFormat df) {
	//默认显示3位小数
	double d = 1.5555555;
	System.out.println(df.format(d));//1.556
	//设置小数点后最大位数为5
	df.setMaximumFractionDigits(5);
	df.setMinimumIntegerDigits(15);
	System.out.println(df.format(d));//1.55556
	df.setMaximumFractionDigits(2);
	System.out.println(df.format(d));//1.56
	//设置小数点后最小位数,不够的时候补0
	df.setMinimumFractionDigits(10);
	System.out.println(df.format(d));//1.5555555500
	//设置整数部分最小长度为3,不够的时候补0
	df.setMinimumIntegerDigits(3);
	System.out.println(df.format(d));
	//设置整数部分的最大值为2,当超过的时候会从个位数开始取相应的位数
	df.setMaximumIntegerDigits(2);
	System.out.println(df.format(d));
}
 
Example 4
Project: openjdk-jdk10   File: NumberFormatProviderImpl.java   Source Code and License Vote up 6 votes
/**
 * Adjusts the minimum and maximum fraction digits to values that
 * are reasonable for the currency's default fraction digits.
 */
private static void adjustForCurrencyDefaultFractionDigits(
        DecimalFormat format, DecimalFormatSymbols symbols) {
    Currency currency = symbols.getCurrency();
    if (currency == null) {
        try {
            currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
        } catch (IllegalArgumentException e) {
        }
    }
    if (currency != null) {
        int digits = currency.getDefaultFractionDigits();
        if (digits != -1) {
            int oldMinDigits = format.getMinimumFractionDigits();
            // Common patterns are "#.##", "#.00", "#".
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == format.getMaximumFractionDigits()) {
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            } else {
                format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                format.setMaximumFractionDigits(digits);
            }
        }
    }
}
 
Example 5
Project: jaer   File: Matrix.java   Source Code and License Vote up 5 votes
/** Print the matrix to the output stream.   Line the elements up in
  * columns with a Fortran-like 'Fw.d' style format.
@param output Output stream.
@param w      Column width.
@param d      Number of digits after the decimal.
*/

public void print (PrintWriter output, int w, int d) {
   DecimalFormat format = new DecimalFormat();
   format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
   format.setMinimumIntegerDigits(1);
   format.setMaximumFractionDigits(d);
   format.setMinimumFractionDigits(d);
   format.setGroupingUsed(false);
   print(output,format,w+2);
}
 
Example 6
Project: yum   File: EmailService.java   Source Code and License Vote up 5 votes
@Transactional
public void sendOrderSummary(LocalDate date) {

    DailyMenu dailyMenu = dailyMenuRep.findByDate(date);
    if (dailyMenu != null) {

        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/yyyy");
        DateTimeFormatter fmtFull = DateTimeFormat.forPattern("EEEE dd MMMM yyyy");

        String formattedDate = date.toString(fmt);
        String titleDate = date.toString(fmtFull);
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);

        String currency = settingsRep.findById(1).getCurrency();

        //Build data
        HashMap<String, HashMap<Food, Integer>> orderByFoodType = getFoodByType(getOrderItems(dailyMenu));

        // create hashmap for the placeholders of the template
        Map<String, Object> model = new HashMap<>();
        model.put("date", titleDate);
        model.put("orderItemsByMain", orderByFoodType.get("MAIN"));
        model.put("orderItemsBySalad", orderByFoodType.get("SALAD"));
        model.put("orderItemsByDrink", orderByFoodType.get("DRINK"));
        model.put("currency", currency);

        List<DailyOrder> dailyOrders = dailyMenu.getDailyOrders();
        model.put("dailyOrders", dailyOrders);

        String emails = settingsRep.findById(1).getReportEmail();
        if (emails != null && !emails.isEmpty()) {
            ArrayList<String> emailsTo = new ArrayList<>(Arrays.asList(emails.split(";")));
            for (String emailTo : emailsTo) {
                sendHtmlTemplateEmail(emailTo, "[Yum] Order summary for " + formattedDate, model, "order-summary.ftlh");
            }
        }
    }
}
 
Example 7
Project: lams   File: CurrencyFormatter.java   Source Code and License Vote up 5 votes
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null && roundingModeOnDecimalFormat) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	return format;
}
 
Example 8
Project: okwallet   File: BtcFormat.java   Source Code and License Vote up 5 votes
/** Sets the number of fractional decimal places to be displayed on the given
 *  NumberFormat object to the value of the given integer.
 *  @return The minimum and maximum fractional places settings that the
 *          formatter had before this change, as an ImmutableList. */
private static ImmutableList<Integer> setFormatterDigits(DecimalFormat formatter, int min, int max) {
    ImmutableList<Integer> ante = ImmutableList.of(
        formatter.getMinimumFractionDigits(),
        formatter.getMaximumFractionDigits()
    );
    formatter.setMinimumFractionDigits(min);
    formatter.setMaximumFractionDigits(max);
    return ante;
}
 
Example 9
Project: Apriori   File: RuleSetTest.java   Source Code and License Vote up 5 votes
/**
 * Tests the functionality of the toString-method.
 */
@Test
public final void testToString() {
    ItemSet<NamedItem> body1 = new ItemSet<>();
    body1.add(new NamedItem("a"));
    body1.setSupport(0.5);
    ItemSet<NamedItem> body2 = new ItemSet<>();
    body2.add(new NamedItem("b"));
    body2.setSupport(0.8);
    AssociationRule<NamedItem> associationRule1 = new AssociationRule<>(body1, new ItemSet<>(),
            0.5);
    AssociationRule<NamedItem> associationRule2 = new AssociationRule<>(body2, new ItemSet<>(),
            0.7);
    RuleSet<NamedItem> ruleSet = new RuleSet<>(Sorting.forAssociationRules());
    ruleSet.add(associationRule1);
    ruleSet.add(associationRule2);
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setMinimumFractionDigits(1);
    decimalFormat.setMaximumFractionDigits(2);
    assertEquals("[" + associationRule1.toString() + " (support = " +
                    decimalFormat.format(new Support().evaluate(associationRule1)) + ", confidence = " +
                    decimalFormat.format(new Confidence().evaluate(associationRule1)) + ", lift = " +
                    decimalFormat.format(new Lift().evaluate(associationRule1)) + ", leverage = " +
                    decimalFormat.format(new Leverage().evaluate(associationRule1)) + "),\n"
                    + associationRule2.toString() + " (support = " +
                    decimalFormat.format(new Support().evaluate(associationRule2)) + ", confidence = " +
                    decimalFormat.format(new Confidence().evaluate(associationRule2)) + ", lift = " +
                    decimalFormat.format(new Lift().evaluate(associationRule2)) + ", leverage = " +
                    decimalFormat.format(new Leverage().evaluate(associationRule2)) + ")]",
            ruleSet.toString());
}
 
Example 10
Project: hanlpStudy   File: Matrix.java   Source Code and License Vote up 5 votes
/**
 * Print the matrix to the output stream.   Line the elements up in
 * columns with a Fortran-like 'Fw.d' style format.
 *
 * @param output Output stream.
 * @param w      Column width.
 * @param d      Number of digits after the decimal.
 */

public void print(PrintWriter output, int w, int d)
{
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(d);
    format.setMinimumFractionDigits(d);
    format.setGroupingUsed(false);
    print(output, format, w + 2);
}
 
Example 11
Project: MapAnalyst   File: Matrix.java   Source Code and License Vote up 5 votes
/** Print the matrix to the output stream.   Line the elements up in
  * columns with a Fortran-like 'Fw.d' style format.
@param output Output stream.
@param w      Column width.
@param d      Number of digits after the decimal.
*/

public void print (PrintWriter output, int w, int d) {
   DecimalFormat format = new DecimalFormat();
   format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
   format.setMinimumIntegerDigits(1);
   format.setMaximumFractionDigits(d);
   format.setMinimumFractionDigits(d);
   format.setGroupingUsed(false);
   print(output,format,w+2);
}
 
Example 12
Project: creacoinj   File: BtcFormat.java   Source Code and License Vote up 5 votes
/** Sets the number of fractional decimal places to be displayed on the given
 *  NumberFormat object to the value of the given integer.
 *  @return The minimum and maximum fractional places settings that the
 *          formatter had before this change, as an ImmutableList. */
private static ImmutableList<Integer> setFormatterDigits(DecimalFormat formatter, int min, int max) {
    ImmutableList<Integer> ante = ImmutableList.of(
        formatter.getMinimumFractionDigits(),
        formatter.getMaximumFractionDigits()
    );
    formatter.setMinimumFractionDigits(min);
    formatter.setMaximumFractionDigits(max);
    return ante;
}
 
Example 13
Project: FuelUp   File: VolumeUtil.java   Source Code and License Vote up 5 votes
public static String getFuelVolume(double value) {
    DecimalFormat bddf = new DecimalFormat();
    bddf.setGroupingUsed(false);
    bddf.setMaximumFractionDigits(2);
    bddf.setMinimumFractionDigits(0);

    return bddf.format(value);
}
 
Example 14
Project: FuelUp   File: VolumeUtil.java   Source Code and License Vote up 5 votes
public static DecimalFormat getFormatterForConsumption(String consumptionUnit) {
    DecimalFormat format = new DecimalFormat();

    if (MainActivity.getInstance().getString(R.string.units_mpg).equals(consumptionUnit)) {
        format.setMaximumFractionDigits(1);
        format.setMinimumFractionDigits(1);
    } else {
        format.setMaximumFractionDigits(2);
        format.setMinimumFractionDigits(2);
    }

    return format;
}
 
Example 15
Project: FuelUp   File: CurrencyUtil.java   Source Code and License Vote up 5 votes
private static String getPriceFormatted(double value, int coefficient, boolean isBefore, int fractionDigits, String symbol) {
    DecimalFormat bddf = new DecimalFormat();
    bddf.setMaximumFractionDigits(fractionDigits);
    bddf.setMinimumFractionDigits(fractionDigits);
    String price = bddf.format(value * coefficient);

    if (isBefore) {
        return symbol + " " + price;
    } else {
        return price + " " + symbol;
    }
}
 
Example 16
Project: FuelUp   File: ListFillUpsAdapter.java   Source Code and License Vote up 5 votes
public ListFillUpsAdapter(Callback callback, Vehicle vehicle) {
    super();

    this.mVehicle = vehicle;
    this.mCallback = callback;

    int consumptionFractionDigits = mVehicle.getDistanceUnit() == DistanceUnit.mi ? 1 : 2;
    consumptionFormat = new DecimalFormat();
    consumptionFormat.setGroupingUsed(false);
    consumptionFormat.setMinimumFractionDigits(consumptionFractionDigits);
    consumptionFormat.setMaximumFractionDigits(consumptionFractionDigits);
}
 
Example 17
Project: 5zig-TIMV-Plugin   File: KDRChangeItem.java   Source Code and License Vote up 5 votes
@Override
protected Object getValue(boolean dummy) {
	try{		
		
		DecimalFormat df = new DecimalFormat();
		df.setMaximumFractionDigits(2);
		df.setMinimumFractionDigits(2);
		
		return df.format(BED.gameKdr) + " (" + (BED.gameKdr - BED.apiKdr > 0 ? "+" + df.format(BED.gameKdr - BED.apiKdr) : df.format(BED.gameKdr - BED.apiKdr)) + ")";
	}catch(Exception e){
			e.printStackTrace();
			return "Server error";
	}
}
 
Example 18
Project: Apriori   File: RuleSet.java   Source Code and License Vote up 5 votes
@Override
public final String toString() {
    StringBuilder stringBuilder = new StringBuilder();
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setMinimumFractionDigits(1);
    decimalFormat.setMaximumFractionDigits(2);
    Iterator<AssociationRule<ItemType>> iterator = iterator();
    stringBuilder.append("[");

    while (iterator.hasNext()) {
        AssociationRule<ItemType> rule = iterator.next();
        stringBuilder.append(rule.toString());
        stringBuilder.append(" (support = ");
        stringBuilder.append(decimalFormat.format(new Support().evaluate(rule)));
        stringBuilder.append(", confidence = ");
        stringBuilder.append(decimalFormat.format(new Confidence().evaluate(rule)));
        stringBuilder.append(", lift = ");
        stringBuilder.append(decimalFormat.format(new Lift().evaluate(rule)));
        stringBuilder.append(", leverage = ");
        stringBuilder.append(decimalFormat.format(new Leverage().evaluate(rule)));
        stringBuilder.append(")");

        if (iterator.hasNext()) {
            stringBuilder.append(",\n");
        }
    }

    stringBuilder.append("]");
    return stringBuilder.toString();
}
 
Example 19
Project: financisto1-holo   File: CurrencyCache.java   Source Code and License Vote up 5 votes
public static DecimalFormat createCurrencyFormat(Currency c) {
	DecimalFormatSymbols dfs = new DecimalFormatSymbols();
	dfs.setDecimalSeparator(charOrEmpty(c.decimalSeparator, dfs.getDecimalSeparator()));
	dfs.setGroupingSeparator(charOrEmpty(c.groupSeparator, dfs.getGroupingSeparator()));
	dfs.setMonetaryDecimalSeparator(dfs.getDecimalSeparator());
	dfs.setCurrencySymbol(c.symbol);

	DecimalFormat df = new DecimalFormat("#,##0.00", dfs);
	df.setGroupingUsed(dfs.getGroupingSeparator() > 0);
	df.setMinimumFractionDigits(c.decimals);
	df.setMaximumFractionDigits(c.decimals);
	df.setDecimalSeparatorAlwaysShown(false);
	return df;
}
 
Example 20
Project: fiery   File: DependAPIStatistic.java   Source Code and License Vote up 4 votes
private String getPercent(int a, int b) {
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    return df.format(a * 100.00 / b) + "%";
}