Java Fraction Format format(Double num)

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

Description

format

License

Open Source License

Declaration

public static String format(Double num) 

Method Source Code


//package com.java2s;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    public static String format(Double num) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        return df.format(num);
    }//from   ww w . j a v a2  s.c om

    public static String format(BigDecimal num) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        return df.format(num);
    }

    public static String format(BigDecimal num, int divisor) {
        return format(num.divide(new BigDecimal(divisor)));
    }

    public static String format(String num, int scale, boolean isFix) {

        NumberFormat nf = new DecimalFormat(parse(num, scale, isFix).toString());

        return nf.format(new BigDecimal(num));
    }

    public static BigDecimal parse(String num, int scale, boolean isFix) {
        StringBuffer flag = new StringBuffer();
        flag.append("#");

        for (int i = 0; i < scale; i++) {
            if (i == 0) {
                flag.append(isFix ? ".0" : ".#");
            } else {
                flag.append(isFix ? "0" : "#");
            }
        }

        return new BigDecimal(num);
    }
}

Related

  1. format(double degrees)
  2. format(double input, String format)
  3. format(double n)
  4. format(double no, String formatter)
  5. format(double num)
  6. format(Double num)
  7. format(double num, int n)
  8. format(double num, String pattern)
  9. format(double number)