A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/numberformatter_8h.html below:

ICU 77.1: i18n/unicode/numberformatter.h File Reference

C++ API: All-in-one formatter for localized numbers, currencies, and units. More...

C++ API: All-in-one formatter for localized numbers, currencies, and units.

For a full list of options, see icu::number::NumberFormatterSettings.

// Most basic usage:
NumberFormatter::withLocale(...).format(123).toString();  // 1,234 in en-US

// Custom notation, unit, and rounding precision:
NumberFormatter::with()
    .notation(Notation::compactShort())
    .unit(CurrencyUnit("EUR", status))
    .precision(Precision::maxDigits(2))
    .locale(...)
    .format(1234)
    .toString();  // €1.2K in en-US

// Create a formatter in a singleton by value for use later:
static const LocalizedNumberFormatter formatter = NumberFormatter::withLocale(...)
    .unit(NoUnit::percent())
    .precision(Precision::fixedFraction(3));
formatter.format(5.9831).toString();  // 5.983% in en-US

// Create a "template" in a singleton unique_ptr but without setting a locale until the call site:
std::unique_ptr<UnlocalizedNumberFormatter> template = NumberFormatter::with()
    .sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS)
    .unit(MeasureUnit::getMeter())
    .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME)
    .clone();
template->locale(...).format(1234).toString();  // +1,234 meters in en-US

This API offers more features than DecimalFormat and is geared toward new users of ICU.

NumberFormatter instances (i.e., LocalizedNumberFormatter and UnlocalizedNumberFormatter) are immutable and thread safe. This means that invoking a configuration method has no effect on the receiving instance; you must store and use the new number formatter instance it returns instead.

UnlocalizedNumberFormatter formatter = UnlocalizedNumberFormatter::with().notation(Notation::scientific());
formatter.precision(Precision.maxFraction(2)); // does nothing!
formatter.locale(Locale.getEnglish()).format(9.8765).toString(); // prints "9.8765E0", not "9.88E0"

This API is based on the fluent design pattern popularized by libraries such as Google's Guava. For extensive details on the design of this API, read the design doc.

Note: To format monetary/currency values, specify the currency in the .unit() function.

Definition in file numberformatter.h.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4