本文整理汇总了C++中NumberFormat::setMaximumFractionDigits方法的典型用法代码示例。如果您正苦于以下问题:C++ NumberFormat::setMaximumFractionDigits方法的具体用法?C++ NumberFormat::setMaximumFractionDigits怎么用?C++ NumberFormat::setMaximumFractionDigits使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NumberFormat
的用法示例。
在下文中一共展示了NumberFormat::setMaximumFractionDigits方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TestCustomNumberFormat
void RelativeDateTimeFormatterTest::TestCustomNumberFormat() {
NumberFormat *nf;
UErrorCode status = U_ZERO_ERROR;
{
RelativeDateTimeFormatter fmt("en", status);
if (U_FAILURE(status)) {
dataerrln(
"Failure creating format object - %s", u_errorName(status));
return;
}
nf = (NumberFormat *) fmt.getNumberFormat().clone();
}
nf->setMinimumFractionDigits(1);
nf->setMaximumFractionDigits(1);
RelativeDateTimeFormatter fmt("en", nf, status);
// Test copy constructor.
RelativeDateTimeFormatter fmt2(fmt);
RunTest(fmt2, kEnglishDecimal, UPRV_LENGTHOF(kEnglishDecimal), "en decimal digits");
// Test assignment
fmt = RelativeDateTimeFormatter("es", status);
RunTest(fmt, kSpanishNoQuantity, UPRV_LENGTHOF(kSpanishNoQuantity), "assignment operator");
}
示例2: unitsBundle
template<> U_I18N_API
const MeasureFormatCacheData *LocaleCacheKey<MeasureFormatCacheData>::createObject(
const void * /*unused*/, UErrorCode &status) const {
const char *localeId = fLoc.getName();
LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, localeId, &status));
static UNumberFormatStyle currencyStyles[] = {
UNUM_CURRENCY_PLURAL, UNUM_CURRENCY_ISO, UNUM_CURRENCY};
LocalPointer<MeasureFormatCacheData> result(new MeasureFormatCacheData(), status);
if (U_FAILURE(status)) {
return NULL;
}
if (!loadMeasureUnitData(
unitsBundle.getAlias(),
*result,
status)) {
return NULL;
}
result->adoptNumericDateFormatters(loadNumericDateFormatters(
unitsBundle.getAlias(), status));
if (U_FAILURE(status)) {
return NULL;
}
for (int32_t i = 0; i < WIDTH_INDEX_COUNT; ++i) {
// NumberFormat::createInstance can erase warning codes from status, so pass it
// a separate status instance
UErrorCode localStatus = U_ZERO_ERROR;
result->adoptCurrencyFormat(i, NumberFormat::createInstance(
localeId, currencyStyles[i], localStatus));
if (localStatus != U_ZERO_ERROR) {
status = localStatus;
}
if (U_FAILURE(status)) {
return NULL;
}
}
NumberFormat *inf = NumberFormat::createInstance(
localeId, UNUM_DECIMAL, status);
if (U_FAILURE(status)) {
return NULL;
}
inf->setMaximumFractionDigits(0);
DecimalFormat *decfmt = dynamic_cast<DecimalFormat *>(inf);
if (decfmt != NULL) {
decfmt->setRoundingMode(DecimalFormat::kRoundDown);
}
result->adoptIntegerFormat(inf);
result->addRef();
return result.orphan();
}
示例3: convertToQt
QString
PercentValcon::format()
{
// Format money using ICU default percent format
UnicodeString text;
UErrorCode status = U_ZERO_ERROR;
NumberFormat* fmt = NumberFormat::createPercentInstance(status);
fmt->setMaximumFractionDigits(4);
fmt->format(_value.toDouble() / 100.0, text, status);
if (U_FAILURE(status))
return "";
return convertToQt(text);
}
示例4: testGreekWithSanitization
// Test bug9042
void TimeUnitTest::testGreekWithSanitization() {
UErrorCode status = U_ZERO_ERROR;
Locale elLoc("el");
NumberFormat* numberFmt = NumberFormat::createInstance(Locale("el"), status);
if (!assertSuccess("NumberFormat::createInstance for el locale", status, TRUE)) return;
numberFmt->setMaximumFractionDigits(1);
TimeUnitFormat* timeUnitFormat = new TimeUnitFormat(elLoc, status);
if (!assertSuccess("TimeUnitFormat::TimeUnitFormat for el locale", status)) return;
timeUnitFormat->setNumberFormat(*numberFmt, status);
delete numberFmt;
delete timeUnitFormat;
}
示例5: testAPI
//.........这里部分代码省略.........
}
// ======= Test parse()
if (fr != NULL)
{
logln("Testing parse()");
double d = -10456.0037;
UnicodeString text("-10,456.0037");
Formattable result1, result2, result3;
ParsePosition pos(0), pos01(0);
fr->parseObject(text, result1, pos);
if(result1.getType() != Formattable::kDouble && result1.getDouble() != d) {
errln("ERROR: Roundtrip failed (via parse()) for " + text);
}
logln(text + " parsed into " + (int32_t) result1.getDouble());
fr->parse(text, result2, pos01);
if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
errln("ERROR: Roundtrip failed (via parse()) for " + text);
}
logln(text + " parsed into " + (int32_t) result2.getDouble());
status = U_ZERO_ERROR;
fr->parse(text, result3, status);
if(U_FAILURE(status)) {
errln("ERROR: parse() failed");
}
if(result3.getType() != Formattable::kDouble && result3.getDouble() != d) {
errln("ERROR: Roundtrip failed (via parse()) for " + text);
}
logln(text + " parsed into " + (int32_t) result3.getDouble());
}
// ======= Test getters and setters
if (fr != NULL && def != NULL)
{
logln("Testing getters and setters");
int32_t count = 0;
const Locale *locales = NumberFormat::getAvailableLocales(count);
logln((UnicodeString) "Got " + count + " locales" );
for(int32_t i = 0; i < count; i++) {
UnicodeString name(locales[i].getName(),"");
logln(name);
}
fr->setParseIntegerOnly( def->isParseIntegerOnly() );
if(fr->isParseIntegerOnly() != def->isParseIntegerOnly() ) {
errln("ERROR: setParseIntegerOnly() failed");
}
fr->setGroupingUsed( def->isGroupingUsed() );
if(fr->isGroupingUsed() != def->isGroupingUsed() ) {
errln("ERROR: setGroupingUsed() failed");
}
fr->setMaximumIntegerDigits( def->getMaximumIntegerDigits() );
if(fr->getMaximumIntegerDigits() != def->getMaximumIntegerDigits() ) {
errln("ERROR: setMaximumIntegerDigits() failed");
}
fr->setMinimumIntegerDigits( def->getMinimumIntegerDigits() );
if(fr->getMinimumIntegerDigits() != def->getMinimumIntegerDigits() ) {
errln("ERROR: setMinimumIntegerDigits() failed");
}
fr->setMaximumFractionDigits( def->getMaximumFractionDigits() );
if(fr->getMaximumFractionDigits() != def->getMaximumFractionDigits() ) {
errln("ERROR: setMaximumFractionDigits() failed");
}
fr->setMinimumFractionDigits( def->getMinimumFractionDigits() );
if(fr->getMinimumFractionDigits() != def->getMinimumFractionDigits() ) {
errln("ERROR: setMinimumFractionDigits() failed");
}
}
// ======= Test getStaticClassID()
logln("Testing getStaticClassID()");
status = U_ZERO_ERROR;
NumberFormat *test = new DecimalFormat(status);
if(U_FAILURE(status)) {
errln("ERROR: Couldn't create a NumberFormat");
}
if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
errln("ERROR: getDynamicClassID() didn't return the expected value");
}
delete test;
delete def;
delete fr;
delete cur;
delete cur_fr;
delete per;
delete per_fr;
}
示例6: symbols
/*
* Set a currency on a NumberFormat with pre-ICU 2.6 APIs.
* This is a "hack" that will not work properly for all cases because
* only ICU 2.6 introduced a more complete framework and data for this.
*
* @param nf The NumberFormat on which to set the currency; takes effect on
* currency-formatting NumberFormat instances.
* This must actually be a DecimalFormat instance.
* The display style of the output is controlled by nf (its pattern,
* usually from the display locale ID used to create this instance)
* while the currency symbol and number of decimals are set for
* the currency.
* @param currency The 3-letter ISO 4217 currency code, NUL-terminated.
* @param errorCode ICU error code, must pass U_SUCCESS() on input.
*/
static void
setNumberFormatCurrency_2_4(NumberFormat &nf, const char *currency, UErrorCode &errorCode) {
// argument checking
if(U_FAILURE(errorCode)) {
return;
}
if(currency==NULL || strlen(currency)!=3) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
return;
}
// check that the formatter is a DecimalFormat instance
// necessary because we will cast to the DecimalFormat subclass to set
// the currency symbol
DecimalFormat *dnf=dynamic_cast<DecimalFormat *>(&nf);
if(dnf==NULL) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
return;
}
// map the currency code to a locale ID
// only the currencies in this array are supported
// it would be possible to map to a locale ID, instantiate a currency
// formatter for that and copy its values, but that would be slower,
// and we have to hardcode something here anyway
static const struct {
// ISO currency ID
const char *currency;
// fractionDigits==minimumFractionDigits==maximumFractionDigits
// for these currencies
int32_t fractionDigits;
/*
* Set the rounding increment to 0 if it is implied with the number of
* fraction digits. Setting an explicit rounding increment makes
* number formatting slower.
* In other words, set it to something other than 0 only for unusual
* cases like "nickel rounding" (0.05) when the increment differs from
* 10^(-maximumFractionDigits).
*/
double roundingIncrement;
// Unicode string with the desired currency display symbol or name
UChar symbol[16];
} currencyMap[]={
{ "USD", 2, 0.0, { 0x24, 0 } },
{ "GBP", 2, 0.0, { 0xa3, 0 } },
{ "EUR", 2, 0.0, { 0x20ac, 0 } },
{ "JPY", 0, 0.0, { 0xa5, 0 } }
};
int32_t i;
for(i=0; i<UPRV_LENGTHOF(currencyMap); ++i) {
if(strcmp(currency, currencyMap[i].currency)==0) {
break;
}
}
if(i==UPRV_LENGTHOF(currencyMap)) {
// a more specific error code would be useful in a real application
errorCode=U_UNSUPPORTED_ERROR;
return;
}
// set the currency-related data into the caller's formatter
nf.setMinimumFractionDigits(currencyMap[i].fractionDigits);
nf.setMaximumFractionDigits(currencyMap[i].fractionDigits);
dnf->setRoundingIncrement(currencyMap[i].roundingIncrement);
DecimalFormatSymbols symbols(*dnf->getDecimalFormatSymbols());
symbols.setSymbol(DecimalFormatSymbols::kCurrencySymbol, currencyMap[i].symbol);
dnf->setDecimalFormatSymbols(symbols); // do not adopt symbols: Jitterbug 2889
}