左晓为主开发手持机充值管理机
zuoxiao
2024-07-12 420562c180324d5cc22bb8bec0fe040c304eca03
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
package com.dayu.baselibrary.utils;
 
import java.math.BigDecimal;
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) {
        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);
    }
 
}