Java amount case conversion method
- 2020-06-19 10:11:27
- OfStack
This article shares the specific code of Java amount capitalization conversion for your reference, the specific content is as follows
/**
* @ClassName: NumberConvert
* @Description: TODO
* @date 2013-3-07 In the afternoon 16:08:10
* @version V1.0
*/
public class MoneyUtil {
/** Upper case number */
private static final String[] NUMBERS = { " zero ", " one ", " Ii. ", " 3 ", " boss ", " wu ", " lu ", " Retailer, ", " ", " nine " };
/** The unit of an integral part */
private static final String[] IUNIT = { " yuan ", " Pick up ", " hk ", " micky ", " wan ", " Pick up ", " hk ", " micky ", " Hundred million ", " Pick up ", " hk ", " micky ", " wan ", " Pick up ", " hk ",
" micky " };
/** The unit of a decimal fraction */
private static final String[] DUNIT = { " Angle ", " points ", " li " };
/**
* You get a capital amount.
*/
public static String toChinese(String str) {
str = str.replaceAll(",", "");// To get rid of ","
String integerStr;// Integral part number
String decimalStr;// Fractional number
// Initialization: Separates the integer part from the decimal part
if (str.indexOf(".") > 0) {
integerStr = str.substring(0, str.indexOf("."));
decimalStr = str.substring(str.indexOf(".") + 1);
} else if (str.indexOf(".") == 0) {
integerStr = "";
decimalStr = str.substring(1);
} else {
integerStr = str;
decimalStr = "";
}
// integerStr Remove the first 0 Don't have to take it out decimalStr In the end 0( The excess part is removed )
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
}
// overflow Out of capacity, return directly
if (integerStr.length() > IUNIT.length) {
System.out.println(str + ": Out of capacity ");
return str;
}
int[] integers = toArray(integerStr);// Integral part number
boolean isMust5 = isMust5(integerStr);// Ten thousand units
int[] decimals = toArray(decimalStr);// Fractional number
return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals);
}
/**
* To convert integer and decimal parts into arrays, from high to low
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
}
/**
* Get the integral part of the amount in Chinese.
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
for (int i = 0; i < length; i++) {
// 0 Appear in key locations: 1234( wan )5678( Hundred million )9012( wan )3456( yuan )
// Special circumstances: 10( Ten dollars, ten dollars, ten hundred thousand, one hundred thousand )
String key = "";
if (integers[i] == 0) {
if ((length - i) == 13)// wan ( Hundred million )( mandatory )
key = IUNIT[4];
else if ((length - i) == 9)// Hundred million ( mandatory )
key = IUNIT[8];
else if ((length - i) == 5 && isMust5)// wan ( Don't have to fill in )
key = IUNIT[4];
else if ((length - i) == 1)// yuan ( mandatory )
key = IUNIT[0];
// 0 In case of 0 Time to zero, not including the last 1 position
if ((length - i) > 1 && integers[i + 1] != 0)
key += NUMBERS[0];
}
chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
}
return chineseInteger.toString();
}
/**
* Get the decimal portion of the amount in Chinese.
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int i = 0; i < decimals.length; i++) {
// give 3 After the decimal place
if (i == 3)
break;
chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i]));
}
return chineseDecimal.toString();
}
/**
* Determine the first 5 A unit of digits " wan " Whether to add.
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) {
// Get the number from the low order, the th 5 To the first 8 A string
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
}
public static void main(String[] args) {
String number = "1.23";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "1234567890123456.123";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "0.0798";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "10,001,000.09";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "01.107700";
System.out.println(number + " " + MoneyUtil.toChinese(number));
}
}
The operation result is:
One yuan and twenty-two cents
1234567890123456.123 One thousand Two hundred and forty-five thousand six hundred and seventy-Eight hundred and ninety-nine thousand one hundred and thirty-three thousand four hundred and fifty-one thousand twelve point three
0.0798 seven nine cents
B: 10,001,000.09 ten thousand one thousand nine cents
01.107700 one Yuan and seventy cents