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);
|
}
|
|
}
|