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 NumberFormat getInstance(Locale inLocale) 

Source Link

Document

Returns a general-purpose number format for the specified locale.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.out.println(NumberFormat.getInstance(new Locale("us")).format(12345.12345));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.CANADA);
    System.out.println(numberFormat.getMaximumFractionDigits());
}

From source file:NumFormat.java

public static void main(String[] av) {
    int data[] = { 100, 1000, 10000, 1000000 };
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    for (int i = 0; i < data.length; ++i) {
        System.out.println(data[i] + "\t" + nf.format(data[i]));
    }//from   w w w.  j a  va2  s  . c  o m
}

From source file:msi.gaml.operators.Maths.java

public static void main(final String[] args) throws ParseException {
    java.lang.System.out.println("Various format tests");
    java.lang.System.out.println("NumberFormat.parse1e1 = " + NumberFormat.getInstance(Locale.US).parse("1e1"));
    java.lang.System.out.println("Double.parse 1e1 = " + Double.parseDouble("1e1"));
    java.lang.System.out/* ww  w.  j  a  v  a 2  s .  co  m*/
            .println("NumberFormat.parse 1E1 = " + NumberFormat.getInstance(Locale.US).parse("1E1"));
    java.lang.System.out.println("Double.parse 1E1 = " + Double.parseDouble("1E1"));
    java.lang.System.out
            .println("NumberFormat.parse 1.0e1 = " + NumberFormat.getInstance(Locale.US).parse("1.0e1"));
    java.lang.System.out.println("Double.parse 1.0e1 = " + Double.parseDouble("1.0e1"));
    java.lang.System.out.println(
            "NumberFormat.parse 0.001E+10 = " + NumberFormat.getInstance(Locale.US).parse("0.001E+10"));
    java.lang.System.out.println("Double.parse 0.001E+10 = " + Double.parseDouble("0.001E+10"));
    java.lang.System.out.println(
            "NumberFormat.parse 0.001E-10 = " + NumberFormat.getInstance(Locale.US).parse("0.001E-10"));
    java.lang.System.out.println("Double.parse 0.001E-10 = " + Double.parseDouble("0.001E-10"));
    java.lang.System.out
            .println("NumberFormat.parse 0.001e-10 =" + NumberFormat.getInstance(Locale.US).parse("0.001e-10"));
    java.lang.System.out.println("Double.parse 0.001e-10 = " + Double.parseDouble("0.001e-10"));
    java.lang.System.out.println("Various arithmetic tests");
    java.lang.System.out.println("cos(PI) = " + Maths.cos_rad(PI));
    java.lang.System.out.println("sin_rad(0.0) = " + sin_rad(0.0));
    java.lang.System.out.println("tan_rad(0.0) = " + tan_rad(0.0));
    java.lang.System.out.println("sin(360) = " + sin(360));
    java.lang.System.out.println("sin(-720) = " + sin(-720));
    java.lang.System.out.println("sin(360.0) = " + sin(360.0));
    java.lang.System.out.println("sin(-720.0) = " + sin(-720.0));
    java.lang.System.out.println("sin(90) = " + sin(90));
    java.lang.System.out.println("sin(45) = " + sin(45));
    java.lang.System.out.println("sin(0) = " + sin(0));
    java.lang.System.out.println("sin(135) = " + sin(135));

    java.lang.System.out.println("Math.sin(360.0) = " + Math.sin(2 * Math.PI));
    // java.lang.System.out.println("3.0 = 3" + (3d == 3));
    // java.lang.System.out.println("3.0 != 3" + (3d != 3));
    java.lang.System.out.println("floor and ceil 2.7 " + floor(2.7) + " and " + ceil(2.7));
    java.lang.System.out.println("floor and ceil -2.7 " + floor(-2.7) + " and " + ceil(-2.7));
    java.lang.System.out.println("floor and ceil -2 " + floor(-2) + " and " + ceil(-2));
    java.lang.System.out.println("floor and ceil 3 " + floor(3) + " and " + ceil(3));
    double atan2diff = 0;
    double atan2diff2 = 0;
    Random rand = new Random();
    long s1 = 0;
    long t1 = 0;
    long t2 = 0;
    long t3 = 0;
    // for ( int i = 0; i < 10000000; i++ ) {
    // double x = rand.nextDouble();
    // double y = rand.nextDouble();
    // s1 = java.lang.System.currentTimeMillis();
    // double a1 = Math.atan2(x, y);
    // t1 += java.lang.System.currentTimeMillis() - s1;
    // s1 = java.lang.System.currentTimeMillis();
    // double a2 = FastMath.atan2(x, y);
    // t2 += java.lang.System.currentTimeMillis() - s1;
    // s1 = java.lang.System.currentTimeMillis();
    // double a3 = Maths.atan2Opt2(x, y);
    // t3 += java.lang.System.currentTimeMillis() - s1;
    //
    // atan2diff += Math.abs(a1 - a2);
    // atan2diff2 += Math.abs(a1 - a3);
    // }
    // java.lang.System.out.println("atan2diff : " + atan2diff + "  atan2diff2 : " + atan2diff2 + " t1 : " + t1 +
    // " t2 : " + t2 + " t3 : " + t3);

    long t4 = 0;
    long t5 = 0;
    long t6 = 0;
    double distDiff1 = 0;
    double distDiff2 = 0;
    for (int i = 0; i < 1000000; i++) {
        double x1 = rand.nextDouble();
        double y1 = rand.nextDouble();
        double x2 = rand.nextDouble();
        double y2 = rand.nextDouble();
        Coordinate c1 = new Coordinate(x1, y1);
        Coordinate c2 = new Coordinate(x2, y2);

        s1 = java.lang.System.currentTimeMillis();
        double a1 = Math.hypot(x2 - x1, y2 - y1);
        t4 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        double a2 = FastMath.hypot(x2 - x1, y2 - y1);
        t5 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        double a3 = c1.distance(c2);
        t6 += java.lang.System.currentTimeMillis() - s1;
        distDiff1 += Math.abs(a1 - a2);
        distDiff2 += Math.abs(a1 - a3);
    }
    java.lang.System.out.println("distDiff1 : " + distDiff1 + "  distDiff2 : " + distDiff2 + " t4 : " + t4
            + " t5 : " + t5 + " t6 : " + t6);

    long t7 = 0;
    long t8 = 0;
    distDiff1 = 0;

    for (int i = 0; i < 1000000; i++) {
        double a1, a2;
        double x1 = rand.nextDouble();
        double x2 = rand.nextDouble();
        double y1 = rand.nextDouble();
        double y2 = rand.nextDouble();
        double z1 = 0.0;
        double z2 = 0.0;
        GamaPoint c2 = new GamaPoint(x2, y2, z2);

        s1 = java.lang.System.currentTimeMillis();
        if (z1 == 0d && c2.getZ() == 0d) {
            a1 = hypot(x1, x2, y1, y2);
        } else {
            a1 = hypot(x1, x2, y1, y2, z1, z2);
        }
        t7 += java.lang.System.currentTimeMillis() - s1;
        s1 = java.lang.System.currentTimeMillis();
        a2 = hypot(x1, x2, y1, y2, z1, c2.getZ());
        t8 += java.lang.System.currentTimeMillis() - s1;
        distDiff1 += Math.abs(a1 - a2);
    }
    java.lang.System.out.println(
            "with 0.0 check : " + t7 + "  with direct 3 parameters call : " + t8 + " distance : " + distDiff1);
    // java.lang.System.out.println("Infinity to int:" + (int) Double.POSITIVE_INFINITY);
    // java.lang.System.out.println("NaN to int:" + (int) Double.NaN);
    // GuiUtils.debug("(int) (1.0/0.0):" + (int) (1.0 / 0.0));
    // GuiUtils.debug("(int) (1.0/0):" + (int) (1.0 / 0));
    // GuiUtils.debug("(int) (1.0/0d):" + (int) (1 / 0d));
    // GuiUtils.debug("(int) (1/0):" + 1 / 0);
}

