/** * Returns a pointer to a Region using the given numeric region code. If the numeric region code is not recognized, * the appropriate error code will be set ( U_ILLEGAL_ARGUMENT_ERROR ). */ const Region* U_EXPORT2 Region::getInstance (int32_t code, UErrorCode &status) { loadRegionData(); Region *r = (Region *)uhash_iget(numericCodeMap,code); if ( !r ) { // Just in case there's an alias that's numeric, try to find it. UErrorCode fs = U_ZERO_ERROR; UnicodeString pat = UNICODE_STRING_SIMPLE("00#"); DecimalFormat *df = new DecimalFormat(pat,fs); UnicodeString id; id.remove(); df->format(code,id); delete df; r = (Region *)uhash_get(regionAliases,&id); } if ( !r ) { status = U_ILLEGAL_ARGUMENT_ERROR; return NULL; } if ( r->type == URGN_DEPRECATED && r->preferredValues->size() == 1) { StringEnumeration *pv = r->getPreferredValues(); pv->reset(status); const UnicodeString *ustr = pv->snext(status); r = (Region *)uhash_get(regionIDMap,(void *)ustr); delete pv; } return r; }
static jcharArray format(JNIEnv* env, jint addr, jobject fpIter, T val) { UErrorCode status = U_ZERO_ERROR; UnicodeString str; DecimalFormat* fmt = toDecimalFormat(addr); FieldPositionIterator fpi; FieldPositionIterator* pfpi = fpIter ? &fpi : NULL; fmt->format(reinterpret_cast<const Formattable&>(val), str, pfpi, status); return formatResult(env, str, pfpi, fpIter); }
std::string AbstractModel::getAllOutcomes(double ocs[]) { if (sizeof(ocs) / sizeof(ocs[0]) != outcomeNames->length) { return "The double array sent as a parameter to GISModel.getAllOutcomes() must not have been produced by this model."; } else { DecimalFormat *df = new DecimalFormat("0.0000"); StringBuffer *sb = new StringBuffer(sizeof(ocs) / sizeof(ocs[0])*2); sb->append(outcomeNames[0])->append("[")->append(df->format(ocs[0]))->append("]"); for (int i = 1; i < sizeof(ocs) / sizeof(ocs[0]); i++) { sb->append(" ")->append(outcomeNames[i])->append("[")->append(df->format(ocs[i]))->append("]"); } //JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to 'toString': return sb->toString(); } }
void ParsePositionTest::TestFieldPosition_example() { //***** no error detection yet !!!!!!! //***** this test is for compiler checks and visual verification only. double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789, 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789}; int dNumSize = 9; UErrorCode status = U_ZERO_ERROR; NumberFormat *nf = NumberFormat::createInstance(status); if (failure(status, "NumberFormat::createInstance", TRUE)){ delete nf; return; }; DecimalFormat *fmt = dynamic_cast<DecimalFormat *>(nf); if(fmt == NULL) { errln("NumberFormat::createInstance returned unexpected class type"); return; } fmt->setDecimalSeparatorAlwaysShown(TRUE); const int tempLen = 20; UnicodeString temp; for (int i=0; i < dNumSize; i++) { temp.remove(); //temp = new StringBuffer(); // Get new buffer FieldPosition pos(NumberFormat::INTEGER_FIELD); UnicodeString buf;// = new StringBuffer(); //char fmtText[tempLen]; //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText); UnicodeString res; res = fmt->format(doubleNum[i], buf, pos); int tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ? tempLen : (tempLen - pos.getEndIndex()); for (int j=0; j<tempOffset; j++) temp += UnicodeString("="/*'='*/); // initialize logln("FP " + temp + res); } logln(""); delete nf; }
static jcharArray format(JNIEnv* env, jlong addr, jobject javaFieldPositionIterator, T value) { UErrorCode status = U_ZERO_ERROR; UnicodeString s; DecimalFormat* fmt = toDecimalFormat(addr); FieldPositionIterator nativeFieldPositionIterator; FieldPositionIterator* fpi = javaFieldPositionIterator ? &nativeFieldPositionIterator : NULL; fmt->format(value, s, fpi, status); if (maybeThrowIcuException(env, "DecimalFormat::format", status)) { return NULL; } return formatResult(env, s, fpi, javaFieldPositionIterator); }
void test_FieldPosition_example( void ) { //***** no error detection yet !!!!!!! //***** this test is for compiler checks and visual verification only. double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789, 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789}; int32_t dNumSize = (int32_t)(sizeof(doubleNum)/sizeof(double)); UErrorCode status = U_ZERO_ERROR; DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status); if (U_FAILURE(status)) { it_dataerrln("NumberFormat::createInstance() error"); return; } fmt->setDecimalSeparatorAlwaysShown(TRUE); const int32_t tempLen = 20; char temp[tempLen]; for (int32_t i=0; i<dNumSize; i++) { FieldPosition pos(NumberFormat::INTEGER_FIELD); UnicodeString buf; //char fmtText[tempLen]; //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText); UnicodeString res = fmt->format(doubleNum[i], buf, pos); for (int32_t j=0; j<tempLen; j++) temp[j] = '='; // clear with spaces int32_t tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ? tempLen : (tempLen - pos.getEndIndex()); temp[tempOffset] = '\0'; it_logln(UnicodeString("FP ") + UnicodeString(temp) + res); } delete fmt; it_logln(""); }
UnicodeString& PluralFormat::format(const Formattable& numberObject, double number, UnicodeString& appendTo, FieldPosition& pos, UErrorCode& status) const { if (U_FAILURE(status)) { return appendTo; } if (msgPattern.countParts() == 0) { return numberFormat->format(numberObject, appendTo, pos, status); } // Get the appropriate sub-message. // Select it based on the formatted number-offset. double numberMinusOffset = number - offset; UnicodeString numberString; FieldPosition ignorePos; FixedPrecision fp; VisibleDigitsWithExponent dec; fp.initVisibleDigitsWithExponent(numberMinusOffset, dec, status); if (U_FAILURE(status)) { return appendTo; } if (offset == 0) { DecimalFormat *decFmt = dynamic_cast<DecimalFormat *>(numberFormat); if(decFmt != NULL) { decFmt->initVisibleDigitsWithExponent( numberObject, dec, status); if (U_FAILURE(status)) { return appendTo; } decFmt->format(dec, numberString, ignorePos, status); } else { numberFormat->format( numberObject, numberString, ignorePos, status); // could be BigDecimal etc. } } else { DecimalFormat *decFmt = dynamic_cast<DecimalFormat *>(numberFormat); if(decFmt != NULL) { decFmt->initVisibleDigitsWithExponent( numberMinusOffset, dec, status); if (U_FAILURE(status)) { return appendTo; } decFmt->format(dec, numberString, ignorePos, status); } else { numberFormat->format( numberMinusOffset, numberString, ignorePos, status); } } int32_t partIndex = findSubMessage(msgPattern, 0, pluralRulesWrapper, &dec, number, status); if (U_FAILURE(status)) { return appendTo; } // Replace syntactic # signs in the top level of this sub-message // (not in nested arguments) with the formatted number-offset. const UnicodeString& pattern = msgPattern.getPatternString(); int32_t prevIndex = msgPattern.getPart(partIndex).getLimit(); for (;;) { const MessagePattern::Part& part = msgPattern.getPart(++partIndex); const UMessagePatternPartType type = part.getType(); int32_t index = part.getIndex(); if (type == UMSGPAT_PART_TYPE_MSG_LIMIT) { return appendTo.append(pattern, prevIndex, index - prevIndex); } else if ((type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) || (type == UMSGPAT_PART_TYPE_SKIP_SYNTAX && MessageImpl::jdkAposMode(msgPattern))) { appendTo.append(pattern, prevIndex, index - prevIndex); if (type == UMSGPAT_PART_TYPE_REPLACE_NUMBER) { appendTo.append(numberString); } prevIndex = part.getLimit(); } else if (type == UMSGPAT_PART_TYPE_ARG_START) { appendTo.append(pattern, prevIndex, index - prevIndex); prevIndex = index; partIndex = msgPattern.getLimitPartIndex(partIndex); index = msgPattern.getPart(partIndex).getLimit(); MessageImpl::appendReducedApostrophes(pattern, prevIndex, index, appendTo); prevIndex = index; } } }
void TestMessageFormat::testBug3() { double myNumber = -123456; DecimalFormat *form = 0; Locale locale[] = { Locale("ar", "", ""), Locale("be", "", ""), Locale("bg", "", ""), Locale("ca", "", ""), Locale("cs", "", ""), Locale("da", "", ""), Locale("de", "", ""), Locale("de", "AT", ""), Locale("de", "CH", ""), Locale("el", "", ""), // 10 Locale("en", "CA", ""), Locale("en", "GB", ""), Locale("en", "IE", ""), Locale("en", "US", ""), Locale("es", "", ""), Locale("et", "", ""), Locale("fi", "", ""), Locale("fr", "", ""), Locale("fr", "BE", ""), Locale("fr", "CA", ""), // 20 Locale("fr", "CH", ""), Locale("he", "", ""), Locale("hr", "", ""), Locale("hu", "", ""), Locale("is", "", ""), Locale("it", "", ""), Locale("it", "CH", ""), Locale("ja", "", ""), Locale("ko", "", ""), Locale("lt", "", ""), // 30 Locale("lv", "", ""), Locale("mk", "", ""), Locale("nl", "", ""), Locale("nl", "BE", ""), Locale("no", "", ""), Locale("pl", "", ""), Locale("pt", "", ""), Locale("ro", "", ""), Locale("ru", "", ""), Locale("sh", "", ""), // 40 Locale("sk", "", ""), Locale("sl", "", ""), Locale("sq", "", ""), Locale("sr", "", ""), Locale("sv", "", ""), Locale("tr", "", ""), Locale("uk", "", ""), Locale("zh", "", ""), Locale("zh", "TW", "") // 49 }; int32_t i; for (i= 0; i < 49; i++) { UnicodeString buffer; logln(locale[i].getDisplayName(buffer)); UErrorCode success = U_ZERO_ERROR; // form = (DecimalFormat*)NumberFormat::createCurrencyInstance(locale[i], success); form = (DecimalFormat*)NumberFormat::createInstance(locale[i], success); if (U_FAILURE(success)) { errln("Err: Number Format "); logln("Number format creation failed."); continue; } Formattable result; FieldPosition pos(0); buffer.remove(); form->format(myNumber, buffer, pos); success = U_ZERO_ERROR; ParsePosition parsePos; form->parse(buffer, result, parsePos); logln(UnicodeString(" -> ") /* + << dec*/ + toString(result) + UnicodeString("[supposed output for result]")); if (U_FAILURE(success)) { errln("Err: Number Format parse"); logln("Number format parse failed."); } delete form; } }