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
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);
669 RND_FRACTION_SIGNIFICANT,
686 union PrecisionUnion {
716 Precision(
const PrecisionType& type,
const PrecisionUnion& union_)
717 : fType(type), fUnion(union_) {}
720 fUnion.errorCode = errorCode;
723 Precision() : fType(RND_BOGUS) {}
725 bool isBogus()
const {
726 return fType == RND_BOGUS;
730 if (fType == RND_ERROR) {
731 status = fUnion.errorCode;
738 Precision withCurrency(
const CurrencyUnit ¤cy,
UErrorCode &status)
const;
740 static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
742 static Precision constructSignificant(int32_t minSig, int32_t maxSig);
745 constructFractionSignificant(
const FractionPrecision &base, int32_t minSig, int32_t maxSig);
747 static IncrementPrecision constructIncrement(
double increment, int32_t minFrac);
752 friend struct impl::MacroProps;
753 friend struct impl::MicroProps;
756 friend class impl::NumberFormatterImpl;
759 friend class impl::NumberPropertyMapper;
762 friend class impl::RoundingImpl;
765 friend class FractionPrecision;
766 friend class CurrencyPrecision;
767 friend class IncrementPrecision;
770 friend class impl::GeneratorHelpers;
773 friend class units::UnitsRouter;
826 using Precision::Precision;
864 using Precision::Precision;
900 using Precision::Precision;
948 bool fFormatFailIfMoreThanMaxDigits;
952 bool fHasError =
false;
954 IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt,
bool formatFailIfMoreThanMaxDigits);
957 fUnion.errorCode = errorCode;
962 fUnion.minMaxInt.fMinInt = -1;
966 static IntegerWidth standard() {
967 return IntegerWidth::zeroFillTo(1);
970 bool isBogus()
const {
971 return !fHasError && fUnion.minMaxInt.fMinInt == -1;
976 status = fUnion.errorCode;
982 void apply(impl::DecimalQuantity &quantity,
UErrorCode &status)
const;
984 bool operator==(
const IntegerWidth& other)
const;
987 friend struct impl::MacroProps;
988 friend struct impl::MicroProps;
991 friend class impl::NumberFormatterImpl;
994 friend class impl::MutablePatternModifier;
995 friend class impl::ImmutablePatternModifier;
998 friend class impl::NumberPropertyMapper;
1001 friend class impl::GeneratorHelpers;
1084 #ifndef U_HIDE_INTERNAL_API
1086 Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt);
1091 impl::DecNum* fArbitrary;
1094 Scale(
UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1098 bool isValid()
const {
1099 return fMagnitude != 0 || fArbitrary !=
nullptr;
1110 void applyTo(impl::DecimalQuantity& quantity)
const;
1112 void applyReciprocalTo(impl::DecimalQuantity& quantity)
const;
1115 friend struct impl::MacroProps;
1116 friend struct impl::MicroProps;
1119 friend class impl::NumberFormatterImpl;
1122 friend class impl::MultiplierFormatHandler;
1125 friend class impl::GeneratorHelpers;
1128 friend class ::icu::numparse::impl::NumberParserImpl;
1129 friend class ::icu::numparse::impl::MultiplierParseHandler;
1141 #ifndef U_HIDE_INTERNAL_API
1168 bool isSet()
const {
return fLength > 0; }
1170 #endif // U_HIDE_INTERNAL_API
1189 friend class impl::NumberFormatterImpl;
1192 friend class impl::GeneratorHelpers;
1196 friend struct impl::MacroProps;
1221 #ifndef U_HIDE_INTERNAL_API
1259 #endif // U_HIDE_INTERNAL_API
1263 if (fType == SYMPTR_DFS && fPtr.dfs ==
nullptr) {
1266 }
else if (fType == SYMPTR_NS && fPtr.ns ==
nullptr) {
1274 enum SymbolsPointerType {
1275 SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1283 void doCopyFrom(
const SymbolsWrapper &other);
1285 void doMoveFrom(SymbolsWrapper&& src);
1294 #ifndef U_HIDE_INTERNAL_API
1308 : fGrouping1(grouping1),
1309 fGrouping2(grouping2),
1310 fMinGrouping(minGrouping),
1311 fStrategy(strategy) {}
1318 #endif // U_HIDE_INTERNAL_API
1339 int16_t fMinGrouping;
1349 bool isBogus()
const {
1350 return fGrouping1 == -3;
1354 void setLocaleData(
const impl::ParsedPatternInfo &patternInfo,
const Locale& locale);
1356 bool groupAtPosition(int32_t position,
const impl::DecimalQuantity &value)
const;
1359 friend struct MacroProps;
1360 friend struct MicroProps;
1363 friend class NumberFormatterImpl;
1366 friend class ::icu::numparse::impl::NumberParserImpl;
1369 friend class impl::GeneratorHelpers;
1376 #ifndef U_HIDE_INTERNAL_API
1385 #endif // U_HIDE_INTERNAL_API
1399 Padder(int32_t width);
1402 fUnion.errorCode = errorCode;
1405 Padder() : fWidth(-2) {}
1407 bool isBogus()
const {
1408 return fWidth == -2;
1413 status = fUnion.errorCode;
1419 bool isValid()
const {
1423 int32_t padAndApply(
const impl::Modifier &mod1,
const impl::Modifier &mod2,
1424 FormattedStringBuilder &
string, int32_t leftIndex, int32_t rightIndex,
1428 friend struct MacroProps;
1429 friend struct MicroProps;
1432 friend class impl::NumberFormatterImpl;
1435 friend class impl::GeneratorHelpers;
1486 const AffixPatternProvider* affixProvider =
nullptr;
1492 int32_t threshold = kInternalDefaultThreshold;
1504 return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1505 padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1506 symbols.
copyErrorTo(status) || scale.copyErrorTo(status) || usage.copyErrorTo(status);
1512 #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
1518 #pragma warning(push)
1519 #pragma warning(disable: 4661)
1527 template<
typename Derived>
2119 #ifndef U_HIDE_DRAFT_API
2172 #endif // U_HIDE_DRAFT_API
2174 #ifndef U_HIDE_INTERNAL_API
2264 fMacros.copyErrorTo(outErrorCode);
2281 friend class impl::NumberRangeFormatterImpl;
2417 #ifndef U_HIDE_INTERNAL_API
2491 #ifndef U_HIDE_INTERNAL_API
2518 const impl::NumberFormatterImpl* fCompiled {
nullptr};
2519 char fUnsafeCallCount[8] {};
2523 const impl::DecimalFormatWarehouse* fWarehouse {
nullptr};
2525 explicit LocalizedNumberFormatter(
const NumberFormatterSettings<LocalizedNumberFormatter>& other);
2527 explicit LocalizedNumberFormatter(NumberFormatterSettings<LocalizedNumberFormatter>&& src)
U_NOEXCEPT;
2529 LocalizedNumberFormatter(
const impl::MacroProps ¯os,
const Locale &locale);
2531 LocalizedNumberFormatter(impl::MacroProps &¯os,
const Locale &locale);
2533 void resetCompiled();
2535 void lnfMoveHelper(LocalizedNumberFormatter&& src);
2537 void lnfCopyHelper(
const LocalizedNumberFormatter& src,
UErrorCode& status);
2542 bool computeCompiled(
UErrorCode& status)
const;
2545 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2546 friend class NumberFormatterSettings<LocalizedNumberFormatter>;
2549 friend class UnlocalizedNumberFormatter;
2552 #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
2554 #pragma warning(pop)
2645 template<typename StringClass>
2646 inline StringClass toDecimalNumber(
UErrorCode& status) const;
2648 #ifndef U_HIDE_DRAFT_API
2661 #endif // U_HIDE_DRAFT_API
2663 #ifndef U_HIDE_INTERNAL_API
2681 const impl::UFormattedNumberData *fData;
2694 : fData(nullptr), fErrorCode(errorCode) {}
2699 friend class LocalizedNumberFormatter;
2702 friend struct impl::UFormattedNumberImpl;
2705 #ifndef U_HIDE_DRAFT_API
2707 template<
typename StringClass>
2708 StringClass FormattedNumber::toDecimalNumber(
UErrorCode& status)
const {
2711 toDecimalNumber(sink, status);
2714 #endif // U_HIDE_DRAFT_API
2792 #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...
Basic definitions for ICU, for both C and C++ APIs.
A unit of currency, such as USD (U.S.
Usage & operator=(Usage &&src) U_NOEXCEPT
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.
Usage(const Usage &other)
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
#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.
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.
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.
void set(StringPiece value)
Usage(Usage &&src) U_NOEXCEPT
@ U_ZERO_ERROR
No error, no warning.
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
int16_t getPrimary() const
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.
Usage & operator=(const Usage &other)
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.
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.
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...
Manages NumberFormatterSettings::usage()'s char* instance on the heap.
static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits, int32_t maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures; in addition,...
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.
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)
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.