Example usage for java.text DecimalFormat setParseIntegerOnly

List of usage examples for java.text DecimalFormat setParseIntegerOnly

Introduction

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

Prototype

public void setParseIntegerOnly(boolean value) 

Source Link

Document

Sets whether or not numbers should be parsed as integers only.

Usage

From source file:NumericTextField.java

public static void main(String[] args) {
    try {//from   w w  w.jav  a  2s  .  co  m
        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:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java

private int determineDepth(String depthString) throws KettleException {
    DecimalFormat df = new DecimalFormat("0");
    ParsePosition pp = new ParsePosition(0);
    df.setParseIntegerOnly(true);
    try {/*from w w w  .  j av a  2 s .  c o  m*/
        Number n = df.parse(depthString, pp);
        if (n == null) {
            return 1; // default
        }
        if (pp.getErrorIndex() == 0) {
            throw new KettleException("Unable to convert stored depth '" + depthString
                    + "' to depth at position " + pp.getErrorIndex());
        }
        return n.intValue();
    } catch (Exception e) {
        throw new KettleException("Unable to convert stored depth '" + depthString + "' to depth", e);
    }
}