ICU 62.1 62.1
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/fpositer.h"
15#include "unicode/measunit.h"
16#include "unicode/nounit.h"
17#include "unicode/plurrule.h"
18#include "unicode/ucurr.h"
19#include "unicode/unum.h"
21#include "unicode/uobject.h"
22
23#ifndef U_HIDE_DRAFT_API
24
80
81// Forward declarations:
82class IFixedDecimal;
83class FieldPositionIteratorHandler;
84
85namespace numparse {
86namespace impl {
87
88// Forward declarations:
89class NumberParserImpl;
90class MultiplierParseHandler;
91
92}
93}
94
95namespace number { // icu::number
96
97// Forward declarations:
98class UnlocalizedNumberFormatter;
99class LocalizedNumberFormatter;
100class FormattedNumber;
101class Notation;
102class ScientificNotation;
103class Precision;
104class FractionPrecision;
105class CurrencyPrecision;
106class IncrementPrecision;
107class IntegerWidth;
108
109namespace impl {
110
116typedef int16_t digits_t;
117
124static constexpr int32_t DEFAULT_THRESHOLD = 3;
125
126// Forward declarations:
127class Padder;
128struct MacroProps;
129struct MicroProps;
130class DecimalQuantity;
132class NumberFormatterImpl;
133struct ParsedPatternInfo;
134class ScientificModifier;
136class RoundingImpl;
137class ScientificHandler;
138class Modifier;
141class NumberPropertyMapper;
143class MultiplierFormatHandler;
144class CurrencySymbols;
145class GeneratorHelpers;
146class DecNum;
147
148} // namespace impl
149
150// Reserve extra names in case they are added as classes in the future:
153
160 public:
186
210
253
277
303
304 private:
305 enum NotationType {
306 NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR
307 } fType;
308
309 union NotationUnion {
310 // For NTN_SCIENTIFIC
312 int8_t fEngineeringInterval;
313 bool fRequireMinInt;
314 impl::digits_t fMinExponentDigits;
315 UNumberSignDisplay fExponentSignDisplay;
316 } scientific;
317
318 // For NTN_COMPACT
319 UNumberCompactStyle compactStyle;
320
321 // For NTN_ERROR
322 UErrorCode errorCode;
323 } fUnion;
324
326
327 Notation(const NotationType &type, const NotationUnion &union_) : fType(type), fUnion(union_) {}
328
329 Notation(UErrorCode errorCode) : fType(NTN_ERROR) {
330 fUnion.errorCode = errorCode;
331 }
332
333 Notation() : fType(NTN_SIMPLE), fUnion() {}
334
335 UBool copyErrorTo(UErrorCode &status) const {
336 if (fType == NTN_ERROR) {
337 status = fUnion.errorCode;
338 return TRUE;
339 }
340 return FALSE;
341 }
342
343 // To allow MacroProps to initialize empty instances:
344 friend struct impl::MacroProps;
345 friend class ScientificNotation;
346
347 // To allow implementation to access internal types:
348 friend class impl::NumberFormatterImpl;
349 friend class impl::ScientificModifier;
350 friend class impl::ScientificHandler;
351
352 // To allow access to the skeleton generation code:
353 friend class impl::GeneratorHelpers;
354};
355
365 public:
380
395
396 private:
397 // Inherit constructor
398 using Notation::Notation;
399
400 // Raw constructor for NumberPropertyMapper
401 ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, impl::digits_t fMinExponentDigits,
402 UNumberSignDisplay fExponentSignDisplay);
403
404 friend class Notation;
405
406 // So that NumberPropertyMapper can create instances
407 friend class impl::NumberPropertyMapper;
408};
409
410// Reserve extra names in case they are added as classes in the future:
412
413// Typedefs for ICU 60/61 compatibility.
414// These will be removed in ICU 64.
415// See http://bugs.icu-project.org/trac/ticket/13746
416typedef Precision Rounder;
420
430
431 public:
449
457
486
501
513
528
542 static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits);
543
556 static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits);
557
566 static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits);
567
579 static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits,
580 int32_t maxSignificantDigits);
581
582#ifndef U_HIDE_DEPRECATED_API
583 // Compatiblity methods that will be removed in ICU 64.
584 // See http://bugs.icu-project.org/trac/ticket/13746
585
587 static inline SignificantDigitsPrecision fixedDigits(int32_t a) {
588 return fixedSignificantDigits(a);
589 }
590
592 static inline SignificantDigitsPrecision minDigits(int32_t a) {
593 return minSignificantDigits(a);
594 }
595
597 static inline SignificantDigitsPrecision maxDigits(int32_t a) {
598 return maxSignificantDigits(a);
599 }
600
602 static inline SignificantDigitsPrecision minMaxDigits(int32_t a, int32_t b) {
603 return minMaxSignificantDigits(a, b);
604 }
605#endif /* U_HIDE_DEPRECATED_API */
606
627
646
647#ifndef U_HIDE_DEPRECATED_API
660#endif /* U_HIDE_DEPRECATED_API */
661
662 private:
663 enum PrecisionType {
664 RND_BOGUS,
665 RND_NONE,
666 RND_FRACTION,
667 RND_SIGNIFICANT,
668 RND_FRACTION_SIGNIFICANT,
669 RND_INCREMENT,
670 RND_CURRENCY,
671 RND_ERROR
672 } fType;
673
674 union PrecisionUnion {
676 // For RND_FRACTION, RND_SIGNIFICANT, and RND_FRACTION_SIGNIFICANT
677 impl::digits_t fMinFrac;
678 impl::digits_t fMaxFrac;
679 impl::digits_t fMinSig;
680 impl::digits_t fMaxSig;
681 } fracSig;
683 double fIncrement;
684 impl::digits_t fMinFrac;
685 impl::digits_t fMaxFrac;
686 } increment; // For RND_INCREMENT
687 UCurrencyUsage currencyUsage; // For RND_CURRENCY
688 UErrorCode errorCode; // For RND_ERROR
689 } fUnion;
690
693
695 UNumberFormatRoundingMode fRoundingMode;
696
697 Precision(const PrecisionType& type, const PrecisionUnion& union_,
698 UNumberFormatRoundingMode roundingMode)
699 : fType(type), fUnion(union_), fRoundingMode(roundingMode) {}
700
701 Precision(UErrorCode errorCode) : fType(RND_ERROR) {
702 fUnion.errorCode = errorCode;
703 }
704
705 Precision() : fType(RND_BOGUS) {}
706
707 bool isBogus() const {
708 return fType == RND_BOGUS;
709 }
710
711 UBool copyErrorTo(UErrorCode &status) const {
712 if (fType == RND_ERROR) {
713 status = fUnion.errorCode;
714 return TRUE;
715 }
716 return FALSE;
717 }
718
719 // On the parent type so that this method can be called internally on Precision instances.
720 Precision withCurrency(const CurrencyUnit &currency, UErrorCode &status) const;
721
722 static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
723
724 static Precision constructSignificant(int32_t minSig, int32_t maxSig);
725
726 static Precision
727 constructFractionSignificant(const FractionPrecision &base, int32_t minSig, int32_t maxSig);
728
729 static IncrementPrecision constructIncrement(double increment, int32_t minFrac);
730
731 static CurrencyPrecision constructCurrency(UCurrencyUsage usage);
732
733 static Precision constructPassThrough();
734
735 // To allow MacroProps/MicroProps to initialize bogus instances:
736 friend struct impl::MacroProps;
737 friend struct impl::MicroProps;
738
739 // To allow NumberFormatterImpl to access isBogus() and other internal methods:
740 friend class impl::NumberFormatterImpl;
741
742 // To allow NumberPropertyMapper to create instances from DecimalFormatProperties:
743 friend class impl::NumberPropertyMapper;
744
745 // To allow access to the main implementation class:
746 friend class impl::RoundingImpl;
747
748 // To allow child classes to call private methods:
749 friend class FractionPrecision;
750 friend class CurrencyPrecision;
751 friend class IncrementPrecision;
752
753 // To allow access to the skeleton generation code:
754 friend class impl::GeneratorHelpers;
755};
756
767 public:
784 Precision withMinDigits(int32_t minSignificantDigits) const;
785
803 Precision withMaxDigits(int32_t maxSignificantDigits) const;
804
805 private:
806 // Inherit constructor
807 using Precision::Precision;
808
809 // To allow parent class to call this class's constructor:
810 friend class Precision;
811};
812
823 public:
841 Precision withCurrency(const CurrencyUnit &currency) const;
842
843 private:
844 // Inherit constructor
845 using Precision::Precision;
846
847 // To allow parent class to call this class's constructor:
848 friend class Precision;
849};
850
861 public:
878
879 private:
880 // Inherit constructor
881 using Precision::Precision;
882
883 // To allow parent class to call this class's constructor:
884 friend class Precision;
885};
886
897 public:
910
923
924 private:
925 union {
926 struct {
927 impl::digits_t fMinInt;
928 impl::digits_t fMaxInt;
929 bool fFormatFailIfMoreThanMaxDigits;
930 } minMaxInt;
931 UErrorCode errorCode;
932 } fUnion;
933 bool fHasError = false;
934
935 IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt, bool formatFailIfMoreThanMaxDigits);
936
937 IntegerWidth(UErrorCode errorCode) { // NOLINT
938 fUnion.errorCode = errorCode;
939 fHasError = true;
940 }
941
942 IntegerWidth() { // NOLINT
943 fUnion.minMaxInt.fMinInt = -1;
944 }
945
947 static IntegerWidth standard() {
948 return IntegerWidth::zeroFillTo(1);
949 }
950
951 bool isBogus() const {
952 return !fHasError && fUnion.minMaxInt.fMinInt == -1;
953 }
954
955 UBool copyErrorTo(UErrorCode &status) const {
956 if (fHasError) {
957 status = fUnion.errorCode;
958 return TRUE;
959 }
960 return FALSE;
961 }
962
963 void apply(impl::DecimalQuantity &quantity, UErrorCode &status) const;
964
965 bool operator==(const IntegerWidth& other) const;
966
967 // To allow MacroProps/MicroProps to initialize empty instances:
968 friend struct impl::MacroProps;
969 friend struct impl::MicroProps;
970
971 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
972 friend class impl::NumberFormatterImpl;
973
974 // So that NumberPropertyMapper can create instances
975 friend class impl::NumberPropertyMapper;
976
977 // To allow access to the skeleton generation code:
978 friend class impl::GeneratorHelpers;
979};
980
989class U_I18N_API Scale : public UMemory {
990 public:
997 static Scale none();
998
1009 static Scale powerOfTen(int32_t power);
1010
1024
1034
1042
1043 // We need a custom destructor for the DecNum, which means we need to declare
1044 // the copy/move constructor/assignment quartet.
1045
1048
1051
1054
1057
1060
1061#ifndef U_HIDE_INTERNAL_API
1063 Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt);
1064#endif /* U_HIDE_INTERNAL_API */
1065
1066 private:
1067 int32_t fMagnitude;
1068 impl::DecNum* fArbitrary;
1069 UErrorCode fError;
1070
1071 Scale(UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1072
1073 Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR) {}
1074
1075 bool isValid() const {
1076 return fMagnitude != 0 || fArbitrary != nullptr;
1077 }
1078
1079 UBool copyErrorTo(UErrorCode &status) const {
1080 if (fError != U_ZERO_ERROR) {
1081 status = fError;
1082 return TRUE;
1083 }
1084 return FALSE;
1085 }
1086
1087 void applyTo(impl::DecimalQuantity& quantity) const;
1088
1089 void applyReciprocalTo(impl::DecimalQuantity& quantity) const;
1090
1091 // To allow MacroProps/MicroProps to initialize empty instances:
1092 friend struct impl::MacroProps;
1093 friend struct impl::MicroProps;
1094
1095 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1096 friend class impl::NumberFormatterImpl;
1097
1098 // To allow the helper class MultiplierFormatHandler access to private fields:
1099 friend class impl::MultiplierFormatHandler;
1100
1101 // To allow access to the skeleton generation code:
1102 friend class impl::GeneratorHelpers;
1103
1104 // To allow access to parsing code:
1105 friend class ::icu::numparse::impl::NumberParserImpl;
1106 friend class ::icu::numparse::impl::MultiplierParseHandler;
1107};
1108
1109namespace impl {
1110
1111// Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1114 public:
1116 SymbolsWrapper() : fType(SYMPTR_NONE), fPtr{nullptr} {}
1117
1120
1123
1126
1129
1132
1133#ifndef U_HIDE_INTERNAL_API
1134
1139 void setTo(const DecimalFormatSymbols &dfs);
1140
1145 void setTo(const NumberingSystem *ns);
1146
1152
1157 bool isNumberingSystem() const;
1158
1164
1170
1171#endif // U_HIDE_INTERNAL_API
1172
1175 if (fType == SYMPTR_DFS && fPtr.dfs == nullptr) {
1177 return TRUE;
1178 } else if (fType == SYMPTR_NS && fPtr.ns == nullptr) {
1180 return TRUE;
1181 }
1182 return FALSE;
1183 }
1184
1185 private:
1186 enum SymbolsPointerType {
1187 SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1188 } fType;
1189
1190 union {
1191 const DecimalFormatSymbols *dfs;
1192 const NumberingSystem *ns;
1193 } fPtr;
1194
1195 void doCopyFrom(const SymbolsWrapper &other);
1196
1197 void doMoveFrom(SymbolsWrapper&& src);
1198
1199 void doCleanup();
1200};
1201
1202// Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1205 public:
1206#ifndef U_HIDE_INTERNAL_API
1209
1215
1216 // Future: static Grouper forProperties(DecimalFormatProperties& properties);
1217
1220 : fGrouping1(grouping1),
1221 fGrouping2(grouping2),
1222 fMinGrouping(minGrouping),
1223 fStrategy(strategy) {}
1224#endif // U_HIDE_INTERNAL_API
1225
1227 int16_t getPrimary() const;
1228
1230 int16_t getSecondary() const;
1231
1232 private:
1241 int16_t fGrouping1;
1242 int16_t fGrouping2;
1243
1251 int16_t fMinGrouping;
1252
1257 UGroupingStrategy fStrategy;
1258
1259 Grouper() : fGrouping1(-3) {};
1260
1261 bool isBogus() const {
1262 return fGrouping1 == -3;
1263 }
1264
1266 void setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale);
1267
1268 bool groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const;
1269
1270 // To allow MacroProps/MicroProps to initialize empty instances:
1271 friend struct MacroProps;
1272 friend struct MicroProps;
1273
1274 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1275 friend class NumberFormatterImpl;
1276
1277 // To allow NumberParserImpl to perform setLocaleData():
1278 friend class ::icu::numparse::impl::NumberParserImpl;
1279
1280 // To allow access to the skeleton generation code:
1281 friend class impl::GeneratorHelpers;
1282};
1283
1284// Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1286class U_I18N_API Padder : public UMemory {
1287 public:
1288#ifndef U_HIDE_INTERNAL_API
1290 static Padder none();
1291
1294#endif // U_HIDE_INTERNAL_API
1295
1298
1299 private:
1300 UChar32 fWidth; // -3 = error; -2 = bogus; -1 = no padding
1301 union {
1302 struct {
1303 int32_t fCp;
1304 UNumberFormatPadPosition fPosition;
1305 } padding;
1306 UErrorCode errorCode;
1307 } fUnion;
1308
1309 Padder(UChar32 cp, int32_t width, UNumberFormatPadPosition position);
1310
1311 Padder(int32_t width);
1312
1313 Padder(UErrorCode errorCode) : fWidth(-3) { // NOLINT
1314 fUnion.errorCode = errorCode;
1315 }
1316
1317 Padder() : fWidth(-2) {} // NOLINT
1318
1319 bool isBogus() const {
1320 return fWidth == -2;
1321 }
1322
1323 UBool copyErrorTo(UErrorCode &status) const {
1324 if (fWidth == -3) {
1325 status = fUnion.errorCode;
1326 return TRUE;
1327 }
1328 return FALSE;
1329 }
1330
1331 bool isValid() const {
1332 return fWidth > 0;
1333 }
1334
1335 int32_t padAndApply(const impl::Modifier &mod1, const impl::Modifier &mod2,
1336 impl::NumberStringBuilder &string, int32_t leftIndex, int32_t rightIndex,
1337 UErrorCode &status) const;
1338
1339 // To allow MacroProps/MicroProps to initialize empty instances:
1340 friend struct MacroProps;
1341 friend struct MicroProps;
1342
1343 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1344 friend class impl::NumberFormatterImpl;
1345
1346 // To allow access to the skeleton generation code:
1347 friend class impl::GeneratorHelpers;
1348};
1349
1350// Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1355
1357 MeasureUnit unit; // = NoUnit::base();
1358
1360 MeasureUnit perUnit; // = NoUnit::base();
1361
1363 Precision precision; // = Precision(); (bogus)
1364
1367
1369 Grouper grouper; // = Grouper(); (bogus)
1370
1372 Padder padder; // = Padder(); (bogus)
1373
1375 IntegerWidth integerWidth; // = IntegerWidth(); (bogus)
1376
1379
1380 // UNUM_XYZ_COUNT denotes null (bogus) values.
1381
1384
1387
1390
1392 Scale scale; // = Scale(); (benign value)
1393
1395 const AffixPatternProvider* affixProvider = nullptr; // no ownership
1396
1398 const PluralRules* rules = nullptr; // no ownership
1399
1401 const CurrencySymbols* currencySymbols = nullptr; // no ownership
1402
1404 int32_t threshold = DEFAULT_THRESHOLD;
1405
1408
1409 // NOTE: Uses default copy and move constructors.
1410
1416 return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1417 padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1418 symbols.copyErrorTo(status) || scale.copyErrorTo(status);
1419 }
1420};
1421
1422} // namespace impl
1423
1428template<typename Derived>
1430 public:
1459 Derived notation(const Notation &notation) const &;
1460
1470 Derived notation(const Notation &notation) &&;
1471
1516 Derived unit(const icu::MeasureUnit &unit) const &;
1517
1528
1545
1556
1574 Derived perUnit(const icu::MeasureUnit &perUnit) const &;
1575
1586
1605
1616
1647 Derived precision(const Precision& precision) const &;
1648
1658 Derived precision(const Precision& precision) &&;
1659
1660#ifndef U_HIDE_DEPRECATED_API
1661 // Compatibility method that will be removed in ICU 64.
1662 // Use precision() instead.
1663 // See http://bugs.icu-project.org/trac/ticket/13746
1666 return precision(rounder);
1667 }
1668#endif /* U_HIDE_DEPRECATED_API */
1669
1689
1699
1728
1740
1766
1777
1818 Derived symbols(const DecimalFormatSymbols &symbols) const &;
1819
1830
1865
1876
1903
1914
1941
1952
1979
1990
2015 Derived scale(const Scale &scale) const &;
2016
2026 Derived scale(const Scale &scale) &&;
2027
2028#ifndef U_HIDE_INTERNAL_API
2029
2035 Derived padding(const impl::Padder &padder) const &;
2036
2038 Derived padding(const impl::Padder &padder) &&;
2039
2046 Derived threshold(int32_t threshold) const &;
2047
2049 Derived threshold(int32_t threshold) &&;
2050
2056 Derived macros(const impl::MacroProps& macros) const &;
2057
2060
2063
2066
2067#endif /* U_HIDE_INTERNAL_API */
2068
2084
2092 if (U_FAILURE(outErrorCode)) {
2093 // Do not overwrite the older error code
2094 return TRUE;
2095 }
2096 fMacros.copyErrorTo(outErrorCode);
2097 return U_FAILURE(outErrorCode);
2098 };
2099
2100 // NOTE: Uses default copy and move constructors.
2101
2102 protected:
2103 impl::MacroProps fMacros;
2104
2105 private:
2106 // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
2107 NumberFormatterSettings() = default;
2108
2109 friend class LocalizedNumberFormatter;
2110 friend class UnlocalizedNumberFormatter;
2111};
2112
2120 : public NumberFormatterSettings<UnlocalizedNumberFormatter>, public UMemory {
2121
2122 public:
2140
2151
2158
2159 // Make default copy constructor call the NumberFormatterSettings copy constructor.
2165
2172
2178
2185
2186 private:
2188
2191
2192 // To give the fluent setters access to this class's constructor:
2194
2195 // To give NumberFormatter::with() access to this class's constructor:
2196 friend class NumberFormatter;
2197};
2198
2206 : public NumberFormatterSettings<LocalizedNumberFormatter>, public UMemory {
2207 public:
2220
2233
2249
2250#ifndef U_HIDE_INTERNAL_API
2251
2255 FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity& dq, UErrorCode& status) const;
2256
2261
2266 const impl::NumberFormatterImpl* getCompiled() const;
2267
2272 int32_t getCallCount() const;
2273
2274#endif
2275
2290
2297
2298 // Make default copy constructor call the NumberFormatterSettings copy constructor.
2304
2311
2317
2324
2325#ifndef U_HIDE_INTERNAL_API
2326
2338 void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const;
2339
2340#endif
2341
2347
2348 private:
2349 // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal
2350 // header, and LocalPointer needs the full class definition in order to delete the instance.
2351 const impl::NumberFormatterImpl* fCompiled {nullptr};
2352 char fUnsafeCallCount[8] {}; // internally cast to u_atomic_int32_t
2353
2354 explicit LocalizedNumberFormatter(const NumberFormatterSettings<LocalizedNumberFormatter>& other);
2355
2356 explicit LocalizedNumberFormatter(NumberFormatterSettings<LocalizedNumberFormatter>&& src) U_NOEXCEPT;
2357
2358 LocalizedNumberFormatter(const impl::MacroProps &macros, const Locale &locale);
2359
2360 LocalizedNumberFormatter(impl::MacroProps &&macros, const Locale &locale);
2361
2362 void lnfMoveHelper(LocalizedNumberFormatter&& src);
2363
2367 bool computeCompiled(UErrorCode& status) const;
2368
2369 // To give the fluent setters access to this class's constructor:
2370 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2371 friend class NumberFormatterSettings<LocalizedNumberFormatter>;
2372
2373 // To give UnlocalizedNumberFormatter::locale() access to this class's constructor:
2374 friend class UnlocalizedNumberFormatter;
2375};
2376
2384 public:
2385#ifndef U_HIDE_DEPRECATED_API
2395#endif /* U_HIDE_DEPRECATED_API */
2396
2406
2407#ifndef U_HIDE_DEPRECATED_API
2420#endif /* U_HIDE_DEPRECATED_API */
2421
2434
2435#ifndef U_HIDE_DEPRECATED_API
2457#endif /* U_HIDE_DEPRECATED_API */
2458
2491
2492#ifndef U_HIDE_DEPRECATED_API
2509#endif /* U_HIDE_DEPRECATED_API */
2510
2525
2526#ifndef U_HIDE_INTERNAL_API
2527
2532 void getDecimalQuantity(impl::DecimalQuantity& output, UErrorCode& status) const;
2533
2538 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
2539
2540#endif
2541
2546
2551
2558
2565
2571
2572 private:
2573 // Can't use LocalPointer because UFormattedNumberData is forward-declared
2574 const impl::UFormattedNumberData *fResults;
2575
2576 // Error code for the terminal methods
2577 UErrorCode fErrorCode;
2578
2583 explicit FormattedNumber(impl::UFormattedNumberData *results)
2584 : fResults(results), fErrorCode(U_ZERO_ERROR) {};
2585
2586 explicit FormattedNumber(UErrorCode errorCode)
2587 : fResults(nullptr), fErrorCode(errorCode) {};
2588
2589 // To give LocalizedNumberFormatter format methods access to this class's constructor:
2590 friend class LocalizedNumberFormatter;
2591};
2592
2638
2639} // namespace number
2641
2642#endif // U_HIDE_DRAFT_API
2643
2644#endif // __NUMBERFORMATTER_H__
2645
2646#endif /* #if !UCONFIG_NO_FORMATTING */
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
Base class for objects to which Unicode characters and strings can be appended.
Definition appendable.h:51
A unit of currency, such as USD (U.S.
Definition currunit.h:37
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition dcfmtsym.h:84
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition fpositer.h:55
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:108
Base class for all formats.
Definition format.h:96
"Smart pointer" base class; do not use directly: use LocalPointer etc.
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:188
A unit such as length, mass, volume, currency, etc.
Definition measunit.h:38
Defines numbering systems.
Definition numsys.h:60
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition plurrule.h:196
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:54
UMemory is the common ICU base class.
Definition uobject.h:112
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:287
A class that defines a rounding precision parameterized by a currency to be used when formatting numb...
Precision withCurrency(const CurrencyUnit &currency) const
Associates a currency with this rounding precision.
The result of a number formatting operation.
void getDecimalQuantity(impl::DecimalQuantity &output, UErrorCode &status) const
Gets the raw DecimalQuantity for plural rule selection.
FormattedNumber & operator=(const FormattedNumber &)=delete
Copying not supported; use move assignment instead.
FormattedNumber(const FormattedNumber &)=delete
Copying not supported; use move constructor instead.
Appendable & appendTo(Appendable &appendable, UErrorCode &status)
Appends the formatted number to an Appendable.
void getAllFieldPositionsImpl(FieldPositionIteratorHandler &fpih, UErrorCode &status) const
Populates the mutable builder type FieldPositionIteratorHandler.
void populateFieldPosition(FieldPosition &fieldPosition, UErrorCode &status)
Determine the start and end indices of the first occurrence of the given field in the output string.
UnicodeString toString() const
Returns a UnicodeString representation of the formatted number.
FormattedNumber & operator=(FormattedNumber &&src)
Move assignment: Leaves the source FormattedNumber in an undefined state.
UBool nextFieldPosition(FieldPosition &fieldPosition, UErrorCode &status) const
Determines the start and end indices of the next occurrence of the given field in the output string.
Appendable & appendTo(Appendable &appendable)
Appends the formatted number to an Appendable.
~FormattedNumber()
Destruct an instance of FormattedNumber, cleaning up any memory it might own.
UnicodeString toString(UErrorCode &status) const
Returns a UnicodeString representation of the formatted number.
void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const
Export the formatted number to a FieldPositionIterator.
void populateFieldPositionIterator(FieldPositionIterator &iterator, UErrorCode &status)
Export the formatted number to a FieldPositionIterator.
FormattedNumber(FormattedNumber &&src)
Move constructor: Leaves the source FormattedNumber in an undefined state.
A class that defines a rounding precision based on a number of fraction places and optionally signifi...
Precision withMinDigits(int32_t minSignificantDigits) const
Ensure that no less than this number of significant digits are retained when rounding according to fr...
Precision withMaxDigits(int32_t maxSignificantDigits) const
Ensure that no more than this number of significant digits are retained when rounding according to fr...
A class that defines a rounding precision parameterized by a rounding increment to be used when forma...
Precision withMinFraction(int32_t minFrac) const
Specifies the minimum number of fraction digits to render after the decimal separator,...
A class that defines the strategy for padding and truncating integers before the decimal separator.
static IntegerWidth zeroFillTo(int32_t minInt)
Pad numbers at the beginning with zeros to guarantee a certain number of numerals before the decimal ...
IntegerWidth truncateAt(int32_t maxInt)
Truncate numbers exceeding a certain number of numerals before the decimal separator.
A NumberFormatter that has a locale associated with it; this means .format() methods are available.
LocalizedNumberFormatter & operator=(LocalizedNumberFormatter &&src)
Move assignment operator: The source LocalizedNumberFormatter will be left in a valid but undefined s...
Format * toFormat(UErrorCode &status) const
Creates a representation of this LocalizedNumberFormat as an icu::Format, enabling the use of this nu...
FormattedNumber formatInt(int64_t value, UErrorCode &status) const
Format the given integer number to a string using the settings specified in the NumberFormatter fluen...
LocalizedNumberFormatter()=default
Default constructor: puts the formatter into a valid but undefined state.
LocalizedNumberFormatter(const LocalizedNumberFormatter &other)
Returns a copy of this LocalizedNumberFormatter.
const impl::NumberFormatterImpl * getCompiled() const
Internal method for testing.
int32_t getCallCount() const
Internal method for testing.
~LocalizedNumberFormatter()
Destruct this LocalizedNumberFormatter, cleaning up any memory it might own.
void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const
This is the core entrypoint to the number formatting pipeline.
FormattedNumber formatDecimal(StringPiece value, UErrorCode &status) const
Format the given decimal number to a string using the settings specified in the NumberFormatter fluen...
void getAffixImpl(bool isPrefix, bool isNegative, UnicodeString &result, UErrorCode &status) const
Internal method for DecimalFormat compatibility.
FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity &dq, UErrorCode &status) const
Internal method.
FormattedNumber formatDouble(double value, UErrorCode &status) const
Format the given float or double to a string using the settings specified in the NumberFormatter flue...
LocalizedNumberFormatter & operator=(const LocalizedNumberFormatter &other)
Copy assignment operator.
LocalizedNumberFormatter(LocalizedNumberFormatter &&src)
Move constructor: The source LocalizedNumberFormatter will be left in a valid but undefined state.
A class that defines the notation style to be used when formatting numbers in NumberFormatter.
static CompactNotation compactShort()
Print the number using short-form compact notation.
static ScientificNotation engineering()
Print the number using engineering notation, a variant of scientific notation in which the exponent m...
static CompactNotation compactLong()
Print the number using long-form compact notation.
static SimpleNotation simple()
Print the number using simple notation without any scaling by powers of ten.
static ScientificNotation scientific()
Print the number using scientific notation (also known as scientific form, standard index form,...
An abstract base class for specifying settings related to number formatting.
Derived adoptSymbols(NumberingSystem *symbols) const &
Specifies that the given numbering system should be used when fetching symbols.
Derived precision(const Precision &precision) const &
Specifies the rounding precision to use when formatting numbers.
Derived decimal(UNumberDecimalSeparatorDisplay style) &&
Overload of decimal() for use on an rvalue reference.
Derived unitWidth(UNumberUnitWidth width) const &
Sets the width of the unit (measure unit or currency).
Derived adoptUnit(icu::MeasureUnit *unit) const &
Like unit(), but takes ownership of a pointer.
Derived grouping(UGroupingStrategy strategy) const &
Specifies the grouping strategy to use when formatting numbers.
Derived perUnit(const icu::MeasureUnit &perUnit) &&
Overload of perUnit() for use on an rvalue reference.
Derived padding(const impl::Padder &padder) &&
Derived adoptSymbols(NumberingSystem *symbols) &&
Overload of adoptSymbols() for use on an rvalue reference.
Derived macros(const impl::MacroProps &macros) &&
Derived roundingMode(UNumberFormatRoundingMode roundingMode) &&
Overload of roundingMode() for use on an rvalue reference.
Derived rounding(const Rounder &rounder) const &
Derived integerWidth(const IntegerWidth &style) const &
Specifies the minimum and maximum number of digits to render before the decimal mark.
Derived macros(const impl::MacroProps &macros) const &
Internal fluent setter to overwrite the entire macros object.
Derived threshold(int32_t threshold) const &
Internal fluent setter to support a custom regulation threshold.
Derived perUnit(const icu::MeasureUnit &perUnit) const &
Sets a unit to be used in the denominator.
Derived integerWidth(const IntegerWidth &style) &&
Overload of integerWidth() for use on an rvalue reference.
Derived unit(const icu::MeasureUnit &unit) const &
Specifies the unit (unit of measure, currency, or percent) to associate with rendered numbers.
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Derived adoptPerUnit(icu::MeasureUnit *perUnit) const &
Like perUnit(), but takes ownership of a pointer.
Derived adoptUnit(icu::MeasureUnit *unit) &&
Overload of adoptUnit() for use on an rvalue reference.
Derived scale(const Scale &scale) &&
Overload of scale() for use on an rvalue reference.
Derived precision(const Precision &precision) &&
Overload of precision() for use on an rvalue reference.
Derived symbols(const DecimalFormatSymbols &symbols) &&
Overload of symbols() for use on an rvalue reference.
Derived padding(const impl::Padder &padder) const &
Set the padding strategy.
Derived notation(const Notation &notation) const &
Specifies the notation style (simple, scientific, or compact) for rendering numbers.
Derived sign(UNumberSignDisplay style) const &
Sets the plus/minus sign display strategy.
Derived grouping(UGroupingStrategy strategy) &&
Overload of grouping() for use on an rvalue reference.
Derived threshold(int32_t threshold) &&
Derived roundingMode(UNumberFormatRoundingMode roundingMode) const &
Specifies how to determine the direction to round a number when it has more digits than fit in the de...
Derived symbols(const DecimalFormatSymbols &symbols) const &
Specifies the symbols (decimal separator, grouping separator, percent sign, numerals,...
Derived macros(impl::MacroProps &&macros) &&
Derived sign(UNumberSignDisplay style) &&
Overload of sign() for use on an rvalue reference.
UnicodeString toSkeleton(UErrorCode &status) const
Creates a skeleton string representation of this number formatter.
Derived adoptPerUnit(icu::MeasureUnit *perUnit) &&
Overload of adoptPerUnit() for use on an rvalue reference.
Derived unitWidth(UNumberUnitWidth width) &&
Overload of unitWidth() for use on an rvalue reference.
Derived decimal(UNumberDecimalSeparatorDisplay style) const &
Sets the decimal separator display strategy.
Derived unit(const icu::MeasureUnit &unit) &&
Overload of unit() for use on an rvalue reference.
Derived macros(impl::MacroProps &&macros) const &
Derived scale(const Scale &scale) const &
Sets a scale (multiplier) to be used to scale the number by an arbitrary amount before formatting.
Derived notation(const Notation &notation) &&
Overload of notation() for use on an rvalue reference.
See the main description in numberformatter.h for documentation and examples.
static UnlocalizedNumberFormatter forSkeleton(const UnicodeString &skeleton, UErrorCode &status)
Call this method at the beginning of a NumberFormatter fluent chain to create an instance based on a ...
static UnlocalizedNumberFormatter with()
Call this method at the beginning of a NumberFormatter fluent chain in which the locale is not curren...
NumberFormatter()=delete
Use factory methods instead of the constructor to create a NumberFormatter.
static LocalizedNumberFormatter withLocale(const Locale &locale)
Call this method at the beginning of a NumberFormatter fluent chain in which the locale is known at t...
A class that defines the rounding precision to be used when formatting numbers in NumberFormatter.
static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures.
static FractionPrecision fixedFraction(int32_t minMaxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal ...
static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits)
Always show at least a certain number of significant digits/figures, padding with zeros if necessary.
Precision withMode(UNumberFormatRoundingMode roundingMode) const
Sets the rounding mode to use when picking the direction to round (up or down).
static FractionPrecision maxFraction(int32_t maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal ...
static SignificantDigitsPrecision minMaxDigits(int32_t a, int32_t b)
static IncrementPrecision increment(double roundingIncrement)
Show numbers rounded if necessary to the closest multiple of a certain rounding increment.
static CurrencyPrecision currency(UCurrencyUsage currencyUsage)
Show numbers rounded and padded according to the rules for the currency unit.
static FractionPrecision minFraction(int32_t minFractionPlaces)
Always show at least a certain number of fraction places after the decimal separator,...
static SignificantDigitsPrecision fixedDigits(int32_t a)
static Precision unlimited()
Show all available digits to full precision.
static FractionPrecision integer()
Show numbers rounded if necessary to the nearest integer.
static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits or significant figures.
static SignificantDigitsPrecision minDigits(int32_t a)
static SignificantDigitsPrecision maxDigits(int32_t a)
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 ...
static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits, int32_t maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures; in addition,...
A class that defines a quantity by which a number should be multiplied when formatting.
static Scale none()
Do not change the value of numbers when formatting or parsing.
static Scale powerOfTen(int32_t power)
Multiply numbers by a power of ten before formatting.
Scale(const Scale &other)
static Scale byDoubleAndPowerOfTen(double multiplicand, int32_t power)
Multiply a number by both a power of ten and by an arbitrary double value.
static Scale byDecimal(StringPiece multiplicand)
Multiply numbers by an arbitrary value before formatting.
Scale(int32_t magnitude, impl::DecNum *arbitraryToAdopt)
Scale & operator=(const Scale &other)
Scale(Scale &&src)
Scale & operator=(Scale &&src)
static Scale byDouble(double multiplicand)
Multiply numbers by an arbitrary value before formatting.
A class that defines the scientific notation style to be used when formatting numbers in NumberFormat...
ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const
Sets whether to show the sign on positive and negative exponents in scientific notation.
ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const
Sets the minimum number of digits to show in the exponent of scientific notation, padding with zeros ...
A NumberFormatter that does not yet have a locale.
UnlocalizedNumberFormatter()=default
Default constructor: puts the formatter into a valid but undefined state.
UnlocalizedNumberFormatter(UnlocalizedNumberFormatter &&src)
Move constructor: The source UnlocalizedNumberFormatter will be left in a valid but undefined state.
UnlocalizedNumberFormatter(const UnlocalizedNumberFormatter &other)
Returns a copy of this UnlocalizedNumberFormatter.
LocalizedNumberFormatter locale(const icu::Locale &locale) &&
Overload of locale() for use on an rvalue reference.
UnlocalizedNumberFormatter & operator=(UnlocalizedNumberFormatter &&src)
Move assignment operator: The source UnlocalizedNumberFormatter will be left in a valid but undefined...
UnlocalizedNumberFormatter & operator=(const UnlocalizedNumberFormatter &other)
Copy assignment operator.
LocalizedNumberFormatter locale(const icu::Locale &locale) const &
Associate the given locale with the number formatter.
Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UGroupingStrategy strategy)
int16_t getSecondary() const
static Grouper forStrategy(UGroupingStrategy grouping)
static Grouper forProperties(const DecimalFormatProperties &properties)
Resolve the values in Properties to a Grouper object.
int16_t getPrimary() const
static Padder forProperties(const DecimalFormatProperties &properties)
static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position)
SymbolsWrapper & operator=(const SymbolsWrapper &other)
SymbolsWrapper(SymbolsWrapper &&src)
bool isNumberingSystem() const
Whether the object is currently holding a NumberingSystem.
const DecimalFormatSymbols * getDecimalFormatSymbols() const
Get the DecimalFormatSymbols pointer.
SymbolsWrapper(const SymbolsWrapper &other)
void setTo(const DecimalFormatSymbols &dfs)
The provided object is copied, but we do not adopt it.
const NumberingSystem * getNumberingSystem() const
Get the NumberingSystem pointer.
void setTo(const NumberingSystem *ns)
Adopt the provided object.
UBool copyErrorTo(UErrorCode &status) const
SymbolsWrapper & operator=(SymbolsWrapper &&src)
bool isDecimalFormatSymbols() const
Whether the object is currently holding a DecimalFormatSymbols.
C++ API: Currency Unit Information.
C++ API: Symbols for formatting numbers.
C++ API: FieldPosition identifies the fields in a formatted output.
C++ API: FieldPosition Iterator.
C++ API: A unit for measuring a quantity.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
C++ API: units for percent and permille.
int16_t digits_t
Datatype for minimum/maximum fraction digits.
C++ API: PluralRules object.
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
C API: Encapsulates information about a currency.
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition ucurr.h:41
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:400
int8_t UBool
The ICU boolean type.
Definition umachine.h:236
#define TRUE
The TRUE value of a UBool.
Definition umachine.h:240
#define FALSE
The FALSE value of a UBool.
Definition umachine.h:244
C API: Compatibility APIs for number formatting.
UNumberFormatRoundingMode
The possible number format rounding modes.
Definition unum.h:279
@ UNUM_ROUND_HALFEVEN
Half-even rounding.
Definition unum.h:288
UNumberCompactStyle
Constants for specifying short or long format.
Definition unum.h:319
UNumberFormatPadPosition
The possible number format pad positions.
Definition unum.h:308
C-compatible API for localized number formatting; not recommended for C++.
UNumberSignDisplay
An enum declaring how to denote positive and negative numbers.
@ UNUM_SIGN_COUNT
One more than the highest UNumberSignDisplay value.
UNumberDecimalSeparatorDisplay
An enum declaring how to render the decimal separator.
@ UNUM_DECIMAL_SEPARATOR_COUNT
One more than the highest UNumberDecimalSeparatorDisplay value.
UNumberUnitWidth
An enum declaring how to render units, including currencies.
@ UNUM_UNIT_WIDTH_COUNT
One more than the highest UNumberUnitWidth value.
UGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i....
C++ API: Common ICU base class UObject.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition utypes.h:396
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition utypes.h:438
@ U_ZERO_ERROR
No error, no warning.
Definition utypes.h:430
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition utypes.h:694
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:360
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:138
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:137