Go to the documentation of this file.
4 #ifndef __NUMBERFORMATTER_H__
5 #define __NUMBERFORMATTER_H__
9 #if U_SHOW_CPLUSPLUS_API
11 #if !UCONFIG_NO_FORMATTING
25 #include "unicode/unounclass.h"
89 class FieldPositionIteratorHandler;
90 class FormattedStringBuilder;
96 class NumberParserImpl;
97 class MultiplierParseHandler;
112 class UnlocalizedNumberFormatter;
113 class LocalizedNumberFormatter;
114 class FormattedNumber;
116 class ScientificNotation;
118 class FractionPrecision;
119 class CurrencyPrecision;
120 class IncrementPrecision;
140 static constexpr int32_t kInternalDefaultThreshold = 3;
146 class DecimalQuantity;
147 class UFormattedNumberData;
148 class NumberFormatterImpl;
149 struct ParsedPatternInfo;
150 class ScientificModifier;
151 class MultiplierProducer;
153 class ScientificHandler;
155 class AffixPatternProvider;
156 class NumberPropertyMapper;
157 struct DecimalFormatProperties;
158 class MultiplierFormatHandler;
159 class CurrencySymbols;
160 class GeneratorHelpers;
162 class NumberRangeFormatterImpl;
164 struct UFormattedNumberImpl;
165 class MutablePatternModifier;
166 class ImmutablePatternModifier;
167 struct DecimalFormatWarehouse;
345 NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR
348 union NotationUnion {
371 Notation(
const NotationType &type,
const NotationUnion &union_) : fType(type), fUnion(union_) {}
374 fUnion.errorCode = errorCode;
377 Notation() : fType(NTN_SIMPLE), fUnion() {}
380 if (fType == NTN_ERROR) {
381 status = fUnion.errorCode;
388 friend struct impl::MacroProps;
389 friend class ScientificNotation;
392 friend class impl::NumberFormatterImpl;
393 friend class impl::ScientificModifier;
394 friend class impl::ScientificHandler;
397 friend class impl::GeneratorHelpers;
442 using Notation::Notation;
451 friend class impl::NumberPropertyMapper;
621 int32_t maxSignificantDigits);
644 #ifndef U_HIDE_DRAFT_API
669 #endif // U_HIDE_DRAFT_API
705 RND_FRACTION_SIGNIFICANT,
722 union PrecisionUnion {
763 Precision(
const PrecisionType& type,
const PrecisionUnion& union_)
764 : fType(type), fUnion(union_) {}
767 fUnion.errorCode = errorCode;
770 Precision() : fType(RND_BOGUS) {}
772 bool isBogus()
const {
773 return fType == RND_BOGUS;
777 if (fType == RND_ERROR) {
778 status = fUnion.errorCode;
785 Precision withCurrency(
const CurrencyUnit ¤cy,
UErrorCode &status)
const;
787 static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
789 static Precision constructSignificant(int32_t minSig, int32_t maxSig);
791 static Precision constructFractionSignificant(
792 const FractionPrecision &base,
798 static IncrementPrecision constructIncrement(uint64_t increment, impl::digits_t magnitude);
803 friend struct impl::MacroProps;
804 friend struct impl::MicroProps;
807 friend class impl::NumberFormatterImpl;
810 friend class impl::NumberPropertyMapper;
813 friend class impl::RoundingImpl;
816 friend class FractionPrecision;
817 friend class CurrencyPrecision;
818 friend class IncrementPrecision;
821 friend class impl::GeneratorHelpers;
824 friend class units::UnitsRouter;
853 int32_t minSignificantDigits,
854 int32_t maxSignificantDigits,
897 using Precision::Precision;
935 using Precision::Precision;
971 using Precision::Precision;
1019 bool fFormatFailIfMoreThanMaxDigits;
1023 bool fHasError =
false;
1025 IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt,
bool formatFailIfMoreThanMaxDigits);
1028 fUnion.errorCode = errorCode;
1033 fUnion.minMaxInt.fMinInt = -1;
1037 static IntegerWidth standard() {
1038 return IntegerWidth::zeroFillTo(1);
1041 bool isBogus()
const {
1042 return !fHasError && fUnion.minMaxInt.fMinInt == -1;
1047 status = fUnion.errorCode;
1053 void apply(impl::DecimalQuantity &quantity,
UErrorCode &status)
const;
1055 bool operator==(
const IntegerWidth& other)
const;
1058 friend struct impl::MacroProps;
1059 friend struct impl::MicroProps;
1062 friend class impl::NumberFormatterImpl;
1065 friend class impl::MutablePatternModifier;
1066 friend class impl::ImmutablePatternModifier;
1069 friend class impl::NumberPropertyMapper;
1072 friend class impl::GeneratorHelpers;
1155 #ifndef U_HIDE_INTERNAL_API
1157 Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt);
1162 impl::DecNum* fArbitrary;
1165 Scale(
UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1169 bool isValid()
const {
1170 return fMagnitude != 0 || fArbitrary !=
nullptr;
1181 void applyTo(impl::DecimalQuantity& quantity)
const;
1183 void applyReciprocalTo(impl::DecimalQuantity& quantity)
const;
1186 friend struct impl::MacroProps;
1187 friend struct impl::MicroProps;
1190 friend class impl::NumberFormatterImpl;
1193 friend class impl::MultiplierFormatHandler;
1196 friend class impl::GeneratorHelpers;
1199 friend class ::icu::numparse::impl::NumberParserImpl;
1200 friend class ::icu::numparse::impl::MultiplierParseHandler;
1223 #ifndef U_HIDE_INTERNAL_API
1246 #endif // U_HIDE_INTERNAL_API
1266 friend class impl::NumberFormatterImpl;
1269 friend class impl::GeneratorHelpers;
1273 friend struct impl::MacroProps;
1298 #ifndef U_HIDE_INTERNAL_API
1336 #endif // U_HIDE_INTERNAL_API
1340 if (fType == SYMPTR_DFS && fPtr.dfs ==
nullptr) {
1343 }
else if (fType == SYMPTR_NS && fPtr.ns ==
nullptr) {
1351 enum SymbolsPointerType {
1352 SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1360 void doCopyFrom(
const SymbolsWrapper &other);
1362 void doMoveFrom(SymbolsWrapper&& src);
1371 #ifndef U_HIDE_INTERNAL_API
1385 : fGrouping1(grouping1),
1386 fGrouping2(grouping2),
1387 fMinGrouping(minGrouping),
1388 fStrategy(strategy) {}
1395 #endif // U_HIDE_INTERNAL_API
1416 int16_t fMinGrouping;
1426 bool isBogus()
const {
1427 return fGrouping1 == -3;
1431 void setLocaleData(
const impl::ParsedPatternInfo &patternInfo,
const Locale& locale);
1433 bool groupAtPosition(int32_t position,
const impl::DecimalQuantity &value)
const;
1436 friend struct MacroProps;
1437 friend struct MicroProps;
1440 friend class NumberFormatterImpl;
1443 friend class ::icu::numparse::impl::NumberParserImpl;
1446 friend class impl::GeneratorHelpers;
1453 #ifndef U_HIDE_INTERNAL_API
1462 #endif // U_HIDE_INTERNAL_API
1476 Padder(int32_t width);
1479 fUnion.errorCode = errorCode;
1482 Padder() : fWidth(-2) {}
1484 bool isBogus()
const {
1485 return fWidth == -2;
1490 status = fUnion.errorCode;
1496 bool isValid()
const {
1500 int32_t padAndApply(
const impl::Modifier &mod1,
const impl::Modifier &mod2,
1501 FormattedStringBuilder &
string, int32_t leftIndex, int32_t rightIndex,
1505 friend struct MacroProps;
1506 friend struct MicroProps;
1509 friend class impl::NumberFormatterImpl;
1512 friend class impl::GeneratorHelpers;
1554 bool approximately =
false;
1569 const AffixPatternProvider* affixProvider =
nullptr;
1575 int32_t threshold = kInternalDefaultThreshold;
1587 return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1588 padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1589 symbols.
copyErrorTo(status) || scale.copyErrorTo(status) || usage.copyErrorTo(status) ||
1590 unitDisplayCase.copyErrorTo(status);
1596 #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
1602 #pragma warning(push)
1603 #pragma warning(disable: 4661)
1611 template<
typename Derived>
2256 #ifndef U_HIDE_DRAFT_API
2257 #ifndef U_HIDE_INTERNAL_API
2272 #endif // U_HIDE_INTERNAL_API
2273 #endif // U_HIDE_DRAFT_API
2275 #ifndef U_HIDE_INTERNAL_API
2368 fMacros.copyErrorTo(outErrorCode);
2385 friend class impl::NumberRangeFormatterImpl;
2521 #ifndef U_HIDE_INTERNAL_API
2601 #ifndef U_HIDE_INTERNAL_API
2628 const impl::NumberFormatterImpl* fCompiled {
nullptr};
2629 char fUnsafeCallCount[8] {};
2633 const impl::DecimalFormatWarehouse* fWarehouse {
nullptr};
2635 explicit LocalizedNumberFormatter(
const NumberFormatterSettings<LocalizedNumberFormatter>& other);
2637 explicit LocalizedNumberFormatter(NumberFormatterSettings<LocalizedNumberFormatter>&& src)
U_NOEXCEPT;
2639 LocalizedNumberFormatter(
const impl::MacroProps ¯os,
const Locale &locale);
2641 LocalizedNumberFormatter(impl::MacroProps &¯os,
const Locale &locale);
2643 void resetCompiled();
2645 void lnfMoveHelper(LocalizedNumberFormatter&& src);
2647 void lnfCopyHelper(
const LocalizedNumberFormatter& src,
UErrorCode& status);
2652 bool computeCompiled(
UErrorCode& status)
const;
2655 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2656 friend class NumberFormatterSettings<LocalizedNumberFormatter>;
2659 friend class UnlocalizedNumberFormatter;
2662 #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
2664 #pragma warning(pop)
2755 template<typename StringClass>
2756 inline StringClass toDecimalNumber(
UErrorCode& status) const;
2771 #ifndef U_HIDE_DRAFT_API
2782 #endif // U_HIDE_DRAFT_API
2784 #ifndef U_HIDE_INTERNAL_API
2800 #ifndef U_HIDE_DEPRECATED_API
2814 const impl::UFormattedNumberData *fData;
2827 : fData(nullptr), fErrorCode(errorCode) {}
2832 friend class LocalizedNumberFormatter;
2835 friend struct impl::UFormattedNumberImpl;
2838 template<
typename StringClass>
2839 StringClass FormattedNumber::toDecimalNumber(
UErrorCode& status)
const {
2842 toDecimalNumber(sink, status);
2928 #endif // __NUMBERFORMATTER_H__
A class that defines a quantity by which a number should be multiplied when formatting.
ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const
Sets the minimum number of digits to show in the exponent of scientific notation, padding with zeros ...
int8_t fEngineeringInterval
A unit such as length, mass, volume, currency, etc.
static ScientificNotation engineering()
Print the number using engineering notation, a variant of scientific notation in which the exponent m...
C++ API: FieldPosition Iterator.
static Scale byDoubleAndPowerOfTen(double multiplicand, int32_t power)
Multiply a number by both a power of ten and by an arbitrary double value.
C API: Parse Error Information.
Precision withMinDigits(int32_t minSignificantDigits) const
Ensure that no less than this number of significant digits are retained when rounding according to fr...
UNumberRoundingPriority fPriority
Basic definitions for ICU, for both C and C++ APIs.
A unit of currency, such as USD (U.S.
A class that defines the strategy for padding and truncating integers before the decimal separator.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
static IntegerWidth zeroFillTo(int32_t minInt)
Pad numbers at the beginning with zeros to guarantee a certain number of numerals before the decimal ...
int8_t UBool
The ICU boolean type, a signed-byte integer.
A class that defines a rounding precision parameterized by a rounding increment to be used when forma...
void setTo(const NumberingSystem *ns)
Adopt the provided object.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Defines rules for mapping non-negative numeric values onto a small set of keywords.
static Grouper forStrategy(UNumberGroupingStrategy grouping)
static Grouper forProperties(const DecimalFormatProperties &properties)
Resolve the values in Properties to a Grouper object.
UCurrencyUsage
Currency Usage used for Decimal Format.
"Smart pointer" class, deletes objects via the standard C++ delete operator.
static Scale byDecimal(StringPiece multiplicand)
Multiply numbers by an arbitrary value before formatting.
static SimpleNotation simple()
Print the number using simple notation without any scaling by powers of ten.
Scale & operator=(Scale &&src) U_NOEXCEPT
static CurrencyPrecision currency(UCurrencyUsage currencyUsage)
Show numbers rounded and padded according to the rules for the currency unit.
Scale & operator=(const Scale &other)
Precision withMinFraction(int32_t minFrac) const
Specifies the minimum number of fraction digits to render after the decimal separator,...
static FractionPrecision maxFraction(int32_t maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal ...
bool isNumberingSystem() const
Whether the object is currently holding a NumberingSystem.
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
A UParseError struct is used to returned detailed information about parsing errors.
static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits)
Always show at least a certain number of significant digits/figures, padding with zeros if necessary.
static CompactNotation compactShort()
Print the number using short-form compact notation.
SymbolsWrapper(SymbolsWrapper &&src) U_NOEXCEPT
C++ API: FieldPosition identifies the fields in a formatted output.
C API: Compatibility APIs for number formatting.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
static FractionPrecision minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal ...
SymbolsWrapper(const SymbolsWrapper &other)
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
impl::digits_t fMinExponentDigits
StringProp unitDisplayCase
#define U_FAILURE(x)
Does the error code indicate a failure?
static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits or significant figures.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Precision trailingZeroDisplay(UNumberTrailingZeroDisplay trailingZeroDisplay) const
Configure how trailing zeros are displayed on numbers.
SymbolsWrapper & operator=(SymbolsWrapper &&src) U_NOEXCEPT
static FractionPrecision minFraction(int32_t minFractionPlaces)
Always show at least a certain number of fraction places after the decimal separator,...
UNumberSignDisplay fExponentSignDisplay
static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures.
A ByteSink can be filled with bytes.
IntegerWidth integerWidth
A class that defines the rounding precision to be used when formatting numbers in NumberFormatter.
UMemory is the common ICU base class.
StringProp & operator=(StringProp &&src) U_NOEXCEPT
StringProp & operator=(const StringProp &other)
static Scale powerOfTen(int32_t power)
Multiply numbers by a power of ten before formatting.
bool isDecimalFormatSymbols() const
Whether the object is currently holding a DecimalFormatSymbols.
C++ API: Symbols for formatting numbers.
static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position)
ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const
Sets whether to show the sign on positive and negative exponents in scientific notation.
@ U_ZERO_ERROR
No error, no warning.
StringProp(StringProp &&src) U_NOEXCEPT
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
int16_t getPrimary() const
Manages NumberFormatterSettings::usage()'s char* instance on the heap.
void setTo(const DecimalFormatSymbols &dfs)
The provided object is copied, but we do not adopt it.
int16_t getSecondary() const
static Padder forProperties(const DecimalFormatProperties &properties)
A class that defines a rounding precision based on a number of fraction places and optionally signifi...
C API: Encapsulates information about a currency.
A class that defines a rounding precision parameterized by a currency to be used when formatting numb...
@ UNUM_ROUND_HALFEVEN
Half-even rounding.
Base class for objects to which Unicode characters and strings can be appended.
static Scale byDouble(double multiplicand)
Multiply numbers by an arbitrary value before formatting.
SymbolsWrapper & operator=(const SymbolsWrapper &other)
Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
Defines numbering systems.
UBool copyErrorTo(UErrorCode &status) const
static ScientificNotation scientific()
Print the number using scientific notation (also known as scientific form, standard index form,...
static Scale none()
Do not change the value of numbers when formatting or parsing.
A class that defines the notation style to be used when formatting numbers in NumberFormatter.
A class that defines the scientific notation style to be used when formatting numbers in NumberFormat...
UNumberFormatRoundingMode
The possible number format rounding modes.
static CompactNotation compactLong()
Print the number using long-form compact notation.
static IncrementPrecision incrementExact(uint64_t mantissa, int16_t magnitude)
Version of Precision::increment() that takes an integer at a particular power of 10.
NounClass
Represents all the grammatical noun classes that are supported by CLDR.
Implementation of ByteSink that writes to a "string".
Precision withCurrency(const CurrencyUnit ¤cy) const
Associates a currency with this rounding precision.
static Precision unlimited()
Show all available digits to full precision.
Precision withSignificantDigits(int32_t minSignificantDigits, int32_t maxSignificantDigits, UNumberRoundingPriority priority) const
Override maximum fraction digits with maximum significant digits depending on the magnitude of the nu...
static FractionPrecision integer()
Show numbers rounded if necessary to the nearest integer.
Precision withMaxDigits(int32_t maxSignificantDigits) const
Ensure that no more than this number of significant digits are retained when rounding according to fr...
static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits, int32_t maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures; in addition,...
StringProp(const StringProp &other)
C++ API: Common ICU base class UObject.
C++ API: units for percent and permille.
C++ API: PluralRules object.
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
impl::digits_t fIncrementMagnitude
void set(StringPiece value)
const DecimalFormatSymbols * getDecimalFormatSymbols() const
Get the DecimalFormatSymbols pointer.
C++ API: A unit for measuring a quantity.
UNumberFormatPadPosition
The possible number format pad positions.
A string-like object that points to a sized piece of memory.
C++ API: Interface for writing bytes, and implementation classes.
C++ API: Currency Unit Information.
Scale(int32_t magnitude, impl::DecNum *arbitraryToAdopt)
bool fRetain
Whether to retain trailing zeros based on the looser strategy.
IntegerWidth truncateAt(int32_t maxInt)
Truncate numbers exceeding a certain number of numerals before the decimal separator.
static FractionPrecision fixedFraction(int32_t minMaxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal ...
Scale(const Scale &other)
A Locale object represents a specific geographical, political, or cultural region.
const NumberingSystem * getNumberingSystem() const
Get the NumberingSystem pointer.
static IncrementPrecision increment(double roundingIncrement)
Show numbers rounded if necessary to the closest multiple of a certain rounding increment.
Scale(Scale &&src) U_NOEXCEPT
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
Represents a span of a string containing a given field.
UNumberCompactStyle
Constants for specifying short or long format.