ICU 76.1 76.1
Loading...
Searching...
No Matches
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#ifndef __NUMBERFORMATTER_H__
5#define __NUMBERFORMATTER_H__
6
7#include "unicode/utypes.h"
8
9#if U_SHOW_CPLUSPLUS_API
10
11#if !UCONFIG_NO_FORMATTING
12
13#include "unicode/appendable.h"
14#include "unicode/bytestream.h"
15#include "unicode/currunit.h"
16#include "unicode/dcfmtsym.h"
18#include "unicode/fieldpos.h"
19#include "unicode/fpositer.h"
20#include "unicode/measunit.h"
21#include "unicode/nounit.h"
22#include "unicode/parseerr.h"
23#include "unicode/plurrule.h"
24#include "unicode/ucurr.h"
25#include "unicode/unum.h"
27#include "unicode/uobject.h"
30
89U_NAMESPACE_BEGIN
90
91// Forward declarations:
92class IFixedDecimal;
93class FieldPositionIteratorHandler;
94class FormattedStringBuilder;
95
96namespace numparse::impl {
97
98// Forward declarations:
99class NumberParserImpl;
100class MultiplierParseHandler;
101
102} // namespace numparse::impl
103
104namespace units {
105
106// Forward declarations:
107class UnitsRouter;
108
109} // namespace units
110
111namespace number { // icu::number
112
113// Forward declarations:
114class UnlocalizedNumberFormatter;
115class LocalizedNumberFormatter;
116class SimpleNumberFormatter;
117class FormattedNumber;
118class Notation;
119class ScientificNotation;
120class Precision;
121class FractionPrecision;
122class CurrencyPrecision;
123class IncrementPrecision;
124class IntegerWidth;
125
126namespace impl {
127
128// can't be #ifndef U_HIDE_INTERNAL_API; referenced throughout this file in public classes
135
136// can't be #ifndef U_HIDE_INTERNAL_API; needed for struct initialization
143static constexpr int32_t kInternalDefaultThreshold = 3;
144
145// Forward declarations:
146class Padder;
147struct MacroProps;
148struct MicroProps;
149class DecimalQuantity;
151class NumberFormatterImpl;
152struct ParsedPatternInfo;
153class ScientificModifier;
155class RoundingImpl;
156class ScientificHandler;
157class Modifier;
159class NumberPropertyMapper;
161class MultiplierFormatHandler;
162class CurrencySymbols;
163class GeneratorHelpers;
164class DecNum;
165class NumberRangeFormatterImpl;
166struct RangeMacroProps;
167struct UFormattedNumberImpl;
168class MutablePatternModifier;
169class ImmutablePatternModifier;
171struct SimpleMicroProps;
173
181
182} // namespace impl
183
190
197
204 public:
230
254
297
321
347
348 private:
349 enum NotationType {
350 NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR
351 } fType;
352
353 union NotationUnion {
354 // For NTN_SCIENTIFIC
366
367 // For NTN_COMPACT
368 UNumberCompactStyle compactStyle;
369
370 // For NTN_ERROR
371 UErrorCode errorCode;
372 } fUnion;
373
375
376 Notation(const NotationType &type, const NotationUnion &union_) : fType(type), fUnion(union_) {}
377
378 Notation(UErrorCode errorCode) : fType(NTN_ERROR) {
379 fUnion.errorCode = errorCode;
380 }
381
382 Notation() : fType(NTN_SIMPLE), fUnion() {}
383
384 UBool copyErrorTo(UErrorCode &status) const {
385 if (fType == NTN_ERROR) {
386 status = fUnion.errorCode;
387 return true;
388 }
389 return false;
390 }
391
392 // To allow MacroProps to initialize empty instances:
393 friend struct impl::MacroProps;
394 friend class ScientificNotation;
395
396 // To allow implementation to access internal types:
397 friend class impl::NumberFormatterImpl;
398 friend class impl::ScientificModifier;
399 friend class impl::ScientificHandler;
400
401 // To allow access to the skeleton generation code:
402 friend class impl::GeneratorHelpers;
403};
404
414 public:
429
444
445 private:
446 // Inherit constructor
447 using Notation::Notation;
448
449 // Raw constructor for NumberPropertyMapper
450 ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, impl::digits_t fMinExponentDigits,
451 UNumberSignDisplay fExponentSignDisplay);
452
453 friend class Notation;
454
455 // So that NumberPropertyMapper can create instances
456 friend class impl::NumberPropertyMapper;
457};
458
465
475
476 public:
495
503
532
547
559
574
589
603
613
626 int32_t maxSignificantDigits);
627
648
673
692
701
702 private:
703 enum PrecisionType {
704 RND_BOGUS,
705 RND_NONE,
706 RND_FRACTION,
707 RND_SIGNIFICANT,
708 RND_FRACTION_SIGNIFICANT,
709
710 // Used for strange increments like 3.14.
711 RND_INCREMENT,
712
713 // Used for increments with 1 as the only digit. This is different than fraction
714 // rounding because it supports having additional trailing zeros. For example, this
715 // class is used to round with the increment 0.010.
716 RND_INCREMENT_ONE,
717
718 // Used for increments with 5 as the only digit (nickel rounding).
719 RND_INCREMENT_FIVE,
720
721 RND_CURRENCY,
722 RND_ERROR
723 } fType;
724
725 union PrecisionUnion {
747 // For RND_INCREMENT, RND_INCREMENT_ONE, and RND_INCREMENT_FIVE
748 // Note: This is a union, so we shouldn't own memory, since
749 // the default destructor would leak it.
756 } increment;
757 UCurrencyUsage currencyUsage; // For RND_CURRENCY
758 UErrorCode errorCode; // For RND_ERROR
759 } fUnion;
760
762
765
766 Precision(const PrecisionType& type, const PrecisionUnion& union_)
767 : fType(type), fUnion(union_) {}
768
769 Precision(UErrorCode errorCode) : fType(RND_ERROR) {
770 fUnion.errorCode = errorCode;
771 }
772
773 Precision() : fType(RND_BOGUS) {}
774
775 bool isBogus() const {
776 return fType == RND_BOGUS;
777 }
778
779 UBool copyErrorTo(UErrorCode &status) const {
780 if (fType == RND_ERROR) {
781 status = fUnion.errorCode;
782 return true;
783 }
784 return false;
785 }
786
787 // On the parent type so that this method can be called internally on Precision instances.
788 Precision withCurrency(const CurrencyUnit &currency, UErrorCode &status) const;
789
790 static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac);
791
792 static Precision constructSignificant(int32_t minSig, int32_t maxSig);
793
794 static Precision constructFractionSignificant(
795 const FractionPrecision &base,
796 int32_t minSig,
797 int32_t maxSig,
799 bool retain);
800
801 static IncrementPrecision constructIncrement(uint64_t increment, impl::digits_t magnitude);
802
803 static CurrencyPrecision constructCurrency(UCurrencyUsage usage);
804
805 // To allow MacroProps/MicroProps to initialize bogus instances:
806 friend struct impl::MacroProps;
807 friend struct impl::MicroProps;
808
809 // To allow NumberFormatterImpl to access isBogus() and other internal methods:
810 friend class impl::NumberFormatterImpl;
811
812 // To allow NumberPropertyMapper to create instances from DecimalFormatProperties:
813 friend class impl::NumberPropertyMapper;
814
815 // To allow access to the main implementation class:
816 friend class impl::RoundingImpl;
817
818 // To allow child classes to call private methods:
819 friend class FractionPrecision;
820 friend class CurrencyPrecision;
821 friend class IncrementPrecision;
822
823 // To allow access to the skeleton generation code:
824 friend class impl::GeneratorHelpers;
825
826 // To allow access to isBogus and the default (bogus) constructor:
827 friend class units::UnitsRouter;
828};
829
840 public:
856 int32_t minSignificantDigits,
857 int32_t maxSignificantDigits,
859
877 Precision withMinDigits(int32_t minSignificantDigits) const;
878
896 Precision withMaxDigits(int32_t maxSignificantDigits) const;
897
898 private:
899 // Inherit constructor
900 using Precision::Precision;
901
902 // To allow parent class to call this class's constructor:
903 friend class Precision;
904};
905
916 public:
934 Precision withCurrency(const CurrencyUnit &currency) const;
935
936 private:
937 // Inherit constructor
938 using Precision::Precision;
939
940 // To allow parent class to call this class's constructor:
941 friend class Precision;
942};
943
954 public:
971
972 private:
973 // Inherit constructor
974 using Precision::Precision;
975
976 // To allow parent class to call this class's constructor:
977 friend class Precision;
978};
979
990 public:
1003
1016
1017 private:
1018 union {
1019 struct {
1020 impl::digits_t fMinInt;
1021 impl::digits_t fMaxInt;
1022 bool fFormatFailIfMoreThanMaxDigits;
1023 } minMaxInt;
1024 UErrorCode errorCode;
1025 } fUnion;
1026 bool fHasError = false;
1027
1028 IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt, bool formatFailIfMoreThanMaxDigits);
1029
1030 IntegerWidth(UErrorCode errorCode) { // NOLINT
1031 fUnion.errorCode = errorCode;
1032 fHasError = true;
1033 }
1034
1035 IntegerWidth() { // NOLINT
1036 fUnion.minMaxInt.fMinInt = -1;
1037 }
1038
1040 static IntegerWidth standard() {
1041 return IntegerWidth::zeroFillTo(1);
1042 }
1043
1044 bool isBogus() const {
1045 return !fHasError && fUnion.minMaxInt.fMinInt == -1;
1046 }
1047
1048 UBool copyErrorTo(UErrorCode &status) const {
1049 if (fHasError) {
1050 status = fUnion.errorCode;
1051 return true;
1052 }
1053 return false;
1054 }
1055
1056 void apply(impl::DecimalQuantity &quantity, UErrorCode &status) const;
1057
1058 bool operator==(const IntegerWidth& other) const;
1059
1060 // To allow MacroProps/MicroProps to initialize empty instances:
1061 friend struct impl::MacroProps;
1062 friend struct impl::MicroProps;
1063
1064 // To allow NumberFormatterImpl to access isBogus():
1065 friend class impl::NumberFormatterImpl;
1066
1067 // To allow the use of this class when formatting:
1068 friend class impl::MutablePatternModifier;
1069 friend class impl::ImmutablePatternModifier;
1070
1071 // So that NumberPropertyMapper can create instances
1072 friend class impl::NumberPropertyMapper;
1073
1074 // To allow access to the skeleton generation code:
1075 friend class impl::GeneratorHelpers;
1076};
1077
1086class U_I18N_API Scale : public UMemory {
1087 public:
1094 static Scale none();
1095
1107
1121
1131
1139
1140 // We need a custom destructor for the DecNum, which means we need to declare
1141 // the copy/move constructor/assignment quartet.
1142
1145
1148
1150 Scale(Scale&& src) noexcept;
1151
1153 Scale& operator=(Scale&& src) noexcept;
1154
1157
1158#ifndef U_HIDE_INTERNAL_API
1161#endif /* U_HIDE_INTERNAL_API */
1162
1163 private:
1164 int32_t fMagnitude;
1165 impl::DecNum* fArbitrary;
1166 UErrorCode fError;
1167
1168 Scale(UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {}
1169
1170 Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR) {}
1171
1172 bool isValid() const {
1173 return fMagnitude != 0 || fArbitrary != nullptr;
1174 }
1175
1176 UBool copyErrorTo(UErrorCode &status) const {
1177 if (U_FAILURE(fError)) {
1178 status = fError;
1179 return true;
1180 }
1181 return false;
1182 }
1183
1184 void applyTo(impl::DecimalQuantity& quantity) const;
1185
1186 void applyReciprocalTo(impl::DecimalQuantity& quantity) const;
1187
1188 // To allow MacroProps/MicroProps to initialize empty instances:
1189 friend struct impl::MacroProps;
1190 friend struct impl::MicroProps;
1191
1192 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1193 friend class impl::NumberFormatterImpl;
1194
1195 // To allow the helper class MultiplierFormatHandler access to private fields:
1196 friend class impl::MultiplierFormatHandler;
1197
1198 // To allow access to the skeleton generation code:
1199 friend class impl::GeneratorHelpers;
1200
1201 // To allow access to parsing code:
1202 friend class ::icu::numparse::impl::NumberParserImpl;
1203 friend class ::icu::numparse::impl::MultiplierParseHandler;
1204};
1205
1206namespace impl {
1207
1208// Do not enclose entire StringProp with #ifndef U_HIDE_INTERNAL_API, needed for a protected field.
1209// And do not enclose its class boilerplate within #ifndef U_HIDE_INTERNAL_API.
1214class U_I18N_API StringProp : public UMemory {
1215
1216 public:
1218 ~StringProp();
1219
1221 StringProp(const StringProp &other);
1222
1224 StringProp &operator=(const StringProp &other);
1225
1226#ifndef U_HIDE_INTERNAL_API
1227
1229 StringProp(StringProp &&src) noexcept;
1230
1232 StringProp &operator=(StringProp &&src) noexcept;
1233
1235 int16_t length() const {
1236 return fLength;
1237 }
1238
1242 void set(StringPiece value);
1243
1245 bool isSet() const {
1246 return fLength > 0;
1247 }
1248
1249#endif // U_HIDE_INTERNAL_API
1250
1251 private:
1252 char *fValue;
1253 int16_t fLength;
1254 UErrorCode fError;
1255
1256 StringProp() : fValue(nullptr), fLength(0), fError(U_ZERO_ERROR) {
1257 }
1258
1260 UBool copyErrorTo(UErrorCode &status) const {
1261 if (U_FAILURE(fError)) {
1262 status = fError;
1263 return true;
1264 }
1265 return false;
1266 }
1267
1268 // Allow NumberFormatterImpl to access fValue.
1269 friend class impl::NumberFormatterImpl;
1270
1271 // Allow skeleton generation code to access private members.
1272 friend class impl::GeneratorHelpers;
1273
1274 // Allow MacroProps/MicroProps to initialize empty instances and to call
1275 // copyErrorTo().
1276 friend struct impl::MacroProps;
1277};
1278
1279// Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1281class U_I18N_API SymbolsWrapper : public UMemory {
1282 public:
1284 SymbolsWrapper() : fType(SYMPTR_NONE), fPtr{nullptr} {}
1285
1287 SymbolsWrapper(const SymbolsWrapper &other);
1288
1290 SymbolsWrapper &operator=(const SymbolsWrapper &other);
1291
1293 SymbolsWrapper(SymbolsWrapper&& src) noexcept;
1294
1296 SymbolsWrapper &operator=(SymbolsWrapper&& src) noexcept;
1297
1299 ~SymbolsWrapper();
1300
1301#ifndef U_HIDE_INTERNAL_API
1302
1307 void setTo(const DecimalFormatSymbols &dfs);
1308
1313 void setTo(const NumberingSystem *ns);
1314
1319 bool isDecimalFormatSymbols() const;
1320
1325 bool isNumberingSystem() const;
1326
1331 const DecimalFormatSymbols *getDecimalFormatSymbols() const;
1332
1337 const NumberingSystem *getNumberingSystem() const;
1338
1339#endif // U_HIDE_INTERNAL_API
1340
1342 UBool copyErrorTo(UErrorCode &status) const {
1343 if (fType == SYMPTR_DFS && fPtr.dfs == nullptr) {
1345 return true;
1346 } else if (fType == SYMPTR_NS && fPtr.ns == nullptr) {
1348 return true;
1349 }
1350 return false;
1351 }
1352
1353 private:
1354 enum SymbolsPointerType {
1355 SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS
1356 } fType;
1357
1358 union {
1359 const DecimalFormatSymbols *dfs;
1360 const NumberingSystem *ns;
1361 } fPtr;
1362
1363 void doCopyFrom(const SymbolsWrapper &other);
1364
1365 void doMoveFrom(SymbolsWrapper&& src);
1366
1367 void doCleanup();
1368};
1369
1370// Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1372class U_I18N_API Grouper : public UMemory {
1373 public:
1374#ifndef U_HIDE_INTERNAL_API
1376 static Grouper forStrategy(UNumberGroupingStrategy grouping);
1377
1382 static Grouper forProperties(const DecimalFormatProperties& properties);
1383
1384 // Future: static Grouper forProperties(DecimalFormatProperties& properties);
1385
1387 Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy)
1388 : fGrouping1(grouping1),
1389 fGrouping2(grouping2),
1390 fMinGrouping(minGrouping),
1391 fStrategy(strategy) {}
1392
1394 int16_t getPrimary() const;
1395
1397 int16_t getSecondary() const;
1398#endif // U_HIDE_INTERNAL_API
1399
1400 private:
1409 int16_t fGrouping1;
1410 int16_t fGrouping2;
1411
1419 int16_t fMinGrouping;
1420
1425 UNumberGroupingStrategy fStrategy;
1426
1427 Grouper() : fGrouping1(-3) {}
1428
1429 bool isBogus() const {
1430 return fGrouping1 == -3;
1431 }
1432
1434 void setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale);
1435
1436 bool groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const;
1437
1438 // To allow MacroProps/MicroProps to initialize empty instances:
1439 friend struct MacroProps;
1440 friend struct MicroProps;
1441 friend struct SimpleMicroProps;
1442
1443 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1444 friend class NumberFormatterImpl;
1445 friend class ::icu::number::SimpleNumberFormatter;
1446
1447 // To allow NumberParserImpl to perform setLocaleData():
1448 friend class ::icu::numparse::impl::NumberParserImpl;
1449
1450 // To allow access to the skeleton generation code:
1451 friend class impl::GeneratorHelpers;
1452};
1453
1454// Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1456class U_I18N_API Padder : public UMemory {
1457 public:
1458#ifndef U_HIDE_INTERNAL_API
1460 static Padder none();
1461
1463 static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position);
1464
1466 static Padder forProperties(const DecimalFormatProperties& properties);
1467#endif // U_HIDE_INTERNAL_API
1468
1469 private:
1470 UChar32 fWidth; // -3 = error; -2 = bogus; -1 = no padding
1471 union {
1472 struct {
1473 int32_t fCp;
1474 UNumberFormatPadPosition fPosition;
1475 } padding;
1476 UErrorCode errorCode;
1477 } fUnion;
1478
1479 Padder(UChar32 cp, int32_t width, UNumberFormatPadPosition position);
1480
1481 Padder(int32_t width);
1482
1483 Padder(UErrorCode errorCode) : fWidth(-3) { // NOLINT
1484 fUnion.errorCode = errorCode;
1485 }
1486
1487 Padder() : fWidth(-2) {} // NOLINT
1488
1489 bool isBogus() const {
1490 return fWidth == -2;
1491 }
1492
1493 UBool copyErrorTo(UErrorCode &status) const {
1494 if (fWidth == -3) {
1495 status = fUnion.errorCode;
1496 return true;
1497 }
1498 return false;
1499 }
1500
1501 bool isValid() const {
1502 return fWidth > 0;
1503 }
1504
1505 int32_t padAndApply(const impl::Modifier &mod1, const impl::Modifier &mod2,
1506 FormattedStringBuilder &string, int32_t leftIndex, int32_t rightIndex,
1507 UErrorCode &status) const;
1508
1509 // To allow MacroProps/MicroProps to initialize empty instances:
1510 friend struct MacroProps;
1511 friend struct MicroProps;
1512
1513 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1514 friend class impl::NumberFormatterImpl;
1515
1516 // To allow access to the skeleton generation code:
1517 friend class impl::GeneratorHelpers;
1518};
1519
1520// Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1522struct U_I18N_API MacroProps : public UMemory {
1524 Notation notation;
1525
1527 MeasureUnit unit; // = MeasureUnit(); (the base dimensionless unit)
1528
1530 MeasureUnit perUnit; // = MeasureUnit(); (the base dimensionless unit)
1531
1533 Precision precision; // = Precision(); (bogus)
1534
1537
1539 Grouper grouper; // = Grouper(); (bogus)
1540
1542 Padder padder; // = Padder(); (bogus)
1543
1545 IntegerWidth integerWidth; // = IntegerWidth(); (bogus)
1546
1548 SymbolsWrapper symbols;
1549
1550 // UNUM_XYZ_COUNT denotes null (bogus) values.
1551
1554
1557
1559 bool approximately = false;
1560
1563
1565 Scale scale; // = Scale(); (benign value)
1566
1568 StringProp usage; // = StringProp(); (no usage)
1569
1571 StringProp unitDisplayCase; // = StringProp(); (nominative)
1572
1574 const AffixPatternProvider* affixProvider = nullptr; // no ownership
1575
1577 const PluralRules* rules = nullptr; // no ownership
1578
1580 int32_t threshold = kInternalDefaultThreshold;
1581
1583 Locale locale;
1584
1585 // NOTE: Uses default copy and move constructors.
1586
1591 bool copyErrorTo(UErrorCode &status) const {
1592 return notation.copyErrorTo(status) || precision.copyErrorTo(status) ||
1593 padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) ||
1594 symbols.copyErrorTo(status) || scale.copyErrorTo(status) || usage.copyErrorTo(status) ||
1595 unitDisplayCase.copyErrorTo(status);
1596 }
1597};
1598
1599} // namespace impl
1600
1601#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
1602// Ignore MSVC warning 4661. This is generated for NumberFormatterSettings<>::toSkeleton() as this method
1603// is defined elsewhere (in number_skeletons.cpp). The compiler is warning that the explicit template instantiation
1604// inside this single translation unit (CPP file) is incomplete, and thus it isn't sure if the template class is
1605// fully defined. However, since each translation unit explicitly instantiates all the necessary template classes,
1606// they will all be passed to the linker, and the linker will still find and export all the class members.
1607#pragma warning(push)
1608#pragma warning(disable: 4661)
1609#endif
1610
1616template<typename Derived>
1618 public:
1647 Derived notation(const Notation &notation) const &;
1648
1658 Derived notation(const Notation &notation) &&;
1659
1708 Derived unit(const icu::MeasureUnit &unit) const &;
1709
1720
1735
1746
1769 Derived perUnit(const icu::MeasureUnit &perUnit) const &;
1770
1781
1796
1807
1838 Derived precision(const Precision& precision) const &;
1839
1849 Derived precision(const Precision& precision) &&;
1850
1870
1880
1909
1920
1946
1957
1998 Derived symbols(const DecimalFormatSymbols &symbols) const &;
1999
2010
2045
2056
2083
2094
2121
2132
2159
2170
2195 Derived scale(const Scale &scale) const &;
2196
2206 Derived scale(const Scale &scale) &&;
2207
2250 Derived usage(StringPiece usage) const &;
2251
2260
2269 Derived displayOptions(const DisplayOptions &displayOptions) const &;
2270
2278 Derived displayOptions(const DisplayOptions &displayOptions) &&;
2279
2280#ifndef U_HIDE_INTERNAL_API
2291 Derived unitDisplayCase(StringPiece unitDisplayCase) const &;
2292
2303#endif // U_HIDE_INTERNAL_API
2304
2305#ifndef U_HIDE_INTERNAL_API
2306
2312 Derived padding(const impl::Padder &padder) const &;
2313
2315 Derived padding(const impl::Padder &padder) &&;
2316
2323 Derived threshold(int32_t threshold) const &;
2324
2327
2333 Derived macros(const impl::MacroProps& macros) const &;
2334
2336 Derived macros(const impl::MacroProps& macros) &&;
2337
2339 Derived macros(impl::MacroProps&& macros) const &;
2340
2342 Derived macros(impl::MacroProps&& macros) &&;
2343
2344#endif /* U_HIDE_INTERNAL_API */
2345
2364
2377
2386
2393 UBool copyErrorTo(UErrorCode &outErrorCode) const {
2394 if (U_FAILURE(outErrorCode)) {
2395 // Do not overwrite the older error code
2396 return true;
2397 }
2398 fMacros.copyErrorTo(outErrorCode);
2399 return U_FAILURE(outErrorCode);
2400 }
2401
2402 // NOTE: Uses default copy and move constructors.
2403
2404 private:
2405 impl::MacroProps fMacros;
2406
2407 // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
2408 NumberFormatterSettings() = default;
2409
2410 friend class LocalizedNumberFormatter;
2411 friend class UnlocalizedNumberFormatter;
2412
2413 // Give NumberRangeFormatter access to the MacroProps
2414 friend void impl::touchRangeLocales(impl::RangeMacroProps& macros);
2415 friend class impl::NumberRangeFormatterImpl;
2416};
2417
2418// Explicit instantiations in source/i18n/number_fluent.cpp.
2419// (MSVC treats imports/exports of explicit instantiations differently.)
2420#ifndef _MSC_VER
2423#endif
2424
2435
2436 public:
2447
2458
2465
2471
2478
2484
2491
2492 private:
2494
2497
2498 explicit UnlocalizedNumberFormatter(const impl::MacroProps &macros);
2499
2500 explicit UnlocalizedNumberFormatter(impl::MacroProps &&macros);
2501
2502 // To give the fluent setters access to this class's constructor:
2504
2505 // To give NumberFormatter::with() access to this class's constructor:
2506 friend class NumberFormatter;
2507
2508 // To give LNF::withoutLocale() access to this class's constructor:
2509 friend class LocalizedNumberFormatter;
2510};
2511
2522 public:
2535
2548
2564
2565#ifndef U_HIDE_INTERNAL_API
2566
2567
2572
2576 FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity& dq, UErrorCode& status) const;
2577
2582
2587 const impl::NumberFormatterImpl* getCompiled() const;
2588
2594
2595#endif /* U_HIDE_INTERNAL_API */
2596
2611
2612#ifndef U_HIDE_DRAFT_API
2620
2628 UnlocalizedNumberFormatter withoutLocale() &&;
2629#endif // U_HIDE_DRAFT_API
2630
2637
2643
2650
2656
2663
2664#ifndef U_HIDE_INTERNAL_API
2665
2678 void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const;
2679
2680#endif /* U_HIDE_INTERNAL_API */
2681
2687
2688 private:
2689 // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal
2690 // header, and LocalPointer needs the full class definition in order to delete the instance.
2691 const impl::NumberFormatterImpl* fCompiled {nullptr};
2692 char fUnsafeCallCount[8] {}; // internally cast to u_atomic_int32_t
2693
2694 // Owned pointer to a DecimalFormatWarehouse, used when copying a LocalizedNumberFormatter
2695 // from a DecimalFormat.
2696 const impl::DecimalFormatWarehouse* fWarehouse {nullptr};
2697
2698 explicit LocalizedNumberFormatter(const NumberFormatterSettings<LocalizedNumberFormatter>& other);
2699
2700 explicit LocalizedNumberFormatter(NumberFormatterSettings<LocalizedNumberFormatter>&& src) noexcept;
2701
2702 LocalizedNumberFormatter(const impl::MacroProps &macros, const Locale &locale);
2703
2704 LocalizedNumberFormatter(impl::MacroProps &&macros, const Locale &locale);
2705
2706 void resetCompiled();
2707
2708 void lnfMoveHelper(LocalizedNumberFormatter&& src);
2709
2710 void lnfCopyHelper(const LocalizedNumberFormatter& src, UErrorCode& status);
2711
2715 bool computeCompiled(UErrorCode& status) const;
2716
2717 // To give the fluent setters access to this class's constructor:
2718 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>;
2719 friend class NumberFormatterSettings<LocalizedNumberFormatter>;
2720
2721 // To give UnlocalizedNumberFormatter::locale() access to this class's constructor:
2722 friend class UnlocalizedNumberFormatter;
2723};
2724
2725#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
2726// Warning 4661.
2727#pragma warning(pop)
2728#endif
2729
2804
2805} // namespace number
2807
2808#endif /* #if !UCONFIG_NO_FORMATTING */
2809
2810#endif /* U_SHOW_CPLUSPLUS_API */
2811
2812#endif // __NUMBERFORMATTER_H__
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
C++ API: Interface for writing bytes, and implementation classes.
A unit of currency, such as USD (U.S.
Definition currunit.h:39
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition dcfmtsym.h:86
Represents all the display options that are supported by CLDR such as grammatical case,...
Base class for all formats.
Definition format.h:98
"Smart pointer" base class; do not use directly: use LocalPointer etc.
"Smart pointer" class, deletes objects via the standard C++ delete operator.
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:195
A unit such as length, mass, volume, currency, etc.
Definition measunit.h:409
Defines numbering systems.
Definition numsys.h:60
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:61
UMemory is the common ICU base class.
Definition uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:296
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.
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 withSignificantDigits(int32_t minSignificantDigits, int32_t maxSignificantDigits, UNumberRoundingPriority priority) const
Override maximum fraction digits with maximum significant digits depending on the magnitude of the nu...
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(LocalizedNumberFormatter &&src) noexcept
Move constructor: The source LocalizedNumberFormatter will be left in a valid but undefined state.
Format * toFormat(UErrorCode &status) const
Creates a representation of this LocalizedNumberFormat as an icu::Format, enabling the use of this nu...
const DecimalFormatSymbols * getDecimalFormatSymbols() const
UnlocalizedNumberFormatter withoutLocale() const &
Disassociate the locale from this formatter.
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.
LocalizedNumberFormatter & operator=(LocalizedNumberFormatter &&src) noexcept
Move assignment operator: The source LocalizedNumberFormatter will be left in a valid but undefined s...
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.
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 unitDisplayCase(StringPiece unitDisplayCase) &&
NOTE: Use displayOptions instead.
LocalPointer< Derived > clone() const &
Returns the current (Un)LocalizedNumberFormatter as a LocalPointer wrapping a heap-allocated copy of ...
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 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 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 unitDisplayCase(StringPiece unitDisplayCase) const &
NOTE: Use displayOptions instead.
Derived grouping(UNumberGroupingStrategy strategy) const &
Specifies the grouping strategy to use when formatting numbers.
Derived unit(const icu::MeasureUnit &unit) const &
Specifies the unit (unit of measure, currency, or percent) to associate with rendered numbers.
Derived displayOptions(const DisplayOptions &displayOptions) const &
Specifies the DisplayOptions.
Derived usage(StringPiece usage) &&
Overload of usage() for use on an rvalue reference.
Derived displayOptions(const DisplayOptions &displayOptions) &&
Overload of displayOptions() for use on an rvalue reference.
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 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 grouping(UNumberGroupingStrategy strategy) &&
Overload of grouping() for use on an rvalue reference.
Derived usage(StringPiece usage) const &
Specifies the usage for which numbers will be formatted ("person-height", "road", "rainfall",...
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, UParseError &perror, UErrorCode &status)
Call this method at the beginning of a NumberFormatter fluent chain to create an instance based on a ...
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.
static FractionPrecision maxFraction(int32_t maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal ...
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,...
Precision trailingZeroDisplay(UNumberTrailingZeroDisplay trailingZeroDisplay) const
Configure how trailing zeros are displayed on numbers.
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 IncrementPrecision incrementExact(uint64_t mantissa, int16_t magnitude)
Version of Precision::increment() that takes an integer at a particular power of 10.
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) noexcept
Scale & operator=(Scale &&src) noexcept
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 & operator=(UnlocalizedNumberFormatter &&src) noexcept
Move assignment operator: The source UnlocalizedNumberFormatter will be left in a valid but undefined...
UnlocalizedNumberFormatter()=default
Default constructor: puts the formatter into 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=(const UnlocalizedNumberFormatter &other)
Copy assignment operator.
UnlocalizedNumberFormatter(UnlocalizedNumberFormatter &&src) noexcept
Move constructor: The source UnlocalizedNumberFormatter will be left in a valid but undefined state.
LocalizedNumberFormatter locale(const icu::Locale &locale) const &
Associate the given locale with the number formatter.
C++ API: Currency Unit Information.
C++ API: Symbols for formatting numbers.
C++ API: Display options class.
C++ API: FieldPosition identifies the fields in a formatted output.
C API: Formatted number result from various number formatting functions.
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.
void touchRangeLocales(impl::RangeMacroProps &macros)
Used for NumberRangeFormatter and implemented in numrange_fluent.cpp.
C API: Parse Error Information.
C++ API: PluralRules object.
A UParseError struct is used to returned detailed information about parsing errors.
Definition parseerr.h:58
bool fRetain
Whether to retain trailing zeros based on the looser strategy.
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:427
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:247
C API: Compatibility APIs for number formatting.
UNumberCompactStyle
Constants for specifying short or long format.
Definition unum.h:289
UNumberFormatPadPosition
The possible number format pad positions.
Definition unum.h:278
C API: Localized number formatting; not recommended for C++.
UNumberRoundingPriority
An enum declaring how to resolve conflicts between maximum fraction digits and maximum significant di...
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.
UNumberTrailingZeroDisplay
An enum declaring how to render trailing zeros.
@ UNUM_TRAILING_ZERO_AUTO
Display trailing zeros according to the settings for minimum fraction and significant digits.
UNumberUnitWidth
An enum declaring how to render units, including currencies.
@ UNUM_UNIT_WIDTH_COUNT
One more than the highest UNumberUnitWidth value.
C API: Header-only input options for various number formatting APIs.
UNumberFormatRoundingMode
The possible number format rounding modes.
@ UNUM_ROUND_HALFEVEN
Half-even rounding.
UNumberGroupingStrategy
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
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:430
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition utypes.h:473
@ U_ZERO_ERROR
No error, no warning.
Definition utypes.h:465
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition utypes.h:747
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:316