Example usage for java.text NumberFormat getNumberInstance

List of usage examples for java.text NumberFormat getNumberInstance

Introduction

In this page you can find the example usage for java.text NumberFormat getNumberInstance.

Prototype

public static final NumberFormat getNumberInstance() 

Source Link

Document

Returns a general-purpose number format for the current default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

public static void main(String[] args) {
    NumberFormat numberFormatGuFalse = NumberFormat.getNumberInstance();
    numberFormatGuFalse.setGroupingUsed(false);
    JFormattedTextField jftFieldGuFalse = new JFormattedTextField(numberFormatGuFalse);

    NumberFormat numberFormatGuTrue = NumberFormat.getNumberInstance();
    // numberFormatGuFalse.setGroupingUsed(true); // not necessary as is default
    JFormattedTextField jftFieldGuTrue = new JFormattedTextField(numberFormatGuTrue);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(jftFieldGuFalse, BorderLayout.NORTH);
    panel.add(jftFieldGuTrue, BorderLayout.SOUTH);

    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);/*from   ww  w  .  j a  v  a 2  s.c om*/
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setParseIntegerOnly(false);
    double usersNumber = 1976.0826;

    numberFormat = NumberFormat.getNumberInstance();
    System.out.println("User's number (DEFAULT LOCALE): " + numberFormat.format(usersNumber));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(2);//ww  w  .  j a v  a  2 s. c  o m
    format.setMinimumFractionDigits(2);
    format.setParseIntegerOnly(true);
    format.setRoundingMode(RoundingMode.HALF_UP);

    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setMaximum(1000);
    formatter.setMinimum(0.0);
    formatter.setAllowsInvalid(false);
    // formatter.setOverwriteMode(false);

    JFormattedTextField tf = new JFormattedTextField(formatter);
    tf.setColumns(10);
    tf.setValue(123456789.99);
    JFormattedTextField tf1 = new JFormattedTextField(formatter);
    tf1.setValue(1234567890.99);
    JFormattedTextField tf2 = new JFormattedTextField(formatter);
    tf2.setValue(1111.1111);
    JFormattedTextField tf3 = new JFormattedTextField(formatter);
    tf3.setValue(-1111.1111);
    JFormattedTextField tf4 = new JFormattedTextField(formatter);
    tf4.setValue(-56);

    JFrame frame = new JFrame("Test");
    frame.setLayout(new GridLayout(5, 0));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tf);
    frame.add(tf1);
    frame.add(tf2);
    frame.add(tf3);
    frame.add(tf4);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String... args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();

    JFormattedTextField ftf = new JFormattedTextField(NumberFormat.getNumberInstance());
    ftf.setColumns(10);//from ww  w. j  a  v a  2 s  .  co m
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
    ftf.setValue(100);
    lastValidValue = "100";
    ftf.addCaretListener(e -> {
        System.out.println("Last Valid Value : " + lastValidValue);
        if (ftf.isEditValid()) {
            String latestValue = ftf.getText();
            System.out.println("Latest Value : " + latestValue);
            if (!(latestValue.equals(lastValidValue)))
                ftf.setBackground(Color.YELLOW.darker());
            else {
                lastValidValue = ftf.getText();
                ftf.setBackground(Color.WHITE);
            }
        } else {
            System.out.println("Invalid Edit Entered.");
        }
    });
    contentPane.add(ftf);
    frame.setContentPane(contentPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
    textField1.setFormatterFactory(new AbstractFormatterFactory() {
        @Override/*from w  w  w . j  a v  a2 s  . c  o  m*/
        public AbstractFormatter getFormatter(JFormattedTextField tf) {
            NumberFormat format = DecimalFormat.getInstance();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);
            format.setRoundingMode(RoundingMode.HALF_UP);
            InternationalFormatter formatter = new InternationalFormatter(format);
            formatter.setAllowsInvalid(false);
            formatter.setMinimum(0.0);
            formatter.setMaximum(1000.00);
            return formatter;
        }
    });
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setRoundingMode(RoundingMode.HALF_UP);
    final JFormattedTextField textField2 = new JFormattedTextField(numberFormat);
    textField2.setValue(new Float(10.01));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.SOUTH);
    frame.setVisible(true);
    frame.pack();
}

From source file:Main.java

public static String toNumberFormat(long value) {
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(0);/*from   www . ja va 2  s  .com*/
    return nf.format(value);
}

From source file:Main.java

public static String numFormat(int len, int num) {
    NumberFormat formatter = NumberFormat.getNumberInstance();
    formatter.setMinimumIntegerDigits(len);
    formatter.setGroupingUsed(false);//from  ww w . ja  va2 s .  c om
    String numStr = formatter.format(num);
    return numStr;
}

From source file:Main.java

public static String formatStringTwoDecPercent(float number) {
    NumberFormat formatter = NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(0);
    formatter.setMaximumFractionDigits(2);

    return String.valueOf(formatter.format(number) + "%");
}

From source file:Main.java

public static double String2AmountFloat(String str) {
    try {/*from  w ww. j  a v  a2  s.c  o m*/
        String tempStr = NumberFormat.getNumberInstance().format(Long.parseLong(str, 10)).replace(",", "");
        return Long.parseLong(tempStr) / 100.00;
    } catch (Exception e) {
        e.printStackTrace();
        return 0.00;
    }
}

From source file:Main.java

public static long String2AmountFloat4QPOS(String str) {
    try {//from   w  w  w .  ja va 2  s  . c om
        String tempStr = NumberFormat.getNumberInstance().format(Long.parseLong(str, 10)).replace(",", "");
        return Long.parseLong(tempStr);
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}