Example usage for java.text NumberFormat getInstance

List of usage examples for java.text NumberFormat getInstance

Introduction

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

Prototype

public static final NumberFormat getInstance() 

Source Link

Document

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

Usage

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(Locale.US);
    System.out.println("User's number (US): " + numberFormat.format(usersNumber));

}

From source file:ConstantLocaleUsage.java

public static void main(String[] argv) {

    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setParseIntegerOnly(false);
    double usersNumber;

    if (argv.length == 1)
        try {/*from w w w . j a  v  a  2 s.  co  m*/
            usersNumber = numberFormat.parse(argv[0]).doubleValue();
        } catch (ParseException e) {
            usersNumber = 197912.29;
        }
    else
        usersNumber = 1976.0826;

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

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(Locale.GERMANY);
    System.out.println("User's number (GERMANY): " + numberFormat.format(usersNumber));

}

From source file:MainClass.java

public static void main(String[] av) {

    String input = "4096.251";
    NumberFormat defForm = NumberFormat.getInstance();

    try {/*from   w  w w .j  a  v a2  s.  c  o  m*/
        Number d = defForm.parse(input);
        System.out.println(input + " parses as " + d + " and formats as " + defForm.format(d));
    } catch (ParseException pe) {
        System.err.println(input + "not parseable!");
    }
}

From source file:MainClass.java

public static void main(String[] argv) {
    double d = 1234567.89;
    double n = -1234567.89;

    String pattern = "#,###.##;(#,###.##)";

    NumberFormat nf = NumberFormat.getInstance();
    if (nf instanceof DecimalFormat) {
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern(pattern);//ww  w.j  av  a  2 s. c  o m
        System.out.println(df.format(d));
        System.out.println(df.format(n));
    }

}

From source file:MainClass.java

public static void main(String[] av) {
    double data[] = { 0, 1, 22d / 7, 100.2345678 };
    NumberFormat form = NumberFormat.getInstance();

    // Set it to look like 999.99[99]
    form.setMinimumIntegerDigits(3);/*www  .  j  a  v  a 2 s . c o  m*/
    form.setMinimumFractionDigits(2);
    form.setMaximumFractionDigits(4);

    // Now print using it.
    for (int i = 0; i < data.length; i++)
        System.out.println(data[i] + "\tformats as " + form.format(data[i]));
}

From source file:NumFormat2.java

/** The main (and only) method in this class. */
public static void main(String[] av) {
    // Get a format instance
    NumberFormat form = NumberFormat.getInstance();

    // Set it to look like 999.99[99]
    form.setMinimumIntegerDigits(3);/*from w  ww . jav a 2 s  . c o m*/
    form.setMinimumFractionDigits(2);
    form.setMaximumFractionDigits(4);

    // Now print using it.
    for (int i = 0; i < data.length; i++)
        System.out.println(data[i] + "\tformats as " + form.format(data[i]));
}

From source file:NumFormatParse.java

/** The main (and only) method in this class. */
public static void main(String[] av) {

    //+/*from w w  w. ja  v  a 2  s  .co m*/
    NumberFormat defForm = NumberFormat.getInstance();

    try {
        Number d = defForm.parse(input);
        System.out.println(input + " parses as " + d + " and formats as " + defForm.format(d));
    } catch (ParseException pe) {
        System.err.println(input + "not parseable!");
    }
    //-
}

From source file:NumberInputNumberFormatgetInstance.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Number Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Font font = new Font("SansSerif", Font.BOLD, 16);

    JLabel label;//from   w  ww.ja va2 s . c om
    JFormattedTextField input;
    JPanel panel;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    Format general = NumberFormat.getInstance();
    label = new JLabel("General/Instance:");
    input = new JFormattedTextField(general);
    input.setValue(2424.50);
    input.setColumns(20);
    input.setFont(font);

    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(label);
    panel.add(input);
    frame.add(panel);

    frame.add(new JTextField());
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    StopWatch clock = new StopWatch();
    NumberFormat format = NumberFormat.getInstance();

    System.out.println("How long does it take to take the sin of 0.34 ten million times?");
    clock.start();//from ww w  .  j  a va 2 s . co m
    for (int i = 0; i < 100000000; i++) {
        Math.sin(0.34);
    }
    clock.stop();

    System.out.println("It takes " + clock.getTime() + " milliseconds");

    System.out.println("How long does it take to multiply 2 doubles one billion times?");
    clock.reset();
    clock.start();
    for (int i = 0; i < 1000000000; i++) {
        double result = 3423.2234 * 23e-4;
    }
    clock.stop();
    System.out.println("It takes " + clock.getTime() + " milliseconds.");

    System.out.println("How long does it take to add 2 ints one billion times?");
    clock.reset();
    clock.start();
    for (int i = 0; i < 1000000000; i++) {
        int result = 293842923 + 33382922;
    }
    clock.stop();
    System.out.println("It takes " + clock.getTime() + " milliseconds.");

    System.out.println("Testing the split() method.");
    clock.reset();
    clock.start();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    clock.split();
    System.out.println("Split Time after 1 sec: " + clock.getTime());
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    System.out.println("Split Time after 2 sec: " + clock.getTime());
    clock.unsplit();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    System.out.println("Time after 3 sec: " + clock.getTime());

}