chinabugotech/hutool · error · IllegalArgumentException

Unknown unit '{}' at: {}

Error message

Unknown unit '{}' at: {}

What it means

NumberChineseFormatter.chineseToNumber throws IllegalArgumentException when it meets a character that is neither a recognized Chinese digit (零一二三四五六七八九) nor a recognized unit (十百千万亿 etc.). chineseToNumber(c) returned -1 and chineseToUnit(c) returned null.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java:535

			final int num = chineseToNumber(c);
			if (num >= 0) {
				if (num == 0) {
					// 遇到零时节结束,权位失效,比如两万二零一十
					if (number > 0 && null != unit) {
						section += number * (unit.value / 10);
					}
					unit = null;
				} else if (number > 0) {
					// 多个数字同时出现,报错
					throw new IllegalArgumentException(StrUtil.format("Bad number '{}{}' at: {}", chinese.charAt(i - 1), c, i));
				}
				// 普通数字
				number = num;
			} else {
				unit = chineseToUnit(c);
				if (null == unit) {
					// 出现非法字符
					throw new IllegalArgumentException(StrUtil.format("Unknown unit '{}' at: {}", c, i));
				}

				//单位
				if (unit.secUnit) {
					// 节单位,按照节求和
					section = (section + number) * unit.value;
					result += section;
					section = 0;
				} else {
					// 非节单位,和单位前的单数字组合为值
					int unitNumber = number;
					if (0 == number && 0 == i) {
						// issue#1726,对于单位开头的数组,默认赋予1
						// 十二 -> 一十二
						// 百二 -> 一百二
						unitNumber = 1;
					}
					section += (unitNumber * unit.value);

View on GitHub (pinned to 8870454b2a)

Solutions

  1. If the input is Arabic, parse it as a number directly instead of chineseToNumber.
  2. Pre-convert Arabic digits to Chinese digits and strip unsupported characters before parsing.
  3. Validate the string against the allowed character set before calling.

Example fix

// before
int n = NumberChineseFormatter.chineseToNumber("100");

// after - branch on script
if (s.matches("\\d+")) {
    n = Integer.parseInt(s);
} else {
    n = NumberChineseFormatter.chineseToNumber(s);
}
Defensive patterns

Strategy: validation

Validate before calling

// only Chinese digits and units allowed
if (!chinese.matches("[零一二三四五六七八九十百千万亿]+")) {
    throw new IllegalArgumentException("non-chinese-numeral characters: " + chinese);
}
NumberChineseFormatter.chineseToNumber(chinese);

Try / catch

try {
    return NumberChineseFormatter.chineseToNumber(s);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Unknown unit")) {
        // maybe it is Arabic -> parse as number instead
        return Integer.parseInt(s);
    }
    throw e;
}

Prevention

When it happens

Trigger: Input contains Arabic digits (0-9), Latin letters, punctuation, spaces, or any character outside the supported digit/unit set.

Common situations: User typed Arabic numerals ("100") instead of Chinese; mixed-language strings; copy-paste introducing stray characters.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/e3639322576c7d39. Report an issue: GitHub.