Java Fraction Format formatDouble(Double num)

Here you can find the source of formatDouble(Double num)

Description

format Double

License

Open Source License

Declaration

public static String formatDouble(Double num) 

Method Source Code

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

import java.text.DecimalFormat;

public class Main {
    public static String formatDouble(Double num) {
        String s = formatDouble(num, "#.##");
        String[] split = s.split("\\.");
        String format = "";
        char[] chars = split[0].toCharArray();
        String append = "";
        for (int i = 0; i < chars.length; i++) {
            append = chars[chars.length - (i + 1)] + append;
            if ((i + 1) % 3 == 0 || i == chars.length - 1) {
                format = append + "," + format;
                append = "";
            }//from  w  ww  . j a  va  2 s .c o  m
        }
        String ret = format.substring(0, format.length() - 1);
        if (split.length > 1)
            ret += "." + split[1];
        return ret;
    }

    public static String formatDouble(Double num, String format) {
        DecimalFormat df = new DecimalFormat(format);
        return df.format(num);
    }
}

Related

  1. formatDouble(Double doubleValue)
  2. formatDouble(Double in)
  3. formatDouble(double inVal, int inNumPlaces, boolean pad)
  4. formatDouble(Double num)
  5. formatDouble(double num)
  6. formatdouble(double number)
  7. formatDouble(double number)
  8. formatDouble(double number)
  9. formatDouble(double number, int decimalDigits)