CurrencyUtils.java
3.22 KB
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
95
96
97
98
99
100
101
package com.diligrp.assistant.shared.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.util.Locale;
/**
* 金额格式转化工具类
*/
public class CurrencyUtils {
private static final int DEFAULT_SCALE = 2;
private static final Locale CURRENT_LOCALE = Locale.CHINA;
private static final BigDecimal YUAN_CENT_UNIT = new BigDecimal(100);
public static String toCurrency(Long cent) {
if (cent == null) {
return null;
}
BigDecimal amount = new BigDecimal(cent);
BigDecimal yuan = amount.divide(YUAN_CENT_UNIT).setScale(DEFAULT_SCALE, RoundingMode.HALF_UP);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(CURRENT_LOCALE);
StringBuffer currency = new StringBuffer();
numberFormat.format(yuan, currency, new FieldPosition(0));
if (cent < 0) {
correctSymbol(currency);
}
return currency.toString();
}
public static String toNoSymbolCurrency(Long cent) {
if (cent == null) {
return null;
}
BigDecimal amount = new BigDecimal(cent);
BigDecimal yuan = amount.divide(YUAN_CENT_UNIT).setScale(DEFAULT_SCALE, RoundingMode.HALF_UP);
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(CURRENT_LOCALE);
StringBuffer currency = new StringBuffer();
numberFormat.format(yuan, currency, new FieldPosition(0));
if (cent < 0) {
correctSymbol(currency);
}
return currency.substring(1);
}
public static String cent2TenNoSymbol(Long cent) {
BigDecimal yuan = point2ten(cent);
if (null == yuan) {
yuan = new BigDecimal(0L);
}
return yuan.toString();
}
public static Long yuan2Cent(BigDecimal yuan) {
if (yuan == null) {
return null;
}
BigDecimal amount = yuan.setScale(DEFAULT_SCALE, RoundingMode.HALF_UP);
BigDecimal cent = amount.multiply(YUAN_CENT_UNIT);
return cent.longValue();
}
private static BigDecimal point2ten(Long point) {
if (null == point) {
point = 0L;
}
BigDecimal centBigDecimal = new BigDecimal(point);
BigInteger divisor = BigInteger.valueOf(1L);
for (int i = 0; i < DEFAULT_SCALE; i++) {
divisor = divisor.multiply(BigInteger.valueOf(10L));
}
return centBigDecimal.divide(new BigDecimal(divisor)).setScale(DEFAULT_SCALE);
}
public static String convert2Percent(Long total, Long percentNumber) {
if (percentNumber == 0L || total == 0L) {
return "0.00%";
}
double percent = percentNumber.doubleValue() / total.doubleValue() * 100;
BigDecimal bigDecimal = new BigDecimal(percent);
return bigDecimal.setScale(2, RoundingMode.HALF_UP) + "%";
}
/**
* $-100.00 => -$100.00
*/
private static void correctSymbol(StringBuffer currency) {
char negativeSymbol = currency.charAt(0);
char currencySymbol = currency.charAt(1);
currency.setCharAt(0, currencySymbol);
currency.setCharAt(1, negativeSymbol);
}
}