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; }
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(""); }
U_CAPI void U_EXPORT2 unum_setAttribute( UNumberFormat* fmt, UNumberFormatAttribute attr, int32_t newValue) { if (((NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) { DecimalFormat* df = (DecimalFormat*) fmt; switch(attr) { case UNUM_PARSE_INT_ONLY: df->setParseIntegerOnly(newValue!=0); break; case UNUM_GROUPING_USED: df->setGroupingUsed(newValue!=0); break; case UNUM_DECIMAL_ALWAYS_SHOWN: df->setDecimalSeparatorAlwaysShown(newValue!=0); break; case UNUM_MAX_INTEGER_DIGITS: df->setMaximumIntegerDigits(newValue); break; case UNUM_MIN_INTEGER_DIGITS: df->setMinimumIntegerDigits(newValue); break; case UNUM_INTEGER_DIGITS: df->setMinimumIntegerDigits(newValue); df->setMaximumIntegerDigits(newValue); break; case UNUM_MAX_FRACTION_DIGITS: df->setMaximumFractionDigits(newValue); break; case UNUM_MIN_FRACTION_DIGITS: df->setMinimumFractionDigits(newValue); break; case UNUM_FRACTION_DIGITS: df->setMinimumFractionDigits(newValue); df->setMaximumFractionDigits(newValue); break; case UNUM_SIGNIFICANT_DIGITS_USED: df->setSignificantDigitsUsed(newValue!=0); break; case UNUM_MAX_SIGNIFICANT_DIGITS: df->setMaximumSignificantDigits(newValue); break; case UNUM_MIN_SIGNIFICANT_DIGITS: df->setMinimumSignificantDigits(newValue); break; case UNUM_MULTIPLIER: df->setMultiplier(newValue); break; case UNUM_GROUPING_SIZE: df->setGroupingSize(newValue); break; case UNUM_ROUNDING_MODE: df->setRoundingMode((DecimalFormat::ERoundingMode)newValue); break; case UNUM_FORMAT_WIDTH: df->setFormatWidth(newValue); break; case UNUM_PADDING_POSITION: /** The position at which padding will take place. */ df->setPadPosition((DecimalFormat::EPadPosition)newValue); break; case UNUM_SECONDARY_GROUPING_SIZE: df->setSecondaryGroupingSize(newValue); break; default: /* Shouldn't get here anyway */ break; } } else { U_ASSERT(((NumberFormat*)fmt)->getDynamicClassID() == RuleBasedNumberFormat::getStaticClassID()); if (attr == UNUM_LENIENT_PARSE) { #if !UCONFIG_NO_COLLATION ((RuleBasedNumberFormat*)fmt)->setLenient((UBool)newValue); #endif } } }
U_CAPI void U_EXPORT2 unum_setAttribute( UNumberFormat* fmt, UNumberFormatAttribute attr, int32_t newValue) { NumberFormat* nf = reinterpret_cast<NumberFormat*>(fmt); if ( attr == UNUM_LENIENT_PARSE ) { // Supported for all subclasses return nf->setLenient(newValue != 0); } // The remaining attributea are only supported for DecimalFormat DecimalFormat* df = dynamic_cast<DecimalFormat*>(nf); if (df != NULL) { switch(attr) { case UNUM_PARSE_INT_ONLY: df->setParseIntegerOnly(newValue!=0); break; case UNUM_GROUPING_USED: df->setGroupingUsed(newValue!=0); break; case UNUM_DECIMAL_ALWAYS_SHOWN: df->setDecimalSeparatorAlwaysShown(newValue!=0); break; case UNUM_MAX_INTEGER_DIGITS: df->setMaximumIntegerDigits(newValue); break; case UNUM_MIN_INTEGER_DIGITS: df->setMinimumIntegerDigits(newValue); break; case UNUM_INTEGER_DIGITS: df->setMinimumIntegerDigits(newValue); df->setMaximumIntegerDigits(newValue); break; case UNUM_MAX_FRACTION_DIGITS: df->setMaximumFractionDigits(newValue); break; case UNUM_MIN_FRACTION_DIGITS: df->setMinimumFractionDigits(newValue); break; case UNUM_FRACTION_DIGITS: df->setMinimumFractionDigits(newValue); df->setMaximumFractionDigits(newValue); break; case UNUM_SIGNIFICANT_DIGITS_USED: df->setSignificantDigitsUsed(newValue!=0); break; case UNUM_MAX_SIGNIFICANT_DIGITS: df->setMaximumSignificantDigits(newValue); break; case UNUM_MIN_SIGNIFICANT_DIGITS: df->setMinimumSignificantDigits(newValue); break; case UNUM_MULTIPLIER: df->setMultiplier(newValue); break; case UNUM_GROUPING_SIZE: df->setGroupingSize(newValue); break; case UNUM_ROUNDING_MODE: df->setRoundingMode((DecimalFormat::ERoundingMode)newValue); break; case UNUM_FORMAT_WIDTH: df->setFormatWidth(newValue); break; case UNUM_PADDING_POSITION: /** The position at which padding will take place. */ df->setPadPosition((DecimalFormat::EPadPosition)newValue); break; case UNUM_SECONDARY_GROUPING_SIZE: df->setSecondaryGroupingSize(newValue); break; default: /* Shouldn't get here anyway */ break; } } }