2023-05-16 16:24:26 +08:00
|
|
|
|
package com.jsowell.common.util;
|
|
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
|
|
2023-05-16 16:31:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 汇付支付的工具类
|
|
|
|
|
|
*/
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public class AdapayUtil {
|
2023-05-16 16:31:23 +08:00
|
|
|
|
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
String amount = "1110.5309";
|
|
|
|
|
|
String s = formatAmount(amount);
|
|
|
|
|
|
System.out.println(s);
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal bigDecimal = new BigDecimal(amount);
|
|
|
|
|
|
String s2 = formatAmount(bigDecimal);
|
|
|
|
|
|
System.out.println(s2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-16 16:31:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 格式化数字 保留两位小数,不足补0
|
|
|
|
|
|
* @param amount
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public static String formatAmount(String amount) {
|
|
|
|
|
|
//保留2位小数
|
2023-05-16 16:31:23 +08:00
|
|
|
|
double d = new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
2023-05-16 16:24:26 +08:00
|
|
|
|
//不足两位则补0
|
|
|
|
|
|
DecimalFormat decimalFormat = new DecimalFormat("0.00#");
|
2023-05-16 16:31:23 +08:00
|
|
|
|
return decimalFormat.format(d);
|
2023-05-16 16:24:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-16 16:31:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 格式化数字 保留两位小数,不足补0
|
|
|
|
|
|
* @param amount
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public static String formatAmount(BigDecimal amount) {
|
|
|
|
|
|
return formatAmount(amount.toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|