mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 19:15:35 +08:00
update 海南平台对接
This commit is contained in:
@@ -3,6 +3,7 @@ package com.jsowell.common.util;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
@@ -19,27 +20,13 @@ import java.util.Base64;
|
||||
@Slf4j
|
||||
public class AdapayUtil {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化数字 保留两位小数,不足补0
|
||||
* @param amount
|
||||
* @return
|
||||
*/
|
||||
public static String formatAmount(String amount) {
|
||||
//保留2位小数
|
||||
double d = new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||||
//不足两位则补0
|
||||
DecimalFormat decimalFormat = new DecimalFormat("0.00#");
|
||||
return decimalFormat.format(d);
|
||||
return formatAmount(new BigDecimal(amount));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,7 +35,34 @@ public class AdapayUtil {
|
||||
* @return
|
||||
*/
|
||||
public static String formatAmount(BigDecimal amount) {
|
||||
return formatAmount(amount.toString());
|
||||
return formatAmount(amount, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化数字 保留n位小数,不足补0
|
||||
* @param amount
|
||||
* @return
|
||||
*/
|
||||
public static String formatAmount(BigDecimal amount, int n) {
|
||||
//保留2位小数
|
||||
double d = amount.setScale(n, RoundingMode.DOWN).doubleValue();
|
||||
//不足则补0
|
||||
StringBuilder pattern = new StringBuilder("0");
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (i == 0) {
|
||||
pattern.append(".");
|
||||
}
|
||||
pattern.append("0");
|
||||
}
|
||||
DecimalFormat decimalFormat = new DecimalFormat(pattern.toString());
|
||||
return decimalFormat.format(d);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BigDecimal bigDecimal = new BigDecimal("1236.8369");
|
||||
System.out.println(formatAmount(bigDecimal, 0));
|
||||
System.out.println(formatAmount(bigDecimal, 2));
|
||||
System.out.println(formatAmount(bigDecimal, 4));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user