我正在编写一个函数,使用ICU来解析由汉字数字字符组成的Unicode字符串,并希望返回字符串的整数值.
"五"=> 5
"三十一"=> 31
"五千九百七十二"=> 5972
我将语言环境设置为Locale :: getJapan()并使用NumberFormat :: parse()来解析字符串.但是,每当我传递任何汉字字符时,parse()方法返回U_INVALID_FORMAT_ERROR.
有谁知道ICU是否支持NumberFormat :: parse()方法中的汉字字符串?我希望,因为我将Locale设置为日语,它将能够解析汉字数值.
谢谢!
#include <iostream>
#include <unicode/numfmt.h>
using namespace std;
int main(int argc, char **argv) {
const Locale &jaLocale = Locale::getJapan();
UErrorCode status = U_ZERO_ERROR;
NumberFormat *nf = NumberFormat::createInstance(jaLocale, status);
UChar number[] = {0x4E94}; // Character for '5' in Japanese '?'
UnicodeString numStr(number);
Formattable formattable;
nf->parse(numStr, formattable, status);
if (U_FAILURE(status)) {
cout << "error parsing as number: " << u_errorName(status) << endl;
return(1);
}
cout << "long value: " << formattable.getLong() << endl;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用ICU规则数字格式(RBNF)模块rbnf.h(C++)或C,在unum.h中使用UNUM_SPELLOUT选项,两者都使用日语的"ja"语言环境.Atryom为您的C++代码提供了更正:new RuleBasedNumberFormat(URBNF_SPELLOUT,jaLocale, status);