From source file:Main.java

public static String getPageFileName(int p) {
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    nf.setMinimumIntegerDigits(3);/*from w  w w .  j  a v  a2 s .c o  m*/
    return "page" + nf.format(p) + ".png";
}

From source file:Main.java

public static String setLocaleNumberFormat(Locale locale, Number number) {

    NumberFormat formatter = NumberFormat.getInstance(locale);
    formatter.setMaximumFractionDigits(4);
    String localeFormattedNumber = formatter.format(number);
    return localeFormattedNumber;

}

From source file:Main.java

public static String roundWithTwoDecimals(double number) {
    try {/*from  www . j  a  v  a  2  s  .  c o  m*/
        // format based on default locale
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        df.setDecimalFormatSymbols(dfs);
        df.applyPattern("#####0.00");
        return df.format(number);
    } catch (IllegalArgumentException e) {
        return null;
    }
}

From source file:Main.java

public static int str2int(String s, int defVal) {
    if (s.equals("N/A"))
        return defVal;
    try {/*from w  ww .  j  av  a  2s .c o m*/
        s = s.trim();
        s = s.contains(" ") ? s.split(" ")[0] : s;
        return NumberFormat.getInstance(Locale.ENGLISH).parse(s).intValue();
    } catch (Exception err) {
        Log.e("str2int", s, err);
        return defVal;
    }
}

From source file:Main.java

private static String createStandardNumber(Object input) {
    // MOD SeB 11/03/2010 bug 11751 : convert number to String using current locale
    // MOD yyi 2010-04-15 bug 12483 : Unify the decimal format as US.
    return NumberFormat.getInstance(Locale.US).format(input);
}

From source file:Test.java

static void displayLocalizedData(Locale l, long number, Date date) {
    NumberFormat nf = NumberFormat.getInstance(l);
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, l);
    System.out.printf("Locale: %s\nNumber: %s\nDate: %s\n\n", l.getDisplayName(), nf.format(number),
            df.format(date));//from w  ww  . j  a  v a  2 s.  c  om
}