Java Fraction Format formatNumFraction(float num, int unit)

Here you can find the source of formatNumFraction(float num, int unit)

Description

format Num Fraction

License

Open Source License

Declaration

public static String formatNumFraction(float num, int unit) 

Method Source Code


//package com.java2s;

import java.text.DecimalFormat;

public class Main {

    public static String formatNumFraction(double num, int unit) {
        String zero = "";
        if (unit == 0) {
            zero = "0";
        } else {// ww w  .j  av a  2s .co  m
            zero = "0.";
        }
        for (int i = 0; i < unit; i++) {
            zero += "0";
        }
        DecimalFormat df = new DecimalFormat(zero);
        return df.format(num);
    }

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

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

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

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

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

    public static String formatNumFraction(float num, int unit) {
        String zero = "";
        if (unit == 0) {
            zero = "0";
        } else {
            zero = "0.";
        }
        for (int i = 0; i < unit; i++) {
            zero += "0";
        }
        DecimalFormat df = new DecimalFormat(zero);
        return df.format(num);
    }

    public static String formatNumFraction(long num, int unit) {
        String zero = "";
        if (unit == 0) {
            zero = "0";
        } else {
            zero = "0.";
        }
        for (int i = 0; i < unit; i++) {
            zero += "0";
        }
        DecimalFormat df = new DecimalFormat(zero);
        return df.format(num);
    }
}

Related

  1. formatNumber(int fractionDigits, double value)
  2. formatNumber(String Format, double Num)
  3. formatNumber(String format, double number)
  4. formatNumber(String number, int maxFractionalDigits)
  5. formatNumberAttribute(double v)
  6. formatNumValueWithComma(double val)
  7. formatoDecimal(String tipo, double valor)
  8. formatOLETime(double oleTime)
  9. formatOneDecimal(double dNumber)