Java汉字数字转阿拉伯_阿拉伯数字转中文(java实现)

直接上代码

private static final String[] CHINESE_NUMBERS = {"零", "一", "两", "三", "四", "五", "六", "七", "八", "九", "十"};

private static final ChineseUnit[] CHINESE_UNIT = {zero, ten, hundred, thousand, ten_thousand, billion, million, ten_million, hundred_mullion};

private static final String[] CHINESE_NUMBERS_2 = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};

/**

*

* @param number

* @return

*/

public static String translateNumber2Chinese(int number) {

String s = String.valueOf(number);

if (number <= 10) {

return CHINESE_NUMBERS[number];

}

Stackstack = new Stack<>();

int index = 0;

for (int i = s.length() - 1; i >= 0; i --) {

NumberUnit numberUnit = new NumberUnit();

numberUnit.chineseNumber = CHINESE_NUMBERS_2[Integer.parseInt(String.valueOf(s.charAt(i)))];

numberUnit.chineseUnit = CHINESE_UNIT[index];

numberUnit.originalNumber = Integer.parseInt(String.valueOf(s.charAt(i)));

stack.push(numberUnit);

index ++;

}

StringBuilder stringBuilder = new StringBuilder();

while (!stack.isEmpty()) {

NumberUnit numberUnit = stack.pop();

if (numberUnit.originalNumber > 0) {

stringBuilder.append(numberUnit.chineseNumber);

if (numberUnit.chineseUnit != zero) {

stringBuilder.append(numberUnit.chineseUnit.getValue());

}

} else if (numberUnit.chineseUnit != zero) {

NumberUnit nextNumber = stack.peek();

if (nextNumber != null && nextNumber.originalNumber != 0) {

stringBuilder.append(numberUnit.chineseNumber);

}

}

}

return stringBuilder.toString();

}

private static class NumberUnit {

protected ChineseUnit chineseUnit;

protected String chineseNumber;

protected int originalNumber;

}

enum ChineseUnit {

/**

*

*/

zero("零"),

/**

*

*/

ten("十"),

/**

*

*/

hundred("百"),

thousand("千"),

ten_thousand("万"),

billion("十"),

million("百"),

ten_million("千"),

hundred_mullion("亿")

;

private String value;

ChineseUnit(String value) {

this.value = value;

}

/**

* Getter method for property value.

*

* @return property value of value

*/

public String getValue() {

return value;

}

/**

* Setter method for property value.

*

* @param value value to be assigned to property value

*/

public void setValue(String value) {

this.value = value;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值