Java Fraction Format format(double n)

Here you can find the source of format(double n)

Description

Formats the given number into a price.

License

Open Source License

Return

The formatted string.

Declaration

public static String format(double n) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    private static String currency = "$";

    /**/*www .  j  ava2 s .c om*/
      * Formats the given number into a price. Adds commas to enable easy reading and the currency symbol or word
      * 
      * @return The formatted string.
      */
    public static String format(double n) {
        //TODO complete
        NumberFormat formatter;
        String number;
        formatter = new DecimalFormat("#,###,###,###.00");
        number = formatter.format(n);
        if (currency.length() == 1) {
            return currency + number;
        } else {
            return number + " " + currency;
        }
    }
}

Related

  1. format(double d, DecimalFormat unitFormatter)
  2. format(Double d, int digit)
  3. format(double d, java.text.NumberFormat format)
  4. format(double degrees)
  5. format(double input, String format)
  6. format(double no, String formatter)
  7. format(Double num)
  8. format(Double num)
  9. format(double num)