Example usage for java.text DecimalFormat setGroupingSize

List of usage examples for java.text DecimalFormat setGroupingSize

Introduction

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

Prototype

public void setGroupingSize(int newValue) 

Source Link

Document

Set the grouping size.

Usage

From source file:Main.java

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

    format.setGroupingSize(4);

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

}

From source file:DecimalFormatDemo.java

static public void main(String[] args) {

    customFormat("###,###.###", 123456.789);
    customFormat("###.##", 123456.789);
    customFormat("000000.000", 123.78);
    customFormat("$###,###.###", 12345.67);
    customFormat("\u00a5###,###.###", 12345.67);

    Locale currentLocale = new Locale("en", "US");

    DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
    unusualSymbols.setDecimalSeparator('|');
    unusualSymbols.setGroupingSeparator('^');
    String strange = "#,##0.###";
    DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
    weirdFormatter.setGroupingSize(4);
    String bizarre = weirdFormatter.format(12345.678);
    System.out.println(bizarre);//  w w w  . j av  a  2  s  .c  o  m

    Locale[] locales = { new Locale("en", "US"), new Locale("de", "DE"), new Locale("fr", "FR") };

    for (int i = 0; i < locales.length; i++) {
        localizedFormat("###,###.###", 123456.789, locales[i]);
    }

}

From source file:NumericTextField.java

public static void main(String[] args) {
    try {/*from  w w w .j av a2s.  com*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    DecimalFormat format = new DecimalFormat("#,###.###");
    format.setGroupingUsed(true);
    format.setGroupingSize(3);
    format.setParseIntegerOnly(false);

    JFrame f = new JFrame("Numeric Text Field Example");
    final NumericTextField tf = new NumericTextField(10, format);

    tf.setValue((double) 123456.789);

    JLabel lbl = new JLabel("Type a number: ");
    f.getContentPane().add(tf, "East");
    f.getContentPane().add(lbl, "West");

    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            try {
                tf.normalize();
                Long l = tf.getLongValue();
                System.out.println("Value is (Long)" + l);
            } catch (ParseException e1) {
                try {
                    Double d = tf.getDoubleValue();
                    System.out.println("Value is (Double)" + d);
                } catch (ParseException e2) {
                    System.out.println(e2);
                }
            }
        }
    });
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static String kgToLBoz(float paramFloat) {
    float f1 = (32768.0F + 23117.0F * paramFloat) / 65536.0F;
    int i = (int) (f1 * 0.1F) / 16;
    float f2 = f1 * 0.1F % 16.0F;
    DecimalFormat localDecimalFormat = new DecimalFormat();
    localDecimalFormat.setMaximumFractionDigits(1);
    localDecimalFormat.setGroupingSize(0);
    localDecimalFormat.setRoundingMode(RoundingMode.FLOOR);
    return i + ":" + localDecimalFormat.format(f2);
}

From source file:Main.java

public static String kgToLB_ForBodyScale(float paramFloat) {
    int i = (int) Math.floor(1.0F + (32768.0F + 144479.0F * (paramFloat * 10.0F)) / 65536.0F);
    if (i % 2 != 0) {
        i--;/*  w  w  w.java 2s  . co  m*/
    }
    DecimalFormat localDecimalFormat = new DecimalFormat();
    localDecimalFormat.setMaximumFractionDigits(1);
    localDecimalFormat.setGroupingSize(0);
    localDecimalFormat.setRoundingMode(RoundingMode.FLOOR);
    return localDecimalFormat.format(0.1D * i);
}

From source file:com.tinypace.mobistore.util.StringUtil.java

public static String formatMoney(BigDecimal d) {
    DecimalFormat mformat = new DecimalFormat();
    mformat.setMaximumFractionDigits(2);
    mformat.setMinimumFractionDigits(2);
    mformat.setGroupingSize(20);
    return mformat.format(d);
}

From source file:com.itude.mobile.mobbl.core.util.StringUtilities.java

public static String formatVolume(String stringToFormat) {
    if (stringToFormat == null || stringToFormat.length() == 0) {
        return null;
    }/*from  w w  w  . ja  va2s  .  c  om*/

    String result = null;

    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(getDefaultFormattingLocale()));

    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
    formatter.setMaximumFractionDigits(0);

    result = formatter.format(Double.parseDouble(stringToFormat));

    return result;
}

From source file:com.itude.mobile.android.util.StringUtil.java

/**
 * Format {@link String} as a Volume/*from  w w  w  .  j av  a2 s.  c  o  m*/
 *  
 * WARNING: Only use this method to present data to the screen
 * 
 * @param locale {@link Locale}
 * @param stringToFormat  {@link String} to format
 * @return a string formatted as a volume with group separators (eg, 131.224.000) assuming the receiver is an int string read from XML
 */
public static String formatVolume(Locale locale, String stringToFormat) {
    if (stringToFormat == null || stringToFormat.length() == 0) {
        return null;
    }

    String result = null;

    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));

    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
    formatter.setMaximumFractionDigits(0);

    result = formatter.format(Double.parseDouble(stringToFormat));

    return result;
}

From source file:com.itude.mobile.mobbl.core.util.StringUtilities.java

private static void setupFormatter(DecimalFormat formatter, int minumumDecimals, int maximumDecimals) {
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(getDefaultFormattingLocale()));
    formatter.setMinimumIntegerDigits(1);

    if (minumumDecimals > -1) {
        formatter.setMinimumFractionDigits(minumumDecimals);
    }//from   w ww. ja  v a 2s .  co  m
    if (maximumDecimals > -1) {
        formatter.setMaximumFractionDigits(maximumDecimals);
    }
    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
}

From source file:com.itude.mobile.android.util.StringUtil.java

/**
 * Set the formatter./*from w ww  .ja v a 2s .  c om*/
 * 
 * @param formatter {@link DecimalFormat}
 * @param locale {@link Locale}
 * @param minimalDecimalNumbers minimal number of decimals
 * @param maximumDecimalNumbers maximum number of decimals
 */
private static void setupFormatter(DecimalFormat formatter, Locale locale, int minimalDecimalNumbers,
        int maximumDecimalNumbers) {
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
    formatter.setMinimumIntegerDigits(1);
    formatter.setMinimumFractionDigits(minimalDecimalNumbers);
    formatter.setMaximumFractionDigits(maximumDecimalNumbers);
    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
}