StringPriceFormat.java 5.38 KB
Newer Older
jiangjiantao's avatar
jiangjiantao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
package com.miya.fastcashier.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * Created by fengyu on 2018/4/17.
 */
public class StringPriceFormat {

    /**
     * 分转元
     *
     * @param p
     * @return
     */
    public static String transStringPriceToDecimalString(String p) {
        DecimalFormat numberFormat = new DecimalFormat("0.00");
        //防止参数不合法异常崩溃
        try {
            BigDecimal price = new BigDecimal(p);
            BigDecimal x = new BigDecimal("100");
            return price.divide(x, 2, RoundingMode.HALF_UP).toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            //空或转换异常则显示--.--
            return "--.--";
        }

    }

    public static String transDoubleStringPriceToDecimalString(String p) {
        DecimalFormat numberFormat = new DecimalFormat("0.00");
        //防止参数不合法异常崩溃
        try {
            Double price = Double.parseDouble(p);
            return numberFormat.format(price / 100.00);
        } catch (Exception ex) {
            ex.printStackTrace();
            //空或转换异常则显示--.--
            return "--.--";
        }

    }


    public static String transStringPriceToDecimalString(double p) {
        DecimalFormat numberFormat = new DecimalFormat("0.00");
        //防止参数不合法异常崩溃
        try {
            return numberFormat.format(p / 100.00);
        } catch (Exception ex) {
            ex.printStackTrace();
            //空或转换异常则显示--.--
            return "--.--";
        }

    }

    /**
     * 字符元转字符分
     *
     * @param yuan 字符元
     * @return 字符分
     */
    public static String transStringYuan2Fen(String yuan) {
        String result = yuan;
        String tmp1 = "", tmp2 = "";
        if (yuan.contains(".")) {
            int index = yuan.indexOf(".");
            tmp1 = yuan.substring(0, index);
            tmp2 = yuan.substring(index + 1, yuan.length());
            result = yuan.replace(".", "");
            if (tmp2.length() <= 0) {
                result = result + "00";
            } else if (tmp2.length() == 1) {
                result = result + "0";
            } else if (tmp2.length() > 2) {
                //如果小数点后有3位或以上说明服务器出现异常,此时仍然只取前两位
                result = result.substring(0, index + 2);
            }
        } else {
            result = result + "00";
        }
        return result;
    }

    /**
     * 获取实付金额,单位:分
     *
     * @param totalPrice 总价,单位:元
     * @param promPrice  优惠价,单位:元
     * @return 实付金额,单位:分
     */
    public static String getPayPriceFenStringFromYuan(String totalPrice, String promPrice) {
        try {
            int totalPriceInt = Integer.parseInt(transStringYuan2Fen(totalPrice));
            int promPriceInt = Integer.parseInt(transStringYuan2Fen(promPrice));
            int payPriceInt = totalPriceInt - promPriceInt;
            //检查数字正确性
            if (totalPriceInt < promPriceInt ||
                    totalPriceInt < 0 ||
                    promPriceInt < 0 ||
                    payPriceInt < 0) {
                //参数不正确,返回0
                return "0";
            }
            return payPriceInt + "";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "0";
        }
    }

    /**
     * 获取实付金额,单位:元
     *
     * @param totalPrice 总价,单位:元
     * @param promPrice  优惠价,单位:元
     * @return 实付金额,单位:元
     */
    public static String getPayPriceYuanStringFromYuan(String totalPrice, String promPrice) {
        try {
            int totalPriceInt = Integer.parseInt(transStringYuan2Fen(totalPrice));
            int promPriceInt = Integer.parseInt(transStringYuan2Fen(promPrice));
            int payPriceInt = totalPriceInt - promPriceInt;
            //检查数字正确性
            if (totalPriceInt < promPriceInt ||
                    totalPriceInt < 0 ||
                    promPriceInt < 0 ||
                    payPriceInt < 0) {
                //参数不正确,返回0
                return "0";
            }
            return transStringPriceToDecimalString(payPriceInt);
        } catch (Exception ex) {
            ex.printStackTrace();
            return "0";
        }
    }

    /**
     * 获取实付金额,单位:分
     *
     * @param totalPrice 总价,单位:分
     * @param promPrice  优惠价,单位:分
     * @return 实付金额,单位:分
     */
    public static String getPayPriceFenStringFromFen(String totalPrice, String promPrice) {
        try {
            int totalPriceInt = Integer.parseInt(totalPrice);
            int promPriceInt = Integer.parseInt(promPrice);
            int payPriceInt = totalPriceInt - promPriceInt;
            //检查数字正确性
            if (totalPriceInt < promPriceInt ||
                    totalPriceInt < 0 ||
                    promPriceInt < 0 ||
                    payPriceInt < 0) {
                //参数不正确,返回0
                return "0";
            }
            return payPriceInt + "";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "0";
        }
    }
}