Format number to given decimal places (from-to) - Java java.lang

Java examples for java.lang:double Format

Description

Format number to given decimal places (from-to)

Demo Code


//package com.java2s;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class Main {
    /**//from  w  ww  .j  av a2s.com
     * Format number to given decimal places (from-to)
     *
     * @param value the value
     * @param minDecimals the decimals from
     * @param maxDecimals the decimals to
     * @return the string
     */
    public static String formatNumberDecimalsMinMax(Number value,
            int minDecimals, int maxDecimals) {
        DecimalFormat df = new DecimalFormat("0.00",
                new DecimalFormatSymbols(new Locale("nl", "BE")));
        df.setMinimumFractionDigits(minDecimals);
        df.setMaximumFractionDigits(maxDecimals);
        return df.format(value);
    }
}

Related Tutorials