Example usage for java.text DecimalFormat setMinimumFractionDigits

List of usage examples for java.text DecimalFormat setMinimumFractionDigits

Introduction

In this page you can find the example usage for java.text DecimalFormat setMinimumFractionDigits.

Prototype

@Override
public void setMinimumFractionDigits(int newValue) 

Source Link

Document

Sets the minimum number of digits allowed in the fraction portion of a number.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();

    format.setMinimumFractionDigits(4);

    System.out.println(format.format(123456789.12345678));

}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat format = new DecimalFormat("####.##");
    format.setMinimumFractionDigits(2);
    final JFormattedTextField field1 = new JFormattedTextField(format);
    final JFormattedTextField field2 = new JFormattedTextField(format);
    field1.setColumns(15);//from  w  ww  .  ja v  a 2s . c o m
    field2.setColumns(15);
    JButton btn = new JButton(new AbstractAction("Multiply by 2") {

        @Override
        public void actionPerformed(ActionEvent e) {
            Number value = (Number) field1.getValue();
            if (value != null) {
                field2.setValue(2 * value.doubleValue());
            }
        }
    });

    JPanel panel = new JPanel();
    panel.add(field1);
    panel.add(btn);
    panel.add(field2);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static String formatNum(double value) {
    String retValue = null;//from   w  w  w  . ja  v  a 2s  . com
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(0);
    df.setMaximumFractionDigits(2);
    retValue = df.format(value);
    retValue = retValue.replaceAll(",", "");
    return retValue;
}

From source file:Main.java

public static String formatCurrency(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);/*w  w w  . j  a v a  2  s  .  c o  m*/
    return df.format(amount);
}

From source file:Main.java

public static String formatNumber(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);//from w  w w  .j  a  v  a2s .c o m
    return df.format(amount);
}

From source file:Main.java

public static String MybigdecimalToString(BigDecimal myBigD) {
    myBigD = myBigD.setScale(2);//from w  ww .  ja va  2s.c  o  m
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(0);
    return df.format(myBigD);
}

From source file:Main.java

public static String converSizeToString(long size) {
    String ext[] = { "B", "KB", "MB", "G", "T", "P" };
    int i = 0;//ww  w  . jav  a  2 s .c  om
    long duration = size;
    while (duration >= 1024) {
        System.out.println("duration=" + duration + "  i=" + i);
        duration = duration >> 10;
        i++;
        System.out.println("duration=" + duration + "  i=" + i);
    }
    double d = Math.pow(2, 10 * (i));
    java.text.DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    return df.format(size / d) + ext[i];
}

From source file:Main.java

public static String formatPercentWithFixedFractionDigit(BigDecimal number, int fractionDigit) {
    String result = "0";

    if (number != null) {
        number = number.multiply(new BigDecimal("100"));
        DecimalFormat formatter = new DecimalFormat();
        formatter.setMaximumFractionDigits(fractionDigit);
        formatter.setMinimumFractionDigits(fractionDigit);

        result = formatter.format(number);
    }//from  w  w  w  .java  2  s.  c o  m
    return result;
}

From source file:Main.java

public static DecimalFormat createDecimalFormat(int minInt, int maxInt, int minFract, int maxFract,
        char separator, RoundingMode mode) {

    DecimalFormat format = (DecimalFormat) DecimalFormat.getNumberInstance();
    format.setRoundingMode(mode);/*  ww  w.  j av  a2 s .c  o  m*/

    format.setMaximumFractionDigits(maxFract);
    format.setMinimumFractionDigits(minFract);
    format.setMaximumIntegerDigits(maxInt);
    format.setMinimumIntegerDigits(minInt);
    DecimalFormatSymbols decimalSymbolComma = new DecimalFormatSymbols();
    decimalSymbolComma.setDecimalSeparator(separator);
    format.setDecimalFormatSymbols(decimalSymbolComma);
    format.setGroupingUsed(false);

    return format;
}

From source file:rsa_matrices.Helpers.java

public static void printMatrix(double[][] matrix, String matrixName) {
    int MAXIMUM_ALLOWED_COUNT = 10;
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);//from w  w  w  . ja v a  2  s .  com
    df.setMinimumFractionDigits(2);

    System.out.println(matrixName);
    for (int row = 0; row < matrix.length && row < MAXIMUM_ALLOWED_COUNT; row++) {
        for (int col = 0; col < matrix[0].length && col < MAXIMUM_ALLOWED_COUNT; col++) {
            System.out.print("  " + df.format(matrix[row][col]));
        }
        if (matrix[0].length > MAXIMUM_ALLOWED_COUNT) {
            System.out.print(" ... ");
        }
        System.out.println();
    }
    if (matrix.length > MAXIMUM_ALLOWED_COUNT) {
        System.out.print("  ... ... ... ");
    }
    System.out.println();
}