package com.crm.rely.backend.util;


/**
 * Created by max on 2017/9/25.
 */
public class FloatFormat {

    public static float formatFloat(float source, int save) {
        float dest = 0F;
        String formatPattern = "%." + save + "f";
        String sourceStr = String.format(formatPattern, source);
        dest = Float.parseFloat(sourceStr);
        return dest;
    }

    public static float formatFloat(float source) {
        return formatFloat(source, 2);
    }

    public static String formatToPercent(Float source) {
        if (source == null) source = 0F;
        try {
            if (source == 0) {
                return "0%";
            } else {
                return String.format("%.2f%s", source, "%");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    public static double formatDouble(double source, int save) {
        double dest = 0F;
        String formatPattern = "%." + save + "f";
        String sourceStr = String.format(formatPattern, source);
        dest = Double.parseDouble(sourceStr);
        return dest;
    }

    public static double formatDouble(double source) {
        return formatDouble(source, 2);
    }

    public static String formatDoubleToPercent(Double source) {
        if (source == null) source = 0D;
        try {
            if (source == 0) {
                return "0%";
            } else {
                return String.format("%.2f%s", source, "%");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


}
