Example usage for java.text NumberFormat format

List of usage examples for java.text NumberFormat format

Introduction

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

Prototype

public final String format(long number) 

Source Link

Document

Specialization of format.

Usage

From source file:Main.java

public static void main(String[] args) {
    double value = 9.01236789E9;
    String text = String.format("%.0f", value);
    System.out.println(text); // 9012367890

    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(0);//w ww  .  j  a  v  a2 s.c  o  m
    format.setGroupingUsed(false);
    System.out.println(format.format(value)); // 9012367890
}

From source file:Main.java

public static void main(String args[]) {
    NumberFormat formatter = new DecimalFormat();

    int maxinteger = Integer.MAX_VALUE;

    formatter = new DecimalFormat("0.######E0");
    System.out.println(formatter.format(maxinteger));

}

From source file:Main.java

public static void main(String[] args) {
    NumberFormat formatter = new DecimalFormat();
    Locale locale = Locale.getDefault();
    System.out.println("Locale = " + locale);
    System.out.println(formatter.format(123.123));

    locale = Locale.ITALY;//from www.  ja va2  s .  c  o m
    Locale.setDefault(locale);
    formatter = new DecimalFormat();
    System.out.println("Locale = " + locale);
    System.out.println(formatter.format(123.123));
}

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  ava  2  s. c  om
        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:NumFormatParse.java

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

    //+//w  ww. j a v  a2 s  . c o  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:BigDecimalInvoiceApp.java

public static void main(String[] args) {
    double subtotal = 123.123;

    double discountPercent = 0.2;
    BigDecimal decimalSubtotal = new BigDecimal(Double.toString(subtotal));
    decimalSubtotal = decimalSubtotal.setScale(2, RoundingMode.HALF_UP);
    BigDecimal decimalDiscountPercent = new BigDecimal(Double.toString(discountPercent));

    BigDecimal discountAmount = decimalSubtotal.multiply(decimalDiscountPercent);
    discountAmount = discountAmount.setScale(2, RoundingMode.HALF_UP);

    BigDecimal totalBeforeTax = decimalSubtotal.subtract(discountAmount);
    BigDecimal salesTaxPercent = new BigDecimal(".05");
    BigDecimal salesTax = salesTaxPercent.multiply(totalBeforeTax);
    salesTax = salesTax.setScale(2, RoundingMode.HALF_UP);
    BigDecimal total = totalBeforeTax.add(salesTax);

    NumberFormat currency = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();

    String message = "Subtotal:         " + currency.format(decimalSubtotal) + "\n" + "Discount percent: "
            + percent.format(decimalDiscountPercent) + "\n" + "Discount amount:  "
            + currency.format(discountAmount) + "\n" + "Total before tax: " + currency.format(totalBeforeTax)
            + "\n" + "Sales tax:        " + currency.format(salesTax) + "\n" + "Invoice total:    "
            + currency.format(total) + "\n";

    System.out.println(message);/*from ww  w  .j a  v a  2  s  .co  m*/

}

From source file:NumberFormatRounding.java

public static void main(String[] args) {
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(2);//w  ww  .j  av a 2  s  .  c o  m

    System.out.println("Default rounding mode: " + nf.getRoundingMode());
    System.out.println("123.454 rounds to " + nf.format(123.454));
    System.out.println("123.455 rounds to " + nf.format(123.455));
    System.out.println("123.456 rounds to " + nf.format(123.456));
    System.out.println();

}

From source file:com.discursive.jccook.math.SimpleRegressionExample.java

public static void main(String[] args) throws MathException {

    SimpleRegression sr = new SimpleRegression();

    // Add data points       
    sr.addData(0, 0);//from   w  ww . j  a v a2 s  .  c om
    sr.addData(1, 1.2);
    sr.addData(2, 2.6);
    sr.addData(3, 3.2);
    sr.addData(4, 4);
    sr.addData(5, 5);

    NumberFormat format = NumberFormat.getInstance();

    // Print the value of y when line intersects the y axis
    System.out.println("Intercept: " + format.format(sr.getIntercept()));

    // Print the number of data points
    System.out.println("N: " + sr.getN());

    // Print the Slope and the Slop Confidence
    System.out.println("Slope: " + format.format(sr.getSlope()));
    System.out.println("Slope Confidence: " + format.format(sr.getSlopeConfidenceInterval()));

    // Print RSquare a measure of relatedness
    System.out.println("RSquare: " + format.format(sr.getRSquare()));

    sr.addData(400, 100);
    sr.addData(300, 105);
    sr.addData(350, 70);
    sr.addData(200, 50);
    sr.addData(150, 300);
    sr.addData(50, 500);

    System.out.println("Intercept: " + format.format(sr.getIntercept()));
    System.out.println("N: " + sr.getN());
    System.out.println("Slope: " + format.format(sr.getSlope()));
    System.out.println("Slope Confidence: " + format.format(sr.getSlopeConfidenceInterval()));
    System.out.println("RSquare: " + format.format(sr.getRSquare()));

}

From source file:Main.java

public static void main(String args[]) {
    NumberFormat formatter = new DecimalFormat();

    int maxinteger = Integer.MAX_VALUE;
    System.out.println(maxinteger);
    formatter = new DecimalFormat("0.######E0");
    System.out.println(formatter.format(maxinteger));

    formatter = new DecimalFormat("0.#####E0");
    System.out.println(formatter.format(maxinteger));

    int mininteger = Integer.MIN_VALUE;
    System.out.println(mininteger);

    formatter = new DecimalFormat("0.######E0");
    System.out.println(formatter.format(mininteger));

    formatter = new DecimalFormat("0.#####E0");
    System.out.println(formatter.format(mininteger));

    double d = 0.12345;
    formatter = new DecimalFormat("0.#####E0");
    System.out.println(formatter.format(d));

    formatter = new DecimalFormat("000000E0");
    System.out.println(formatter.format(d));
}

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));

}