{"record":{"id":"e3639322576c7d39","repo":"chinabugotech/hutool","slug":"unknown-unit-at","errorCode":null,"errorMessage":"Unknown unit '{}' at: {}","messagePattern":"Unknown unit '(.+?)' at: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java","lineNumber":535,"sourceCode":"\t\t\tfinal int num = chineseToNumber(c);\n\t\t\tif (num >= 0) {\n\t\t\t\tif (num == 0) {\n\t\t\t\t\t// 遇到零时节结束，权位失效，比如两万二零一十\n\t\t\t\t\tif (number > 0 && null != unit) {\n\t\t\t\t\t\tsection += number * (unit.value / 10);\n\t\t\t\t\t}\n\t\t\t\t\tunit = null;\n\t\t\t\t} else if (number > 0) {\n\t\t\t\t\t// 多个数字同时出现，报错\n\t\t\t\t\tthrow new IllegalArgumentException(StrUtil.format(\"Bad number '{}{}' at: {}\", chinese.charAt(i - 1), c, i));\n\t\t\t\t}\n\t\t\t\t// 普通数字\n\t\t\t\tnumber = num;\n\t\t\t} else {\n\t\t\t\tunit = chineseToUnit(c);\n\t\t\t\tif (null == unit) {\n\t\t\t\t\t// 出现非法字符\n\t\t\t\t\tthrow new IllegalArgumentException(StrUtil.format(\"Unknown unit '{}' at: {}\", c, i));\n\t\t\t\t}\n\n\t\t\t\t//单位\n\t\t\t\tif (unit.secUnit) {\n\t\t\t\t\t// 节单位，按照节求和\n\t\t\t\t\tsection = (section + number) * unit.value;\n\t\t\t\t\tresult += section;\n\t\t\t\t\tsection = 0;\n\t\t\t\t} else {\n\t\t\t\t\t// 非节单位，和单位前的单数字组合为值\n\t\t\t\t\tint unitNumber = number;\n\t\t\t\t\tif (0 == number && 0 == i) {\n\t\t\t\t\t\t// issue#1726，对于单位开头的数组，默认赋予1\n\t\t\t\t\t\t// 十二 -> 一十二\n\t\t\t\t\t\t// 百二 -> 一百二\n\t\t\t\t\t\tunitNumber = 1;\n\t\t\t\t\t}\n\t\t\t\t\tsection += (unitNumber * unit.value);","sourceCodeStart":517,"sourceCodeEnd":553,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java#L517-L553","documentation":"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.","triggerScenarios":"Input contains Arabic digits (0-9), Latin letters, punctuation, spaces, or any character outside the supported digit/unit set.","commonSituations":"User typed Arabic numerals (\"100\") instead of Chinese; mixed-language strings; copy-paste introducing stray characters.","solutions":["If the input is Arabic, parse it as a number directly instead of chineseToNumber.","Pre-convert Arabic digits to Chinese digits and strip unsupported characters before parsing.","Validate the string against the allowed character set before calling."],"exampleFix":"// before\nint n = NumberChineseFormatter.chineseToNumber(\"100\");\n\n// after - branch on script\nif (s.matches(\"\\\\d+\")) {\n    n = Integer.parseInt(s);\n} else {\n    n = NumberChineseFormatter.chineseToNumber(s);\n}","handlingStrategy":"validation","validationCode":"// only Chinese digits and units allowed\nif (!chinese.matches(\"[零一二三四五六七八九十百千万亿]+\")) {\n    throw new IllegalArgumentException(\"non-chinese-numeral characters: \" + chinese);\n}\nNumberChineseFormatter.chineseToNumber(chinese);","typeGuard":null,"tryCatchPattern":"try {\n    return NumberChineseFormatter.chineseToNumber(s);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Unknown unit\")) {\n        // maybe it is Arabic -> parse as number instead\n        return Integer.parseInt(s);\n    }\n    throw e;\n}","preventionTips":["Branch on script: parse Arabic numerals with Integer.parseInt, Chinese with chineseToNumber.","Strip whitespace and unsupported punctuation before parsing."],"tags":["convert","chinese-numeral","parsing","validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}