本文整理汇总了C++中NumberFormat::parseCurrency方法的典型用法代码示例。如果您正苦于以下问题:C++ NumberFormat::parseCurrency方法的具体用法?C++ NumberFormat::parseCurrency怎么用?C++ NumberFormat::parseCurrency使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NumberFormat
的用法示例。
在下文中一共展示了NumberFormat::parseCurrency方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: stringToNumber
std::string GlobalizationNDK::stringToNumber(const std::string& args)
{
if (args.empty()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: no arguments provided!");
return errorInJson(PARSING_ERROR, "No arguments provided!");
}
Json::Reader reader;
Json::Value root;
bool parse = reader.parse(args, root);
if (!parse) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: invalid json data: %s",
args.c_str());
return errorInJson(PARSING_ERROR, "Invalid json data!");
}
Json::Value sv = root["numberString"];
if (sv.isNull()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: no numberString provided!");
return errorInJson(FORMATTING_ERROR, "No numberString provided!");
}
if (!sv.isString()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: invalid numberString type: %d!",
sv.type());
return errorInJson(FORMATTING_ERROR, "Invalid numberString type!");
}
std::string str = sv.asString();
if (str.empty()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: empty numberString!");
return errorInJson(FORMATTING_ERROR, "Empty numberString!");
}
// This is the default value when no options provided.
ENumberType type = kNumberDecimal;
Json::Value options = root["options"];
std::string error;
if (!handleNumberOptions(options, type, error))
return errorInJson(PARSING_ERROR, error);
UErrorCode status = U_ZERO_ERROR;
NumberFormat* nf;
switch (type) {
case kNumberDecimal:
default:
nf = NumberFormat::createInstance(status);
break;
case kNumberCurrency:
nf = NumberFormat::createCurrencyInstance(status);
break;
case kNumberPercent:
nf = NumberFormat::createPercentInstance(status);
break;
}
if (!nf) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: failed to create NumberFormat instance for type %d: %d",
status, type);
return errorInJson(UNKNOWN_ERROR, "Failed to create NumberFormat instance!");
}
std::auto_ptr<NumberFormat> deleter(nf);
UnicodeString uStr = UnicodeString::fromUTF8(str);
Formattable value;
if (type == kNumberCurrency) {
ParsePosition pos;
CurrencyAmount* ca = nf->parseCurrency(uStr, pos);
if (ca)
value = ca->getNumber();
else
nf->parse(uStr, value, status);
} else
nf->parse(uStr, value, status);
if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: failed (%d) to parse string: %s",
status, str.c_str());
return errorInJson(PARSING_ERROR, "Failed to parse string!");
}
if (!value.isNumeric()) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToNumber: string is not numeric: %s",
str.c_str());
return errorInJson(FORMATTING_ERROR, "String is not numeric!");
}
return resultInJson(value.getDouble());
}