ICU 64.2 64.2

numberformatter.h
Go to the documentation of this file.
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMBERFORMATTER_H__
8 #define __NUMBERFORMATTER_H__
9
10 #include "unicode/appendable.h"
11 #include "unicode/dcfmtsym.h"
12 #include "unicode/currunit.h"
13 #include "unicode/fieldpos.h"
14 #include "unicode/formattedvalue.h"
15 #include "unicode/fpositer.h"
16 #include "unicode/measunit.h"
17 #include "unicode/nounit.h"
18 #include "unicode/parseerr.h"
19 #include "unicode/plurrule.h"
20 #include "unicode/ucurr.h"
21 #include "unicode/unum.h"
22 #include "unicode/unumberformatter.h"
23 #include "unicode/uobject.h"
24
25 #ifndef U_HIDE_DRAFT_API
26
83 U_NAMESPACE_BEGIN
84
85 // Forward declarations:
86 class IFixedDecimal;
87 class FieldPositionIteratorHandler;
88
89 namespace numparse {
90 namespace impl {
91
92 // Forward declarations:
93 class NumberParserImpl;
94 class MultiplierParseHandler;
95
96 }
97 }
98
99 namespace number { // icu::number
100
101 // Forward declarations:
102 class UnlocalizedNumberFormatter;
103 class LocalizedNumberFormatter;
104 class FormattedNumber;
105 class Notation;
106 class ScientificNotation;
107 class Precision;
108 class FractionPrecision;
109 class CurrencyPrecision;
110 class IncrementPrecision;
111 class IntegerWidth;
112
113 namespace impl {
114
115 // can't be #ifndef U_HIDE_INTERNAL_API; referenced throughout this file in public classes
121 typedef int16_t digits_t;
122
123 // can't be #ifndef U_HIDE_INTERNAL_API; needed for struct initialization
130 static constexpr int32_t kInternalDefaultThreshold = 3;
131
132 // Forward declarations:
133 class Padder;
134 struct MacroProps;
135 struct MicroProps;
136 class DecimalQuantity;
137 class UFormattedNumberData;
138 class NumberFormatterImpl;
139 struct ParsedPatternInfo;
140 class ScientificModifier;
141 class MultiplierProducer;
142 class RoundingImpl;
143 class ScientificHandler;
144 class Modifier;
145 class NumberStringBuilder;
146 class AffixPatternProvider;
147 class NumberPropertyMapper;
148 struct DecimalFormatProperties;
149 class MultiplierFormatHandler;
150 class CurrencySymbols;
151 class GeneratorHelpers;
152 class DecNum;
153 class NumberRangeFormatterImpl;
154 struct RangeMacroProps;
155 struct UFormattedNumberImpl;
156
163 void touchRangeLocales(impl::RangeMacroProps& macros);
164
165 } // namespace impl
166
172 typedef Notation CompactNotation;
173
179 typedef Notation SimpleNotation;
180
186 class U_I18N_API Notation : public UMemory {
187 public:
212 static ScientificNotation scientific();
213
236 static ScientificNotation engineering();
237
279 static CompactNotation compactShort();
280
303 static CompactNotation compactLong();
304
329 static SimpleNotation simple();
330
331 private:
332 enum NotationType {
333 NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR
334 } fType;
335
336 union NotationUnion {
337 // For NTN_SCIENTIFIC
339 struct ScientificSettings {
341 int8_t fEngineeringInterval;
343 bool fRequireMinInt;
345 impl::digits_t fMinExponentDigits;
347 UNumberSignDisplay fExponentSignDisplay;
348 } scientific;
349
350 // For NTN_COMPACT
351 UNumberCompactStyle compactStyle;
352
353 // For NTN_ERROR
354 UErrorCode errorCode;
355 } fUnion;
356
357 typedef NotationUnion::ScientificSettings ScientificSettings;
358
359 Notation(const NotationType &type, const NotationUnion &union_) : fType(type), fUnion(union_) {}
360
361 Notation(UErrorCode errorCode) : fType(NTN_ERROR) {
362 fUnion.errorCode = errorCode;
363 }
364
365 Notation() : fType(NTN_SIMPLE), fUnion() {}
366
367 UBool copyErrorTo(UErrorCode &status) const {
368 if (fType == NTN_ERROR) {
369 status = fUnion.errorCode;
370 return TRUE;
371 }
372 return FALSE;
373 }
374
375 // To allow MacroProps to initialize empty instances:
376 friend struct impl::MacroProps;
377 friend class ScientificNotation;
378
379 // To allow implementation to access internal types:
380 friend class impl::NumberFormatterImpl;
381 friend class impl::ScientificModifier;
382 friend class impl::ScientificHandler;
383
384 // To allow access to the skeleton generation code:
385 friend class impl::GeneratorHelpers;
386 };
387
396 class U_I18N_API ScientificNotation : public Notation {
397 public:
411 ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const;
412
426 ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const;
427
428 private:
429 // Inherit constructor
430 using Notation::Notation;
431
432 // Raw constructor for NumberPropertyMapper
433 ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, impl::digits_t fMinExponentDigits,
434 UNumberSignDisplay fExponentSignDisplay);
435
436 friend class Notation;
437
438 // So that NumberPropertyMapper can create instances
439 friend class impl::NumberPropertyMapper;
440 };
441
447 typedef Precision SignificantDigitsPrecision;
448
457 class U_I18N_API Precision : public UMemory {
458
459 public:
477 static Precision unlimited();
478
485 static FractionPrecision integer();
486
514 static FractionPrecision fixedFraction(int32_t minMaxFractionPlaces);
515
529 static FractionPrecision minFraction(int32_t minFractionPlaces);
530
541 static FractionPrecision maxFraction(int32_t maxFractionPlaces);
542
556 static FractionPrecision minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces);
557
571 static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits);
572
585 static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits);
586
595 static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits);
596
608 static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits,
609 int32_t maxSignificantDigits);
610
630 static IncrementPrecision increment(double roundingIncrement);
631
649 static CurrencyPrecision currency(UCurrencyUsage currencyUsage);
650
651 private:
652 enum PrecisionType {
653 RND_BOGUS,
654 RND_NONE,
655 RND_FRACTION,
656 RND_SIGNIFICANT,
657 RND_FRACTION_SIGNIFICANT,
658
659 // Used for strange increments like 3.14.
660 RND_INCREMENT,
661
662 // Used for increments with 1 as the only digit. This is different than fraction
663 // rounding because it supports having additional trailing zeros. For example, this
664 // class is used to round with the increment 0.010.
665 RND_INCREMENT_ONE,
666
667 // Used for increments with 5 as the only digit (nickel rounding).
668 RND_INCREMENT_FIVE,
669
670 RND_CURRENCY,
671 RND_ERROR
672 } fType;
673
674 union PrecisionUnion {
676 struct FractionSignificantSettings {
677 // For RND_FRACTION, RND_SIGNIFICANT, and RND_FRACTION_SIGNIFICANT
679 impl::digits_t fMinFrac;
681 impl::digits_t fMaxFrac;
683 impl::digits_t fMinSig;
685 impl::digits_t fMaxSig;
686 } fracSig;
688 struct IncrementSettings {
689 // For RND_INCREMENT, RND_INCREMENT_ONE, and RND_INCREMENT_FIVE
691 double fIncrement;
693 impl::digits_t fMinFrac;
695 impl::digits_t fMaxFrac;
696 } increment;
697 UCurrencyUsage currencyUsage; // For RND_CURRENCY
698 UErrorCode errorCode; // For RND_ERROR
699 } fUnion;
700
701 typedef PrecisionUnion::FractionSignificantSettings FractionSignificantSettings;
702 typedef PrecisionUnion::IncrementSettings IncrementSettings;
703
705 UNumberFormatRoundingMode fRoundingMode;
706
707 Precision(const PrecisionType& type, const PrecisionUnion& union_,
708 UNumberFormatRoundingMode roundingMode)
709 : fType(type), fUnion(union_), fRoundingMode(roundingMode) {}
710
711 Precision(UErrorCode errorCode) : fType(RND_ERROR) {
712 fUnion.errorCode = errorCode;
713 }
714
715 Precision() : fType(RND_BOGUS) {}
716
717 bool isBogus() const {
718 return fType == RND_BOGUS;
719 }
720
721 UBool copyErrorTo(UErrorCode &status) const {
722 if (fType == RND_ERROR) {
723 status = fUnion.errorCode;
724 return TRUE;
725 }
726 return FALSE;
727 }
728
729 // On the parent type so that this method can be called internally on Precision instances.
730 Precision withCurrency(const CurrencyUnit &currency, UErrorCode &status) const;
731
732 static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
733
734 static Precision constructSignificant(int32_t minSig, int32_t maxSig);
735
736 static Precision
737 constructFractionSignificant(const FractionPrecision &base, int32_t minSig, int32_t maxSig);
738
739 static IncrementPrecision constructIncrement(double increment, int32_t minFrac);
740
741 static CurrencyPrecision constructCurrency(UCurrencyUsage usage);
742
743 static Precision constructPassThrough();
744
745 // To allow MacroProps/MicroProps to initialize bogus instances:
746 friend struct impl::MacroProps;
747 friend struct impl::MicroProps;
748
749 // To allow NumberFormatterImpl to access isBogus() and other internal methods:
750 friend class impl::NumberFormatterImpl;
751
752 // To allow NumberPropertyMapper to create instances from DecimalFormatProperties:
753 friend class impl::NumberPropertyMapper;
754
755 // To allow access to the main implementation class:
756 friend class impl::RoundingImpl;
757
758 // To allow child classes to call private methods:
759 friend class FractionPrecision;
760 friend class CurrencyPrecision;
761 friend class IncrementPrecision;
762
763 // To allow access to the skeleton generation code:
764 friend class impl::GeneratorHelpers;
765 };
766
776 class U_I18N_API FractionPrecision : public Precision {
777 public:
794 Precision withMinDigits(int32_t minSignificantDigits) const;
795
813 Precision withMaxDigits(int32_t maxSignificantDigits) const;
814
815 private:
816 // Inherit constructor
817 using Precision::Precision;
818
819 // To allow parent class to call this class's constructor:
820 friend class Precision;
821 };
822
832 class U_I18N_API CurrencyPrecision : public Precision {
833 public:
851 Precision withCurrency(const CurrencyUnit &currency) const;
852
853 private:
854 // Inherit constructor
855 using Precision::Precision;
856
857 // To allow parent class to call this class's constructor:
858 friend class Precision;
859 };
860
870 class U_I18N_API IncrementPrecision : public Precision {
871 public:
887 Precision withMinFraction(int32_t minFrac) const;
888
889 private:
890 // Inherit constructor
891 using Precision::Precision;
892
893 // To allow parent class to call this class's constructor:
894 friend class Precision;
895 };
896
906 class U_I18N_API IntegerWidth : public UMemory {
907 public:
919 static IntegerWidth zeroFillTo(int32_t minInt);
920
932 IntegerWidth truncateAt(int32_t maxInt);
933
934 private:
935 union {
936 struct {
937 impl::digits_t fMinInt;
938 impl::digits_t fMaxInt;
939 bool fFormatFailIfMoreThanMaxDigits;
940 } minMaxInt;
941 UErrorCode errorCode;
942 } fUnion;
943 bool fHasError = false;
944
945 IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt, bool formatFailIfMoreThanMaxDigits);
946
947 IntegerWidth(UErrorCode errorCode) { // NOLINT
948 fUnion.errorCode = errorCode;
949 fHasError = true;
950 }
951
952 IntegerWidth() { // NOLINT
953 fUnion.minMaxInt.fMinInt = -1;
954 }
955
957 static IntegerWidth standard() {
958 return IntegerWidth::zeroFillTo(1);
959 }
960
961 bool isBogus() const {
962 return !fHasError && fUnion.minMaxInt.fMinInt == -1;
963 }
964
965 UBool copyErrorTo(UErrorCode &status) const {
966 if (fHasError) {
967 status = fUnion.errorCode;
968 return TRUE;
969 }
970 return FALSE;
971 }
972
973 void apply(impl::DecimalQuantity &quantity, UErrorCode &status) const;
974
975 bool operator==(const IntegerWidth& other) const;
976
977 // To allow MacroProps/MicroProps to initialize empty instances:
978 friend struct impl::MacroProps;
979 friend struct impl::MicroProps;
980
981 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
982 friend class impl::NumberFormatterImpl;
983
984 // So that NumberPropertyMapper can create instances
985 friend class impl::NumberPropertyMapper;
986
987 // To allow access to the skeleton generation code:
988 friend class impl::GeneratorHelpers;
989 };
990
999 class U_I18N_API Scale : public UMemory {
1000 public:
1007 static Scale none();
1008
1019 static Scale powerOfTen(int32_t power);
1020
1033 static Scale byDecimal(StringPiece multiplicand);
1034
1043 static Scale byDouble(double multiplicand);
1044
1051 static Scale byDoubleAndPowerOfTen(double multiplicand, int32_t power);
1052
1053 // We need a custom destructor for the DecNum, which means we need to declare
1054 // the copy/move constructor/assignment quartet.
1055
1057 Scale(const Scale& other);
1058
1060 Scale& operator=(const Scale& other);
1061
1063 Scale(Scale&& src) U_NOEXCEPT;
1064
1066 Scale& operator=(Scale&& src) U_NOEXCEPT;
1067
1069 ~Scale();
1070
1071 #ifndef U_HIDE_INTERNAL_API
1072
1073 Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt);
1074 #endif /* U_HIDE_INTERNAL_API */
1075
1076 private:
1077 int32_t fMagnitude;
1078 impl::DecNum* fArbitrary;
1079 UErrorCode fError;
1080
1081 Scale(UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1082
1083 Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR) {}
1084
1085 bool isValid() const {
1086 return fMagnitude != 0 || fArbitrary != nullptr;
1087 }
1088
1089 UBool copyErrorTo(UErrorCode &status) const {
1090 if (fError != U_ZERO_ERROR) {
1091 status = fError;
1092 return TRUE;
1093 }
1094 return FALSE;
1095 }
1096
1097 void applyTo(impl::DecimalQuantity& quantity) const;
1098
1099 void applyReciprocalTo(impl::DecimalQuantity& quantity) const;
1100
1101 // To allow MacroProps/MicroProps to initialize empty instances:
1102 friend struct impl::MacroProps;
1103 friend struct impl::MicroProps;
1104
1105 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1106 friend class impl::NumberFormatterImpl;
1107
1108 // To allow the helper class MultiplierFormatHandler access to private fields:
1109 friend class impl::MultiplierFormatHandler;
1110
1111 // To allow access to the skeleton generation code:
1112 friend class impl::GeneratorHelpers;
1113
1114 // To allow access to parsing code:
1115 friend class ::icu::numparse::impl::NumberParserImpl;
1116 friend class ::icu::numparse::impl::MultiplierParseHandler;
1117 };
1118
1119 namespace impl {
1120
1121 // Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1123 class U_I18N_API SymbolsWrapper : public UMemory {
1124 public:
1126 SymbolsWrapper() : fType(SYMPTR_NONE), fPtr{nullptr} {}
1127
1129 SymbolsWrapper(const SymbolsWrapper &other);
1130
1132 SymbolsWrapper &operator=(const SymbolsWrapper &other);
1133
1135 SymbolsWrapper(SymbolsWrapper&& src) U_NOEXCEPT;
1136
1138 SymbolsWrapper &operator=(SymbolsWrapper&& src) U_NOEXCEPT;
1139
1141 ~SymbolsWrapper();
1142
1143 #ifndef U_HIDE_INTERNAL_API
1144
1149 void setTo(const DecimalFormatSymbols &dfs);
1150
1155 void setTo(const NumberingSystem *ns);
1156
1161 bool isDecimalFormatSymbols() const;
1162
1167 bool isNumberingSystem() const;
1168
1173 const DecimalFormatSymbols *getDecimalFormatSymbols() const;
1174
1179 const NumberingSystem *getNumberingSystem() const;
1180
1181 #endif // U_HIDE_INTERNAL_API
1182
1184 UBool copyErrorTo(UErrorCode &status) const {
1185 if (fType == SYMPTR_DFS && fPtr.dfs == nullptr) {
1186 status = U_MEMORY_ALLOCATION_ERROR;
1187 return TRUE;
1188 } else if (fType == SYMPTR_NS && fPtr.ns == nullptr) {
1189 status = U_MEMORY_ALLOCATION_ERROR;
1190 return TRUE;
1191 }
1192 return FALSE;
1193 }
1194
1195 private:
1196 enum SymbolsPointerType {
1197 SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1198 } fType;
1199
1200 union {
1201 const DecimalFormatSymbols *dfs;
1202 const NumberingSystem *ns;
1203 } fPtr;
1204
1205 void doCopyFrom(const SymbolsWrapper &other);
1206
1207 void doMoveFrom(SymbolsWrapper&& src);
1208
1209 void doCleanup();
1210 };
1211
1212 // Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1214 class U_I18N_API Grouper : public UMemory {
1215 public:
1216 #ifndef U_HIDE_INTERNAL_API
1217
1218 static Grouper forStrategy(UNumberGroupingStrategy grouping);
1219
1224 static Grouper forProperties(const DecimalFormatProperties& properties);
1225
1226 // Future: static Grouper forProperties(DecimalFormatProperties& properties);
1227
1229 Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
1230 : fGrouping1(grouping1),
1231 fGrouping2(grouping2),
1232 fMinGrouping(minGrouping),
1233 fStrategy(strategy) {}
1234 #endif // U_HIDE_INTERNAL_API
1235
1237 int16_t getPrimary() const;
1238
1240 int16_t getSecondary() const;
1241
1242 private:
1251 int16_t fGrouping1;
1252 int16_t fGrouping2;
1253
1261 int16_t fMinGrouping;
1262
1267 UNumberGroupingStrategy fStrategy;
1268
1269 Grouper() : fGrouping1(-3) {}
1270
1271 bool isBogus() const {
1272 return fGrouping1 == -3;
1273 }
1274
1276 void setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale);
1277
1278 bool groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const;
1279
1280 // To allow MacroProps/MicroProps to initialize empty instances:
1281 friend struct MacroProps;
1282 friend struct MicroProps;
1283
1284 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1285 friend class NumberFormatterImpl;
1286
1287 // To allow NumberParserImpl to perform setLocaleData():
1288 friend class ::icu::numparse::impl::NumberParserImpl;
1289
1290 // To allow access to the skeleton generation code:
1291 friend class impl::GeneratorHelpers;
1292 };
1293
1294 // Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1296 class U_I18N_API Padder : public UMemory {
1297 public:
1298 #ifndef U_HIDE_INTERNAL_API
1299
1300 static Padder none();
1301
1303 static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position);
1304 #endif // U_HIDE_INTERNAL_API
1305
1307 static Padder forProperties(const DecimalFormatProperties& properties);
1308
1309 private:
1310 UChar32 fWidth; // -3 = error; -2 = bogus; -1 = no padding
1311 union {
1312 struct {
1313 int32_t fCp;
1314 UNumberFormatPadPosition fPosition;
1315 } padding;
1316 UErrorCode errorCode;
1317 } fUnion;
1318
1319 Padder(UChar32 cp, int32_t width, UNumberFormatPadPosition position);
1320
1321 Padder(int32_t width);
1322
1323 Padder(UErrorCode errorCode) : fWidth(-3) { // NOLINT
1324 fUnion.errorCode = errorCode;
1325 }
1326
1327 Padder() : fWidth(-2) {} // NOLINT
1328
1329 bool isBogus() const {
1330 return fWidth == -2;
1331 }
1332
1333 UBool copyErrorTo(UErrorCode &status) const {
1334 if (fWidth == -3) {
1335 status = fUnion.errorCode;
1336 return TRUE;
1337 }
1338 return FALSE;
1339 }
1340
1341 bool isValid() const {
1342 return fWidth > 0;
1343 }
1344
1345 int32_t padAndApply(const impl::Modifier &mod1, const impl::Modifier &mod2,
1346 impl::NumberStringBuilder &string, int32_t leftIndex, int32_t rightIndex,
1347 UErrorCode &status) const;
1348
1349 // To allow MacroProps/MicroProps to initialize empty instances:
1350 friend struct MacroProps;
1351 friend struct MicroProps;
1352
1353 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1354 friend class impl::NumberFormatterImpl;
1355
1356 // To allow access to the skeleton generation code:
1357 friend class impl::GeneratorHelpers;
1358 };
1359
1360 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1362 struct U_I18N_API MacroProps : public UMemory {
1364 Notation notation;
1365
1367 MeasureUnit unit; // = NoUnit::base();
1368
1370 MeasureUnit perUnit; // = NoUnit::base();
1371
1373 Precision precision; // = Precision(); (bogus)
1374
1376 UNumberFormatRoundingMode roundingMode = UNUM_ROUND_HALFEVEN;
1377
1379 Grouper grouper; // = Grouper(); (bogus)
1380
1382 Padder padder; // = Padder(); (bogus)
1383
1385 IntegerWidth integerWidth; // = IntegerWidth(); (bogus)
1386
1388 SymbolsWrapper symbols;
1389
1390 // UNUM_XYZ_COUNT denotes null (bogus) values.
1391
1393 UNumberUnitWidth unitWidth = UNUM_UNIT_WIDTH_COUNT;
1394
1396 UNumberSignDisplay sign = UNUM_SIGN_COUNT;
1397
1399 UNumberDecimalSeparatorDisplay decimal = UNUM_DECIMAL_SEPARATOR_COUNT;
1400
1402 Scale scale; // = Scale(); (benign value)
1403
1405 const AffixPatternProvider* affixProvider = nullptr; // no ownership
1406
1408 const PluralRules* rules = nullptr; // no ownership
1409
1411 const CurrencySymbols* currencySymbols = nullptr; // no ownership
1412
1414 int32_t threshold = kInternalDefaultThreshold;
1415
1417 Locale locale;
1418
1419 // NOTE: Uses default copy and move constructors.
1420
1425 bool copyErrorTo(UErrorCode &status) const {
1426 return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1427 padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1428 symbols.copyErrorTo(status) || scale.copyErrorTo(status);
1429 }
1430 };
1431
1432 } // namespace impl
1433
1439 template<typename Derived>
1440 class U_I18N_API NumberFormatterSettings {
1441 public:
1470 Derived notation(const Notation &notation) const &;
1471
1481 Derived notation(const Notation &notation) &&;
1482
1526 Derived unit(const icu::MeasureUnit &unit) const &;
1527
1537 Derived unit(const icu::MeasureUnit &unit) &&;
1538
1552 Derived adoptUnit(icu::MeasureUnit *unit) const &;
1553
1563 Derived adoptUnit(icu::MeasureUnit *unit) &&;
1564
1587 Derived perUnit(const icu::MeasureUnit &perUnit) const &;
1588
1598 Derived perUnit(const icu::MeasureUnit &perUnit) &&;
1599
1613 Derived adoptPerUnit(icu::MeasureUnit *perUnit) const &;
1614
1624 Derived adoptPerUnit(icu::MeasureUnit *perUnit) &&;
1625
1656 Derived precision(const Precision& precision) const &;
1657
1667 Derived precision(const Precision& precision) &&;
1668
1687 Derived roundingMode(UNumberFormatRoundingMode roundingMode) const &;
1688
1697 Derived roundingMode(UNumberFormatRoundingMode roundingMode) &&;
1698
1726 Derived grouping(UNumberGroupingStrategy strategy) const &;
1727
1737 Derived grouping(UNumberGroupingStrategy strategy) &&;
1738
1763 Derived integerWidth(const IntegerWidth &style) const &;
1764
1774 Derived integerWidth(const IntegerWidth &style) &&;
1775
1816 Derived symbols(const DecimalFormatSymbols &symbols) const &;
1817
1827 Derived symbols(const DecimalFormatSymbols &symbols) &&;
1828
1862 Derived adoptSymbols(NumberingSystem *symbols) const &;
1863
1873 Derived adoptSymbols(NumberingSystem *symbols) &&;
1874
1900 Derived unitWidth(UNumberUnitWidth width) const &;
1901
1911 Derived unitWidth(UNumberUnitWidth width) &&;
1912
1938 Derived sign(UNumberSignDisplay style) const &;
1939
1949 Derived sign(UNumberSignDisplay style) &&;
1950
1976 Derived decimal(UNumberDecimalSeparatorDisplay style) const &;
1977
1987 Derived decimal(UNumberDecimalSeparatorDisplay style) &&;
1988
2013 Derived scale(const Scale &scale) const &;
2014
2024 Derived scale(const Scale &scale) &&;
2025
2026 #ifndef U_HIDE_INTERNAL_API
2027
2033 Derived padding(const impl::Padder &padder) const &;
2034
2036 Derived padding(const impl::Padder &padder) &&;
2037
2044 Derived threshold(int32_t threshold) const &;
2045
2047 Derived threshold(int32_t threshold) &&;
2048
2054 Derived macros(const impl::MacroProps& macros) const &;
2055
2057 Derived macros(const impl::MacroProps& macros) &&;
2058
2060 Derived macros(impl::MacroProps&& macros) const &;
2061
2063 Derived macros(impl::MacroProps&& macros) &&;
2064
2065 #endif /* U_HIDE_INTERNAL_API */
2066
2081 UnicodeString toSkeleton(UErrorCode& status) const;
2082
2094 LocalPointer<Derived> clone() const &;
2095
2103 LocalPointer<Derived> clone() &&;
2104
2111 UBool copyErrorTo(UErrorCode &outErrorCode) const {
2112 if (U_FAILURE(outErrorCode)) {
2113 // Do not overwrite the older error code
2114 return TRUE;
2115 }
2116 fMacros.copyErrorTo(outErrorCode);
2117 return U_FAILURE(outErrorCode);
2118 }
2119
2120 // NOTE: Uses default copy and move constructors.
2121
2122 private:
2123 impl::MacroProps fMacros;
2124
2125 // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
2126 NumberFormatterSettings() = default;
2127
2128 friend class LocalizedNumberFormatter;
2129 friend class UnlocalizedNumberFormatter;
2130
2131 // Give NumberRangeFormatter access to the MacroProps
2132 friend void impl::touchRangeLocales(impl::RangeMacroProps& macros);
2133 friend class impl::NumberRangeFormatterImpl;
2134 };
2135
2144 class U_I18N_API UnlocalizedNumberFormatter
2145 : public NumberFormatterSettings<UnlocalizedNumberFormatter>, public UMemory {
2146
2147 public:
2157 LocalizedNumberFormatter locale(const icu::Locale &locale) const &;
2158
2168 LocalizedNumberFormatter locale(const icu::Locale &locale) &&;
2169
2175 UnlocalizedNumberFormatter() = default;
2176
2181 UnlocalizedNumberFormatter(const UnlocalizedNumberFormatter &other);
2182
2188 UnlocalizedNumberFormatter(UnlocalizedNumberFormatter&& src) U_NOEXCEPT;
2189
2194 UnlocalizedNumberFormatter& operator=(const UnlocalizedNumberFormatter& other);
2195
2201 UnlocalizedNumberFormatter& operator=(UnlocalizedNumberFormatter&& src) U_NOEXCEPT;
2202
2203 private:
2204 explicit UnlocalizedNumberFormatter(const NumberFormatterSettings<UnlocalizedNumberFormatter>& other);
2205
2206 explicit UnlocalizedNumberFormatter(
2207 NumberFormatterSettings<UnlocalizedNumberFormatter>&& src) U_NOEXCEPT;
2208
2209 // To give the fluent setters access to this class's constructor:
2210 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2211
2212 // To give NumberFormatter::with() access to this class's constructor:
2213 friend class NumberFormatter;
2214 };
2215
2224 class U_I18N_API LocalizedNumberFormatter
2225 : public NumberFormatterSettings<LocalizedNumberFormatter>, public UMemory {
2226 public:
2238 FormattedNumber formatInt(int64_t value, UErrorCode &status) const;
2239
2251 FormattedNumber formatDouble(double value, UErrorCode &status) const;
2252
2267 FormattedNumber formatDecimal(StringPiece value, UErrorCode& status) const;
2268
2269 #ifndef U_HIDE_INTERNAL_API
2270
2274 FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity& dq, UErrorCode& status) const;
2275
2279 void getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, UErrorCode& status) const;
2280
2285 const impl::NumberFormatterImpl* getCompiled() const;
2286
2291 int32_t getCallCount() const;
2292
2293 #endif /* U_HIDE_INTERNAL_API */
2294
2308 Format* toFormat(UErrorCode& status) const;
2309
2315 LocalizedNumberFormatter() = default;
2316
2321 LocalizedNumberFormatter(const LocalizedNumberFormatter &other);
2322
2328 LocalizedNumberFormatter(LocalizedNumberFormatter&& src) U_NOEXCEPT;
2329
2334 LocalizedNumberFormatter& operator=(const LocalizedNumberFormatter& other);
2335
2341 LocalizedNumberFormatter& operator=(LocalizedNumberFormatter&& src) U_NOEXCEPT;
2342
2343 #ifndef U_HIDE_INTERNAL_API
2344
2357 void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const;
2358
2359 #endif /* U_HIDE_INTERNAL_API */
2360
2365 ~LocalizedNumberFormatter();
2366
2367 private:
2368 // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal
2369 // header, and LocalPointer needs the full class definition in order to delete the instance.
2370 const impl::NumberFormatterImpl* fCompiled {nullptr};
2371 char fUnsafeCallCount[8] {}; // internally cast to u_atomic_int32_t
2372
2373 explicit LocalizedNumberFormatter(const NumberFormatterSettings<LocalizedNumberFormatter>& other);
2374
2375 explicit LocalizedNumberFormatter(NumberFormatterSettings<LocalizedNumberFormatter>&& src) U_NOEXCEPT;
2376
2377 LocalizedNumberFormatter(const impl::MacroProps &macros, const Locale &locale);
2378
2379 LocalizedNumberFormatter(impl::MacroProps &&macros, const Locale &locale);
2380
2381 void clear();
2382
2383 void lnfMoveHelper(LocalizedNumberFormatter&& src);
2384
2388 bool computeCompiled(UErrorCode& status) const;
2389
2390 // To give the fluent setters access to this class's constructor:
2391 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2392 friend class NumberFormatterSettings<LocalizedNumberFormatter>;
2393
2394 // To give UnlocalizedNumberFormatter::locale() access to this class's constructor:
2395 friend class UnlocalizedNumberFormatter;
2396 };
2397
2406 class U_I18N_API FormattedNumber : public UMemory, public FormattedValue {
2407 public:
2408
2413 FormattedNumber()
2414 : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
2415
2420 FormattedNumber(FormattedNumber&& src) U_NOEXCEPT;
2421
2426 virtual ~FormattedNumber() U_OVERRIDE;
2427
2429 FormattedNumber(const FormattedNumber&) = delete;
2430
2432 FormattedNumber& operator=(const FormattedNumber&) = delete;
2433
2438 FormattedNumber& operator=(FormattedNumber&& src) U_NOEXCEPT;
2439
2440 // Copybrief: this method is older than the parent method
2448 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
2449
2450 // Copydoc: this method is new in ICU 64
2452 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
2453
2454 // Copybrief: this method is older than the parent method
2462 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
2463
2464 // Copydoc: this method is new in ICU 64
2466 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
2467
2501 UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
2502
2518 void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
2519
2520 #ifndef U_HIDE_INTERNAL_API
2521
2526 void getDecimalQuantity(impl::DecimalQuantity& output, UErrorCode& status) const;
2527
2532 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
2533
2534 #endif /* U_HIDE_INTERNAL_API */
2535
2536 private:
2537 // Can't use LocalPointer because UFormattedNumberData is forward-declared
2538 const impl::UFormattedNumberData *fData;
2539
2540 // Error code for the terminal methods
2541 UErrorCode fErrorCode;
2542
2547 explicit FormattedNumber(impl::UFormattedNumberData *results)
2548 : fData(results), fErrorCode(U_ZERO_ERROR) {}
2549
2550 explicit FormattedNumber(UErrorCode errorCode)
2551 : fData(nullptr), fErrorCode(errorCode) {}
2552
2553 // To give LocalizedNumberFormatter format methods access to this class's constructor:
2554 friend class LocalizedNumberFormatter;
2555
2556 // To give C API access to internals
2557 friend struct impl::UFormattedNumberImpl;
2558 };
2559
2565 class U_I18N_API NumberFormatter final {
2566 public:
2574 static UnlocalizedNumberFormatter with();
2575
2585 static LocalizedNumberFormatter withLocale(const Locale &locale);
2586
2601 static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton, UErrorCode& status);
2602
2620 static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton,
2621 UParseError& perror, UErrorCode& status);
2622
2626 NumberFormatter() = delete;
2627 };
2628
2629 } // namespace number
2630 U_NAMESPACE_END
2631
2632 #endif // U_HIDE_DRAFT_API
2633
2634 #endif // __NUMBERFORMATTER_H__
2635
2636 #endif /* #if !UCONFIG_NO_FORMATTING */
formattedvalue.h
C++ API: Abstract operations for localized strings.
icu::number::impl::MacroProps::perUnit
MeasureUnit perUnit
Definition: numberformatter.h:1370
UNUM_SIGN_COUNT
One more than the highest UNumberSignDisplay value.
Definition: unumberformatter.h:367
icu::Format
Base class for all formats.
Definition: format.h:96
icu::number::Precision::PrecisionUnion::IncrementSettings::fMaxFrac
impl::digits_t fMaxFrac
Definition: numberformatter.h:695
icu::DecimalFormatSymbols
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:84
U_OVERRIDE
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
UNumberFormatRoundingMode
UNumberFormatRoundingMode
The possible number format rounding modes.
Definition: unum.h:279
icu::MeasureUnit
A unit such as length, mass, volume, currency, etc.
Definition: measunit.h:38
currunit.h
C++ API: Currency Unit Information.
unumberformatter.h
C-compatible API for localized number formatting; not recommended for C++.
icu::number::impl::MacroProps::padder
Padder padder
Definition: numberformatter.h:1382
icu::number::NumberFormatter
See the main description in numberformatter.h for documentation and examples.
Definition: numberformatter.h:2565
U_FAILURE
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:695
icu::number::IntegerWidth
A class that defines the strategy for padding and truncating integers before the decimal separator.
Definition: numberformatter.h:906
fpositer.h
C++ API: FieldPosition Iterator.
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::NumberingSystem
Defines numbering systems.
Definition: numsys.h:58
appendable.h
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
nounit.h
C++ API: units for percent and permille.
U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:435
plurrule.h
C++ API: PluralRules object.
icu::number::Precision::PrecisionUnion::FractionSignificantSettings
Definition: numberformatter.h:676
icu::number::IncrementPrecision
A class that defines a rounding precision parameterized by a rounding increment to be used when forma...
Definition: numberformatter.h:870
icu::number::Precision::PrecisionUnion::IncrementSettings::fIncrement
double fIncrement
Definition: numberformatter.h:691
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
UNumberDecimalSeparatorDisplay
UNumberDecimalSeparatorDisplay
An enum declaring how to render the decimal separator.
Definition: unumberformatter.h:383
icu::PluralRules
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:198
U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition: utypes.h:443
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMinSig
impl::digits_t fMinSig
Definition: numberformatter.h:683
icu::number::Notation::NotationUnion::ScientificSettings::fExponentSignDisplay
UNumberSignDisplay fExponentSignDisplay
Definition: numberformatter.h:347
icu::number::Notation::NotationUnion::ScientificSettings
Definition: numberformatter.h:339
icu::number::impl::MacroProps
Definition: numberformatter.h:1362
icu::number::NumberFormatterSettings
An abstract base class for specifying settings related to number formatting.
Definition: numberformatter.h:1440
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301
UNUM_ROUND_HALFEVEN
Half-even rounding.
Definition: unum.h:288
fieldpos.h
C++ API: FieldPosition identifies the fields in a formatted output.
icu::number::impl::MacroProps::grouper
Grouper grouper
Definition: numberformatter.h:1379
icu::number::Notation
A class that defines the notation style to be used when formatting numbers in NumberFormatter.
Definition: numberformatter.h:186
icu::number::impl::RangeMacroProps
Definition: numberrangeformatter.h:207
icu::number::impl::touchRangeLocales
void touchRangeLocales(impl::RangeMacroProps &macros)
Used for NumberRangeFormatter and implemented in numrange_fluent.cpp.
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
icu::number::Notation::NotationUnion::ScientificSettings::fMinExponentDigits
impl::digits_t fMinExponentDigits
Definition: numberformatter.h:345
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
icu::number::impl::MacroProps::integerWidth
IntegerWidth integerWidth
Definition: numberformatter.h:1385
measunit.h
C++ API: A unit for measuring a quantity.
ucurr.h
C API: Encapsulates information about a currency.
icu::number::Precision::PrecisionUnion::IncrementSettings
Definition: numberformatter.h:688
UNumberSignDisplay
UNumberSignDisplay
An enum declaring how to denote positive and negative numbers.
Definition: unumberformatter.h:295
icu::number::FractionPrecision
A class that defines a rounding precision based on a number of fraction places and optionally signifi...
Definition: numberformatter.h:776
UNumberGroupingStrategy
UNumberGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i....
Definition: unumberformatter.h:197
UCurrencyUsage
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition: ucurr.h:41
UChar32
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:389
icu::number::NumberFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberformatter.h:2111
icu::number::impl::MacroProps::locale
Locale locale
Definition: numberformatter.h:1417
icu::number::impl::SymbolsWrapper::SymbolsWrapper
SymbolsWrapper()
Definition: numberformatter.h:1126
icu::number::impl::MacroProps::notation
Notation notation
Definition: numberformatter.h:1364
icu::number::impl::MacroProps::symbols
SymbolsWrapper symbols
Definition: numberformatter.h:1388
icu::number::CurrencyPrecision
A class that defines a rounding precision parameterized by a currency to be used when formatting numb...
Definition: numberformatter.h:832
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:229
icu::CurrencyUnit
A unit of currency, such as USD (U.S.
Definition: currunit.h:37
UNumberFormatPadPosition
UNumberFormatPadPosition
The possible number format pad positions.
Definition: unum.h:308
icu::number::impl::MacroProps::unit
MeasureUnit unit
Definition: numberformatter.h:1367
uobject.h
C++ API: Common ICU base class UObject.
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:40
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
icu::number::UnlocalizedNumberFormatter
A NumberFormatter that does not yet have a locale.
Definition: numberformatter.h:2144
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UNumberCompactStyle
UNumberCompactStyle
Constants for specifying short or long format.
Definition: unum.h:319
parseerr.h
C API: Parse Error Information.
icu::number::LocalizedNumberFormatter
A NumberFormatter that has a locale associated with it; this means .format() methods are available.
Definition: numberformatter.h:2224
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMinFrac
impl::digits_t fMinFrac
Definition: numberformatter.h:679
icu::number::Notation::NotationUnion::ScientificSettings::fRequireMinInt
bool fRequireMinInt
Definition: numberformatter.h:343
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMaxFrac
impl::digits_t fMaxFrac
Definition: numberformatter.h:681
icu::number::Scale
A class that defines a quantity by which a number should be multiplied when formatting.
Definition: numberformatter.h:999
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:401
icu::number::impl::MacroProps::scale
Scale scale
Definition: numberformatter.h:1402
icu::number::impl::SymbolsWrapper
Definition: numberformatter.h:1123
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
icu::number::impl::MacroProps::precision
Precision precision
Definition: numberformatter.h:1373
dcfmtsym.h
C++ API: Symbols for formatting numbers.
icu::number::ScientificNotation
A class that defines the scientific notation style to be used when formatting numbers in NumberFormat...
Definition: numberformatter.h:396
UNumberUnitWidth
UNumberUnitWidth
An enum declaring how to render units, including currencies.
Definition: unumberformatter.h:100
UParseError
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:58
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::number::impl::Padder
Definition: numberformatter.h:1296
FALSE
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:233
icu::number::impl::digits_t
int16_t digits_t
Datatype for minimum/maximum fraction digits.
Definition: numberformatter.h:121
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
icu::number::FormattedNumber
The result of a number formatting operation.
Definition: numberformatter.h:2406
icu::number::impl::Grouper::Grouper
Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
Definition: numberformatter.h:1229
icu::number::Precision::PrecisionUnion::FractionSignificantSettings::fMaxSig
impl::digits_t fMaxSig
Definition: numberformatter.h:685
icu::number::impl::Grouper
Definition: numberformatter.h:1214
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:112
icu::number::Precision
A class that defines the rounding precision to be used when formatting numbers in NumberFormatter.
Definition: numberformatter.h:457
UNUM_DECIMAL_SEPARATOR_COUNT
One more than the highest UNumberDecimalSeparatorDisplay value.
Definition: unumberformatter.h:404
U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:464
UNUM_UNIT_WIDTH_COUNT
One more than the highest UNumberUnitWidth value.
Definition: unumberformatter.h:166
icu::number::FormattedNumber::FormattedNumber
FormattedNumber()
Default constructor; makes an empty FormattedNumber.
Definition: numberformatter.h:2413
icu::number::Notation::NotationUnion::ScientificSettings::fEngineeringInterval
int8_t fEngineeringInterval
Definition: numberformatter.h:341
icu::number::impl::MacroProps::copyErrorTo
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
Definition: numberformatter.h:1425
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:51
unum.h
C API: Compatibility APIs for number formatting.
icu::number::Precision::PrecisionUnion::IncrementSettings::fMinFrac
impl::digits_t fMinFrac
Definition: numberformatter.h:693
icu::number::impl::SymbolsWrapper::copyErrorTo
UBool copyErrorTo(UErrorCode &status) const
Definition: numberformatter.h:1184
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192