mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix
This commit is contained in:
parent
8734905dfc
commit
95d71a8079
@ -19,11 +19,11 @@ public class NumberChineseFormatter {
|
|||||||
/**
|
/**
|
||||||
* 简体中文形式
|
* 简体中文形式
|
||||||
**/
|
**/
|
||||||
private static final String[] SIMPLE_DIGITS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
|
private static final char[] SIMPLE_DIGITS = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
|
||||||
/**
|
/**
|
||||||
* 繁体中文形式
|
* 繁体中文形式
|
||||||
**/
|
**/
|
||||||
private static final String[] TRADITIONAL_DIGITS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
|
private static final char[] TRADITIONAL_DIGITS = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 简体中文单位
|
* 简体中文单位
|
||||||
@ -66,7 +66,7 @@ public class NumberChineseFormatter {
|
|||||||
* @return 中文
|
* @return 中文
|
||||||
*/
|
*/
|
||||||
public static String format(double amount, boolean isUseTraditional, boolean isMoneyMode) {
|
public static String format(double amount, boolean isUseTraditional, boolean isMoneyMode) {
|
||||||
final String[] numArray = isUseTraditional ? TRADITIONAL_DIGITS : SIMPLE_DIGITS;
|
final char[] numArray = isUseTraditional ? TRADITIONAL_DIGITS : SIMPLE_DIGITS;
|
||||||
|
|
||||||
if (amount > 99999999999999.99 || amount < -99999999999999.99) {
|
if (amount > 99999999999999.99 || amount < -99999999999999.99) {
|
||||||
throw new IllegalArgumentException("Number support only: (-99999999999999.99 ~ 99999999999999.99)!");
|
throw new IllegalArgumentException("Number support only: (-99999999999999.99 ~ 99999999999999.99)!");
|
||||||
@ -127,7 +127,7 @@ public class NumberChineseFormatter {
|
|||||||
|
|
||||||
// 整数部分为 0, 则表达为"零"
|
// 整数部分为 0, 则表达为"零"
|
||||||
if (StrUtil.EMPTY.equals(chineseStr.toString())) {
|
if (StrUtil.EMPTY.equals(chineseStr.toString())) {
|
||||||
chineseStr = new StringBuilder(numArray[0]);
|
chineseStr = new StringBuilder(String.valueOf(numArray[0]));
|
||||||
}
|
}
|
||||||
//负数
|
//负数
|
||||||
if (negative) { // 整数部分不为 0
|
if (negative) { // 整数部分不为 0
|
||||||
@ -163,12 +163,12 @@ public class NumberChineseFormatter {
|
|||||||
* @since 5.3.9
|
* @since 5.3.9
|
||||||
*/
|
*/
|
||||||
public static String numberCharToChinese(char c, boolean isUseTraditional) {
|
public static String numberCharToChinese(char c, boolean isUseTraditional) {
|
||||||
String[] numArray = isUseTraditional ? TRADITIONAL_DIGITS : SIMPLE_DIGITS;
|
char[] numArray = isUseTraditional ? TRADITIONAL_DIGITS : SIMPLE_DIGITS;
|
||||||
int index = c - 48;
|
int index = c - 48;
|
||||||
if (index < 0 || index >= numArray.length) {
|
if (index < 0 || index >= numArray.length) {
|
||||||
return String.valueOf(c);
|
return String.valueOf(c);
|
||||||
}
|
}
|
||||||
return numArray[index];
|
return String.valueOf(numArray[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -179,7 +179,7 @@ public class NumberChineseFormatter {
|
|||||||
* @return 转换后的汉字
|
* @return 转换后的汉字
|
||||||
*/
|
*/
|
||||||
private static String toChinese(int amountPart, boolean isUseTraditional) {
|
private static String toChinese(int amountPart, boolean isUseTraditional) {
|
||||||
String[] numArray = isUseTraditional ? TRADITIONAL_DIGITS : SIMPLE_DIGITS;
|
final char[] numArray = isUseTraditional ? TRADITIONAL_DIGITS : SIMPLE_DIGITS;
|
||||||
String[] units = isUseTraditional ? TRADITIONAL_UNITS : SIMPLE_UNITS;
|
String[] units = isUseTraditional ? TRADITIONAL_UNITS : SIMPLE_UNITS;
|
||||||
|
|
||||||
int temp = amountPart;
|
int temp = amountPart;
|
||||||
@ -223,16 +223,19 @@ public class NumberChineseFormatter {
|
|||||||
boolean secUnit = false;
|
boolean secUnit = false;
|
||||||
final int length = chinese.length();
|
final int length = chinese.length();
|
||||||
while (pos < length) {
|
while (pos < length) {
|
||||||
int num = ArrayUtil.indexOf(SIMPLE_DIGITS, chinese.substring(pos, pos + 1));
|
final int num = ArrayUtil.indexOf(SIMPLE_DIGITS, chinese.charAt(pos));
|
||||||
if (num >= 0) {
|
if (num >= 0) {
|
||||||
|
// 普通数字
|
||||||
number = num;
|
number = num;
|
||||||
pos += 1;
|
pos += 1;
|
||||||
if (pos >= length) {
|
if (pos >= length) {
|
||||||
|
// 末尾是数字
|
||||||
section += number;
|
section += number;
|
||||||
rtn += section;
|
rtn += section;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
//单位
|
||||||
int unit = 1;
|
int unit = 1;
|
||||||
int tmp = chineseToUnit(chinese.substring(pos, pos + 1));
|
int tmp = chineseToUnit(chinese.substring(pos, pos + 1));
|
||||||
if (tmp != -1) {
|
if (tmp != -1) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.convert;
|
package cn.hutool.core.convert;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class NumberChineseFormatterTest {
|
public class NumberChineseFormatterTest {
|
||||||
@ -89,4 +90,10 @@ public class NumberChineseFormatterTest {
|
|||||||
Assert.assertEquals(1000000, NumberChineseFormatter.chineseToNumber("一百万"));
|
Assert.assertEquals(1000000, NumberChineseFormatter.chineseToNumber("一百万"));
|
||||||
Assert.assertEquals(2000100112, NumberChineseFormatter.chineseToNumber("二十亿零一十万零一百一十二"));
|
Assert.assertEquals(2000100112, NumberChineseFormatter.chineseToNumber("二十亿零一十万零一百一十二"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void chineseToNumberTest2(){
|
||||||
|
Assert.assertEquals(120, NumberChineseFormatter.chineseToNumber("一百二"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user