How to parse kanji numeric characters using ICU?
- by Aki
I'm writing a function using ICU to parse an Unicode string which consists of kanji numeric character(s) and want to return the integer value of the string.
"?" = 5
"???" = 31
"???????" = 5972
I'm setting the locale to Locale::getJapan() and using the NumberFormat::parse() to parse the character string. However, whenever I pass it any Kanji characters, the parse() method is returning U_INVALID_FORMAT_ERROR.
Does anyone know if ICU supports Kanji character strings in the NumberFormat::parse() method? I was hoping that since I'm setting the Locale to Japanese that it would be able to parse Kanji numeric values.
Thanks!
#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;
}