左晓为主开发手持机充值管理机
zuoxiao
2025-03-07 0ec9693c39a910233fc186a8cefab9f61030df78
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
package com.dayu.baselibrary.utils;
 
import android.text.TextUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
 
/**
 * Copyright (C), 2022,
 * Author: zuo
 * Date: 2022/3/23 10:59
 * Description:
 */
public class MornyUtil {
    /**
     * 元转分,确保price保留两位有效数字
     *
     * @return
     */
    public static int changeY2F(String amount) {
        if (TextUtils.isEmpty(amount)){
            return 0;
        }
        BigDecimal bigDecimal = new BigDecimal(amount).setScale(2);
        return bigDecimal.multiply(new BigDecimal(100)).intValue();
    }
 
 
    /**
     * 元转厘,确保price保留两位有效数字
     *
     * @return
     */
    public static int changeY2L(String amount) {
        BigDecimal bigDecimal = new BigDecimal(amount).setScale(3);
        return bigDecimal.multiply(new BigDecimal(1000)).intValue();
    }
 
 
    /**
     * 分转元,转换为bigDecimal在toString
     *
     * @return
     */
    public static String changeF2Y(int price) {
        return BigDecimal.valueOf(Long.valueOf(price)).divide(new BigDecimal(100)).toString();
    }
 
 
    /**
     * 精确除法,保留两位小数
     *
     * @param dividend
     * @param divisor
     * @return
     */
    public static String intDiv(int dividend, int divisor) {
        double result = (double) dividend / divisor;
        // 使用 DecimalFormat 格式化结果,保留两位小数
        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(result);
    }
 
 
    /**
     * 系数和水价相乘获得电价保留三位小数
     * @param num1
     * @param num2
     * @return
     */
    public static String multiplyPrice(float num1, float num2) {
        BigDecimal bd1 = BigDecimal.valueOf(num1);
        BigDecimal bd2 = BigDecimal.valueOf(num2);
        BigDecimal result = bd1.multiply(bd2);
        return result.setScale(3, RoundingMode.HALF_UP).toString();
    }
 
 
    /**
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String sumPrice(float num1, float num2) {
        BigDecimal bd1 = BigDecimal.valueOf(num1);
        BigDecimal bd2 = BigDecimal.valueOf(num2);
        BigDecimal result = bd1.add(bd2);
        return result.setScale(2, RoundingMode.HALF_UP).toString();
    }
 
 
}