1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #ifndef __NUMBERFORMATTER_H__
5 #define __NUMBERFORMATTER_H__
7 #include "unicode/utypes.h"
9 #if U_SHOW_CPLUSPLUS_API
11 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/appendable.h"
14 #include "unicode/bytestream.h"
15 #include "unicode/currunit.h"
16 #include "unicode/dcfmtsym.h"
17 #include "unicode/fieldpos.h"
18 #include "unicode/formattedvalue.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"
26 #include "unicode/unumberformatter.h"
27 #include "unicode/uobject.h"
31 * \brief C++ API: Library for localized number formatting introduced in ICU 60.
33 * This library was introduced in ICU 60 to simplify the process of formatting localized number strings.
34 * Basic usage examples:
37 * // Most basic usage:
38 * NumberFormatter::withLocale(...).format(123).toString(); // 1,234 in en-US
40 * // Custom notation, unit, and rounding precision:
41 * NumberFormatter::with()
42 * .notation(Notation::compactShort())
43 * .unit(CurrencyUnit("EUR", status))
44 * .precision(Precision::maxDigits(2))
47 * .toString(); // €1.2K in en-US
49 * // Create a formatter in a singleton by value for use later:
50 * static const LocalizedNumberFormatter formatter = NumberFormatter::withLocale(...)
51 * .unit(NoUnit::percent())
52 * .precision(Precision::fixedFraction(3));
53 * formatter.format(5.9831).toString(); // 5.983% in en-US
55 * // Create a "template" in a singleton unique_ptr but without setting a locale until the call site:
56 * std::unique_ptr<UnlocalizedNumberFormatter> template = NumberFormatter::with()
57 * .sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS)
58 * .unit(MeasureUnit::getMeter())
59 * .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME)
61 * template->locale(...).format(1234).toString(); // +1,234 meters in en-US
65 * This API offers more features than DecimalFormat and is geared toward new users of ICU.
68 * NumberFormatter instances (i.e., LocalizedNumberFormatter and UnlocalizedNumberFormatter)
69 * are immutable and thread safe. This means that invoking a configuration method has no
70 * effect on the receiving instance; you must store and use the new number formatter instance it returns instead.
73 * UnlocalizedNumberFormatter formatter = UnlocalizedNumberFormatter::with().notation(Notation::scientific());
74 * formatter.precision(Precision.maxFraction(2)); // does nothing!
75 * formatter.locale(Locale.getEnglish()).format(9.8765).toString(); // prints "9.8765E0", not "9.88E0"
79 * This API is based on the <em>fluent</em> design pattern popularized by libraries such as Google's Guava. For
80 * extensive details on the design of this API, read <a href="https://goo.gl/szi5VB">the design doc</a>.
87 // Forward declarations:
89 class FieldPositionIteratorHandler
;
90 class FormattedStringBuilder
;
95 // Forward declarations:
96 class NumberParserImpl
;
97 class MultiplierParseHandler
;
102 namespace number
{ // icu::number
104 // Forward declarations:
105 class UnlocalizedNumberFormatter
;
106 class LocalizedNumberFormatter
;
107 class FormattedNumber
;
109 class ScientificNotation
;
111 class FractionPrecision
;
112 class CurrencyPrecision
;
113 class IncrementPrecision
;
118 // can't be #ifndef U_HIDE_INTERNAL_API; referenced throughout this file in public classes
120 * Datatype for minimum/maximum fraction digits. Must be able to hold kMaxIntFracSig.
124 typedef int16_t digits_t
;
126 // can't be #ifndef U_HIDE_INTERNAL_API; needed for struct initialization
128 * Use a default threshold of 3. This means that the third time .format() is called, the data structures get built
129 * using the "safe" code path. The first two calls to .format() will trigger the unsafe code path.
133 static constexpr int32_t kInternalDefaultThreshold
= 3;
135 // Forward declarations:
139 class DecimalQuantity
;
140 class UFormattedNumberData
;
141 class NumberFormatterImpl
;
142 struct ParsedPatternInfo
;
143 class ScientificModifier
;
144 class MultiplierProducer
;
146 class ScientificHandler
;
148 class AffixPatternProvider
;
149 class NumberPropertyMapper
;
150 struct DecimalFormatProperties
;
151 class MultiplierFormatHandler
;
152 class CurrencySymbols
;
153 class GeneratorHelpers
;
155 class NumberRangeFormatterImpl
;
156 struct RangeMacroProps
;
157 struct UFormattedNumberImpl
;
160 * Used for NumberRangeFormatter and implemented in numrange_fluent.cpp.
161 * Declared here so it can be friended.
165 void touchRangeLocales(impl::RangeMacroProps
& macros
);
170 * Extra name reserved in case it is needed in the future.
174 typedef Notation CompactNotation
;
177 * Extra name reserved in case it is needed in the future.
181 typedef Notation SimpleNotation
;
184 * A class that defines the notation style to be used when formatting numbers in NumberFormatter.
188 class U_I18N_API Notation
: public UMemory
{
191 * Print the number using scientific notation (also known as scientific form, standard index form, or standard form
192 * in the UK). The format for scientific notation varies by locale; for example, many Western locales display the
193 * number in the form "#E0", where the number is displayed with one digit before the decimal separator, zero or more
194 * digits after the decimal separator, and the corresponding power of 10 displayed after the "E".
197 * Example outputs in <em>en-US</em> when printing 8.765E4 through 8.765E-3:
211 * @return A ScientificNotation for chaining or passing to the NumberFormatter notation() setter.
214 static ScientificNotation
scientific();
217 * Print the number using engineering notation, a variant of scientific notation in which the exponent must be
221 * Example outputs in <em>en-US</em> when printing 8.765E4 through 8.765E-3:
235 * @return A ScientificNotation for chaining or passing to the NumberFormatter notation() setter.
238 static ScientificNotation
engineering();
241 * Print the number using short-form compact notation.
244 * <em>Compact notation</em>, defined in Unicode Technical Standard #35 Part 3 Section 2.4.1, prints numbers with
245 * localized prefixes or suffixes corresponding to different powers of ten. Compact notation is similar to
246 * engineering notation in how it scales numbers.
249 * Compact notation is ideal for displaying large numbers (over ~1000) to humans while at the same time minimizing
250 * screen real estate.
253 * In short form, the powers of ten are abbreviated. In <em>en-US</em>, the abbreviations are "K" for thousands, "M"
254 * for millions, "B" for billions, and "T" for trillions. Example outputs in <em>en-US</em> when printing 8.765E7
269 * When compact notation is specified without an explicit rounding precision, numbers are rounded off to the closest
270 * integer after scaling the number by the corresponding power of 10, but with a digit shown after the decimal
271 * separator if there is only one digit before the decimal separator. The default compact notation rounding precision
275 * Precision::integer().withMinDigits(2)
278 * @return A CompactNotation for passing to the NumberFormatter notation() setter.
281 static CompactNotation
compactShort();
284 * Print the number using long-form compact notation. For more information on compact notation, see
285 * {@link #compactShort}.
288 * In long form, the powers of ten are spelled out fully. Example outputs in <em>en-US</em> when printing 8.765E7
302 * @return A CompactNotation for passing to the NumberFormatter notation() setter.
305 static CompactNotation
compactLong();
308 * Print the number using simple notation without any scaling by powers of ten. This is the default behavior.
311 * Since this is the default behavior, this method needs to be called only when it is necessary to override a
315 * Example outputs in <em>en-US</em> when printing 8.765E7 through 8.765E0:
328 * @return A SimpleNotation for passing to the NumberFormatter notation() setter.
331 static SimpleNotation
simple();
335 NTN_SCIENTIFIC
, NTN_COMPACT
, NTN_SIMPLE
, NTN_ERROR
338 union NotationUnion
{
339 // For NTN_SCIENTIFIC
341 struct ScientificSettings
{
343 int8_t fEngineeringInterval
;
347 impl::digits_t fMinExponentDigits
;
349 UNumberSignDisplay fExponentSignDisplay
;
353 UNumberCompactStyle compactStyle
;
356 UErrorCode errorCode
;
359 typedef NotationUnion::ScientificSettings ScientificSettings
;
361 Notation(const NotationType
&type
, const NotationUnion
&union_
) : fType(type
), fUnion(union_
) {}
363 Notation(UErrorCode errorCode
) : fType(NTN_ERROR
) {
364 fUnion
.errorCode
= errorCode
;
367 Notation() : fType(NTN_SIMPLE
), fUnion() {}
369 UBool
copyErrorTo(UErrorCode
&status
) const {
370 if (fType
== NTN_ERROR
) {
371 status
= fUnion
.errorCode
;
377 // To allow MacroProps to initialize empty instances:
378 friend struct impl::MacroProps
;
379 friend class ScientificNotation
;
381 // To allow implementation to access internal types:
382 friend class impl::NumberFormatterImpl
;
383 friend class impl::ScientificModifier
;
384 friend class impl::ScientificHandler
;
386 // To allow access to the skeleton generation code:
387 friend class impl::GeneratorHelpers
;
391 * A class that defines the scientific notation style to be used when formatting numbers in NumberFormatter.
394 * To create a ScientificNotation, use one of the factory methods in {@link Notation}.
398 class U_I18N_API ScientificNotation
: public Notation
{
401 * Sets the minimum number of digits to show in the exponent of scientific notation, padding with zeros if
402 * necessary. Useful for fixed-width display.
405 * For example, with minExponentDigits=2, the number 123 will be printed as "1.23E02" in <em>en-US</em> instead of
406 * the default "1.23E2".
408 * @param minExponentDigits
409 * The minimum number of digits to show in the exponent.
410 * @return A ScientificNotation, for chaining.
413 ScientificNotation
withMinExponentDigits(int32_t minExponentDigits
) const;
416 * Sets whether to show the sign on positive and negative exponents in scientific notation. The default is AUTO,
417 * showing the minus sign but not the plus sign.
420 * For example, with exponentSignDisplay=ALWAYS, the number 123 will be printed as "1.23E+2" in <em>en-US</em>
421 * instead of the default "1.23E2".
423 * @param exponentSignDisplay
424 * The strategy for displaying the sign in the exponent.
425 * @return A ScientificNotation, for chaining.
428 ScientificNotation
withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay
) const;
431 // Inherit constructor
432 using Notation::Notation
;
434 // Raw constructor for NumberPropertyMapper
435 ScientificNotation(int8_t fEngineeringInterval
, bool fRequireMinInt
, impl::digits_t fMinExponentDigits
,
436 UNumberSignDisplay fExponentSignDisplay
);
438 friend class Notation
;
440 // So that NumberPropertyMapper can create instances
441 friend class impl::NumberPropertyMapper
;
445 * Extra name reserved in case it is needed in the future.
449 typedef Precision SignificantDigitsPrecision
;
452 * A class that defines the rounding precision to be used when formatting numbers in NumberFormatter.
455 * To create a Precision, use one of the factory methods.
459 class U_I18N_API Precision
: public UMemory
{
463 * Show all available digits to full precision.
466 * <strong>NOTE:</strong> When formatting a <em>double</em>, this method, along with {@link #minFraction} and
467 * {@link #minSignificantDigits}, will trigger complex algorithm similar to <em>Dragon4</em> to determine the
468 * low-order digits and the number of digits to display based on the value of the double.
469 * If the number of fraction places or significant digits can be bounded, consider using {@link #maxFraction}
470 * or {@link #maxSignificantDigits} instead to maximize performance.
471 * For more information, read the following blog post.
474 * http://www.serpentine.com/blog/2011/06/29/here-be-dragons-advances-in-problems-you-didnt-even-know-you-had/
476 * @return A Precision for chaining or passing to the NumberFormatter precision() setter.
479 static Precision
unlimited();
482 * Show numbers rounded if necessary to the nearest integer.
484 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
487 static FractionPrecision
integer();
490 * Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator).
491 * Additionally, pad with zeros to ensure that this number of places are always shown.
494 * Example output with minMaxFractionPlaces = 3:
508 * This method is equivalent to {@link #minMaxFraction} with both arguments equal.
510 * @param minMaxFractionPlaces
511 * The minimum and maximum number of numerals to display after the decimal separator (rounding if too
512 * long or padding with zeros if too short).
513 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
516 static FractionPrecision
fixedFraction(int32_t minMaxFractionPlaces
);
519 * Always show at least a certain number of fraction places after the decimal separator, padding with zeros if
520 * necessary. Do not perform rounding (display numbers to their full precision).
523 * <strong>NOTE:</strong> If you are formatting <em>doubles</em>, see the performance note in {@link #unlimited}.
525 * @param minFractionPlaces
526 * The minimum number of numerals to display after the decimal separator (padding with zeros if
528 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
531 static FractionPrecision
minFraction(int32_t minFractionPlaces
);
534 * Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator).
535 * Unlike the other fraction rounding strategies, this strategy does <em>not</em> pad zeros to the end of the
538 * @param maxFractionPlaces
539 * The maximum number of numerals to display after the decimal mark (rounding if necessary).
540 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
543 static FractionPrecision
maxFraction(int32_t maxFractionPlaces
);
546 * Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator);
547 * in addition, always show at least a certain number of places after the decimal separator, padding with zeros if
550 * @param minFractionPlaces
551 * The minimum number of numerals to display after the decimal separator (padding with zeros if
553 * @param maxFractionPlaces
554 * The maximum number of numerals to display after the decimal separator (rounding if necessary).
555 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
558 static FractionPrecision
minMaxFraction(int32_t minFractionPlaces
, int32_t maxFractionPlaces
);
561 * Show numbers rounded if necessary to a certain number of significant digits or significant figures. Additionally,
562 * pad with zeros to ensure that this number of significant digits/figures are always shown.
565 * This method is equivalent to {@link #minMaxSignificantDigits} with both arguments equal.
567 * @param minMaxSignificantDigits
568 * The minimum and maximum number of significant digits to display (rounding if too long or padding with
569 * zeros if too short).
570 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
573 static SignificantDigitsPrecision
fixedSignificantDigits(int32_t minMaxSignificantDigits
);
576 * Always show at least a certain number of significant digits/figures, padding with zeros if necessary. Do not
577 * perform rounding (display numbers to their full precision).
580 * <strong>NOTE:</strong> If you are formatting <em>doubles</em>, see the performance note in {@link #unlimited}.
582 * @param minSignificantDigits
583 * The minimum number of significant digits to display (padding with zeros if too short).
584 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
587 static SignificantDigitsPrecision
minSignificantDigits(int32_t minSignificantDigits
);
590 * Show numbers rounded if necessary to a certain number of significant digits/figures.
592 * @param maxSignificantDigits
593 * The maximum number of significant digits to display (rounding if too long).
594 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
597 static SignificantDigitsPrecision
maxSignificantDigits(int32_t maxSignificantDigits
);
600 * Show numbers rounded if necessary to a certain number of significant digits/figures; in addition, always show at
601 * least a certain number of significant digits, padding with zeros if necessary.
603 * @param minSignificantDigits
604 * The minimum number of significant digits to display (padding with zeros if necessary).
605 * @param maxSignificantDigits
606 * The maximum number of significant digits to display (rounding if necessary).
607 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
610 static SignificantDigitsPrecision
minMaxSignificantDigits(int32_t minSignificantDigits
,
611 int32_t maxSignificantDigits
);
614 * Show numbers rounded if necessary to the closest multiple of a certain rounding increment. For example, if the
615 * rounding increment is 0.5, then round 1.2 to 1 and round 1.3 to 1.5.
618 * In order to ensure that numbers are padded to the appropriate number of fraction places, call
619 * withMinFraction() on the return value of this method.
620 * For example, to round to the nearest 0.5 and always display 2 numerals after the
621 * decimal separator (to display 1.2 as "1.00" and 1.3 as "1.50"), you can run:
624 * Precision::increment(0.5).withMinFraction(2)
627 * @param roundingIncrement
628 * The increment to which to round numbers.
629 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
632 static IncrementPrecision
increment(double roundingIncrement
);
635 * Show numbers rounded and padded according to the rules for the currency unit. The most common
636 * rounding precision settings for currencies include <code>Precision::fixedFraction(2)</code>,
637 * <code>Precision::integer()</code>, and <code>Precision::increment(0.05)</code> for cash transactions
638 * ("nickel rounding").
641 * The exact rounding details will be resolved at runtime based on the currency unit specified in the
642 * NumberFormatter chain. To round according to the rules for one currency while displaying the symbol for another
643 * currency, the withCurrency() method can be called on the return value of this method.
645 * @param currencyUsage
646 * Either STANDARD (for digital transactions) or CASH (for transactions where the rounding increment may
647 * be limited by the available denominations of cash or coins).
648 * @return A CurrencyPrecision for chaining or passing to the NumberFormatter precision() setter.
651 static CurrencyPrecision
currency(UCurrencyUsage currencyUsage
);
659 RND_FRACTION_SIGNIFICANT
,
661 // Used for strange increments like 3.14.
664 // Used for increments with 1 as the only digit. This is different than fraction
665 // rounding because it supports having additional trailing zeros. For example, this
666 // class is used to round with the increment 0.010.
669 // Used for increments with 5 as the only digit (nickel rounding).
673 RND_INCREMENT_SIGNIFICANT
, // Apple addition rdar://52538227
677 union PrecisionUnion
{
679 struct FractionSignificantSettings
{
680 // For RND_FRACTION, RND_SIGNIFICANT, and RND_FRACTION_SIGNIFICANT
682 impl::digits_t fMinFrac
;
684 impl::digits_t fMaxFrac
;
686 impl::digits_t fMinSig
;
688 impl::digits_t fMaxSig
;
691 struct IncrementSettings
{
692 // For RND_INCREMENT, RND_INCREMENT_ONE, and RND_INCREMENT_FIVE
696 impl::digits_t fMinFrac
;
698 impl::digits_t fMaxFrac
;
701 struct IncrementSignificantSettings
{ // Apple addition rdar://52538227
702 // For // Apple addition rdar://52538227
706 impl::digits_t fMinSig
;
708 impl::digits_t fMaxSig
;
710 UCurrencyUsage currencyUsage
; // For RND_CURRENCY
711 UErrorCode errorCode
; // For RND_ERROR
714 typedef PrecisionUnion::FractionSignificantSettings FractionSignificantSettings
;
715 typedef PrecisionUnion::IncrementSettings IncrementSettings
;
716 typedef PrecisionUnion::IncrementSignificantSettings IncrementSignificantSettings
;
718 /** The Precision encapsulates the RoundingMode when used within the implementation. */
719 UNumberFormatRoundingMode fRoundingMode
;
721 Precision(const PrecisionType
& type
, const PrecisionUnion
& union_
,
722 UNumberFormatRoundingMode roundingMode
)
723 : fType(type
), fUnion(union_
), fRoundingMode(roundingMode
) {}
725 Precision(UErrorCode errorCode
) : fType(RND_ERROR
) {
726 fUnion
.errorCode
= errorCode
;
729 Precision() : fType(RND_BOGUS
) {}
731 bool isBogus() const {
732 return fType
== RND_BOGUS
;
735 UBool
copyErrorTo(UErrorCode
&status
) const {
736 if (fType
== RND_ERROR
) {
737 status
= fUnion
.errorCode
;
743 // On the parent type so that this method can be called internally on Precision instances.
744 Precision
withCurrency(const CurrencyUnit
¤cy
, UErrorCode
&status
) const;
746 static FractionPrecision
constructFraction(int32_t minFrac
, int32_t maxFrac
);
748 static Precision
constructSignificant(int32_t minSig
, int32_t maxSig
);
750 static Precision
constructIncrementSignificant(double increment
, int32_t minSig
, int32_t maxSig
); // Apple
753 constructFractionSignificant(const FractionPrecision
&base
, int32_t minSig
, int32_t maxSig
);
755 static IncrementPrecision
constructIncrement(double increment
, int32_t minFrac
);
757 static CurrencyPrecision
constructCurrency(UCurrencyUsage usage
);
759 static Precision
constructPassThrough();
761 // To allow MacroProps/MicroProps to initialize bogus instances:
762 friend struct impl::MacroProps
;
763 friend struct impl::MicroProps
;
765 // To allow NumberFormatterImpl to access isBogus() and other internal methods:
766 friend class impl::NumberFormatterImpl
;
768 // To allow NumberPropertyMapper to create instances from DecimalFormatProperties:
769 friend class impl::NumberPropertyMapper
;
771 // To allow access to the main implementation class:
772 friend class impl::RoundingImpl
;
774 // To allow child classes to call private methods:
775 friend class FractionPrecision
;
776 friend class CurrencyPrecision
;
777 friend class IncrementPrecision
;
779 // To allow access to the skeleton generation code:
780 friend class impl::GeneratorHelpers
;
784 * A class that defines a rounding precision based on a number of fraction places and optionally significant digits to be
785 * used when formatting numbers in NumberFormatter.
788 * To create a FractionPrecision, use one of the factory methods on Precision.
792 class U_I18N_API FractionPrecision
: public Precision
{
795 * Ensure that no less than this number of significant digits are retained when rounding according to fraction
799 * For example, with integer rounding, the number 3.141 becomes "3". However, with minimum figures set to 2, 3.141
800 * becomes "3.1" instead.
803 * This setting does not affect the number of trailing zeros. For example, 3.01 would print as "3", not "3.0".
805 * @param minSignificantDigits
806 * The number of significant figures to guarantee.
807 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
810 Precision
withMinDigits(int32_t minSignificantDigits
) const;
813 * Ensure that no more than this number of significant digits are retained when rounding according to fraction
817 * For example, with integer rounding, the number 123.4 becomes "123". However, with maximum figures set to 2, 123.4
818 * becomes "120" instead.
821 * This setting does not affect the number of trailing zeros. For example, with fixed fraction of 2, 123.4 would
824 * @param maxSignificantDigits
825 * Round the number to no more than this number of significant figures.
826 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
829 Precision
withMaxDigits(int32_t maxSignificantDigits
) const;
832 // Inherit constructor
833 using Precision::Precision
;
835 // To allow parent class to call this class's constructor:
836 friend class Precision
;
840 * A class that defines a rounding precision parameterized by a currency to be used when formatting numbers in
844 * To create a CurrencyPrecision, use one of the factory methods on Precision.
848 class U_I18N_API CurrencyPrecision
: public Precision
{
851 * Associates a currency with this rounding precision.
854 * <strong>Calling this method is <em>not required</em></strong>, because the currency specified in unit()
855 * is automatically applied to currency rounding precisions. However,
856 * this method enables you to override that automatic association.
859 * This method also enables numbers to be formatted using currency rounding rules without explicitly using a
863 * The currency to associate with this rounding precision.
864 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
867 Precision
withCurrency(const CurrencyUnit
¤cy
) const;
870 // Inherit constructor
871 using Precision::Precision
;
873 // To allow parent class to call this class's constructor:
874 friend class Precision
;
878 * A class that defines a rounding precision parameterized by a rounding increment to be used when formatting numbers in
882 * To create an IncrementPrecision, use one of the factory methods on Precision.
886 class U_I18N_API IncrementPrecision
: public Precision
{
889 * Specifies the minimum number of fraction digits to render after the decimal separator, padding with zeros if
890 * necessary. By default, no trailing zeros are added.
893 * For example, if the rounding increment is 0.5 and minFrac is 2, then the resulting strings include "0.00",
894 * "0.50", "1.00", and "1.50".
897 * Note: In ICU4J, this functionality is accomplished via the scale of the BigDecimal rounding increment.
899 * @param minFrac The minimum number of digits after the decimal separator.
900 * @return A precision for chaining or passing to the NumberFormatter precision() setter.
903 Precision
withMinFraction(int32_t minFrac
) const;
906 // Inherit constructor
907 using Precision::Precision
;
909 // To allow parent class to call this class's constructor:
910 friend class Precision
;
914 * A class that defines the strategy for padding and truncating integers before the decimal separator.
917 * To create an IntegerWidth, use one of the factory methods.
920 * @see NumberFormatter
922 class U_I18N_API IntegerWidth
: public UMemory
{
925 * Pad numbers at the beginning with zeros to guarantee a certain number of numerals before the decimal separator.
928 * For example, with minInt=3, the number 55 will get printed as "055".
931 * The minimum number of places before the decimal separator.
932 * @return An IntegerWidth for chaining or passing to the NumberFormatter integerWidth() setter.
935 static IntegerWidth
zeroFillTo(int32_t minInt
);
938 * Truncate numbers exceeding a certain number of numerals before the decimal separator.
940 * For example, with maxInt=3, the number 1234 will get printed as "234".
943 * The maximum number of places before the decimal separator. maxInt == -1 means no
945 * @return An IntegerWidth for passing to the NumberFormatter integerWidth() setter.
948 IntegerWidth
truncateAt(int32_t maxInt
);
953 impl::digits_t fMinInt
;
954 impl::digits_t fMaxInt
;
955 bool fFormatFailIfMoreThanMaxDigits
;
957 UErrorCode errorCode
;
959 bool fHasError
= false;
961 IntegerWidth(impl::digits_t minInt
, impl::digits_t maxInt
, bool formatFailIfMoreThanMaxDigits
);
963 IntegerWidth(UErrorCode errorCode
) { // NOLINT
964 fUnion
.errorCode
= errorCode
;
968 IntegerWidth() { // NOLINT
969 fUnion
.minMaxInt
.fMinInt
= -1;
972 /** Returns the default instance. */
973 static IntegerWidth
standard() {
974 return IntegerWidth::zeroFillTo(1);
977 bool isBogus() const {
978 return !fHasError
&& fUnion
.minMaxInt
.fMinInt
== -1;
981 UBool
copyErrorTo(UErrorCode
&status
) const {
983 status
= fUnion
.errorCode
;
989 void apply(impl::DecimalQuantity
&quantity
, UErrorCode
&status
) const;
991 bool operator==(const IntegerWidth
& other
) const;
993 // To allow MacroProps/MicroProps to initialize empty instances:
994 friend struct impl::MacroProps
;
995 friend struct impl::MicroProps
;
997 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
998 friend class impl::NumberFormatterImpl
;
1000 // So that NumberPropertyMapper can create instances
1001 friend class impl::NumberPropertyMapper
;
1003 // To allow access to the skeleton generation code:
1004 friend class impl::GeneratorHelpers
;
1008 * A class that defines a quantity by which a number should be multiplied when formatting.
1011 * To create a Scale, use one of the factory methods.
1015 class U_I18N_API Scale
: public UMemory
{
1018 * Do not change the value of numbers when formatting or parsing.
1020 * @return A Scale to prevent any multiplication.
1023 static Scale
none();
1026 * Multiply numbers by a power of ten before formatting. Useful for combining with a percent unit:
1029 * NumberFormatter::with().unit(NoUnit::percent()).multiplier(Scale::powerOfTen(2))
1032 * @return A Scale for passing to the setter in NumberFormatter.
1035 static Scale
powerOfTen(int32_t power
);
1038 * Multiply numbers by an arbitrary value before formatting. Useful for unit conversions.
1040 * This method takes a string in a decimal number format with syntax
1041 * as defined in the Decimal Arithmetic Specification, available at
1042 * http://speleotrove.com/decimal
1044 * Also see the version of this method that takes a double.
1046 * @return A Scale for passing to the setter in NumberFormatter.
1049 static Scale
byDecimal(StringPiece multiplicand
);
1052 * Multiply numbers by an arbitrary value before formatting. Useful for unit conversions.
1054 * This method takes a double; also see the version of this method that takes an exact decimal.
1056 * @return A Scale for passing to the setter in NumberFormatter.
1059 static Scale
byDouble(double multiplicand
);
1062 * Multiply a number by both a power of ten and by an arbitrary double value.
1064 * @return A Scale for passing to the setter in NumberFormatter.
1067 static Scale
byDoubleAndPowerOfTen(double multiplicand
, int32_t power
);
1069 // We need a custom destructor for the DecNum, which means we need to declare
1070 // the copy/move constructor/assignment quartet.
1072 /** @stable ICU 62 */
1073 Scale(const Scale
& other
);
1075 /** @stable ICU 62 */
1076 Scale
& operator=(const Scale
& other
);
1078 /** @stable ICU 62 */
1079 Scale(Scale
&& src
) U_NOEXCEPT
;
1081 /** @stable ICU 62 */
1082 Scale
& operator=(Scale
&& src
) U_NOEXCEPT
;
1084 /** @stable ICU 62 */
1087 #ifndef U_HIDE_INTERNAL_API
1089 Scale(int32_t magnitude
, impl::DecNum
* arbitraryToAdopt
);
1090 #endif /* U_HIDE_INTERNAL_API */
1094 impl::DecNum
* fArbitrary
;
1097 Scale(UErrorCode error
) : fMagnitude(0), fArbitrary(nullptr), fError(error
) {}
1099 Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR
) {}
1101 bool isValid() const {
1102 return fMagnitude
!= 0 || fArbitrary
!= nullptr;
1105 UBool
copyErrorTo(UErrorCode
&status
) const {
1106 if (fError
!= U_ZERO_ERROR
) {
1113 void applyTo(impl::DecimalQuantity
& quantity
) const;
1115 void applyReciprocalTo(impl::DecimalQuantity
& quantity
) const;
1117 // To allow MacroProps/MicroProps to initialize empty instances:
1118 friend struct impl::MacroProps
;
1119 friend struct impl::MicroProps
;
1121 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1122 friend class impl::NumberFormatterImpl
;
1124 // To allow the helper class MultiplierFormatHandler access to private fields:
1125 friend class impl::MultiplierFormatHandler
;
1127 // To allow access to the skeleton generation code:
1128 friend class impl::GeneratorHelpers
;
1130 // To allow access to parsing code:
1131 friend class ::icu::numparse::impl::NumberParserImpl
;
1132 friend class ::icu::numparse::impl::MultiplierParseHandler
;
1137 // Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1139 class U_I18N_API SymbolsWrapper
: public UMemory
{
1142 SymbolsWrapper() : fType(SYMPTR_NONE
), fPtr
{nullptr} {}
1145 SymbolsWrapper(const SymbolsWrapper
&other
);
1148 SymbolsWrapper
&operator=(const SymbolsWrapper
&other
);
1151 SymbolsWrapper(SymbolsWrapper
&& src
) U_NOEXCEPT
;
1154 SymbolsWrapper
&operator=(SymbolsWrapper
&& src
) U_NOEXCEPT
;
1159 #ifndef U_HIDE_INTERNAL_API
1162 * Set whether DecimalFormatSymbols copy is deep (clone)
1163 * or shallow (pointer copy). Apple <rdar://problem/49955427>
1166 void setDFSShallowCopy(UBool shallow
);
1169 * The provided object is copied, but we do not adopt it.
1172 void setTo(const DecimalFormatSymbols
&dfs
);
1175 * Adopt the provided object.
1178 void setTo(const NumberingSystem
*ns
);
1181 * Whether the object is currently holding a DecimalFormatSymbols.
1184 bool isDecimalFormatSymbols() const;
1187 * Whether the object is currently holding a NumberingSystem.
1190 bool isNumberingSystem() const;
1193 * Get the DecimalFormatSymbols pointer. No ownership change.
1196 const DecimalFormatSymbols
*getDecimalFormatSymbols() const;
1199 * Get the NumberingSystem pointer. No ownership change.
1202 const NumberingSystem
*getNumberingSystem() const;
1204 #endif // U_HIDE_INTERNAL_API
1207 UBool
copyErrorTo(UErrorCode
&status
) const {
1208 if ((fType
== SYMPTR_DFS
|| fType
== SYMPTR_DFS_SHALLOWCOPY
) && fPtr
.dfs
== nullptr) {
1209 status
= U_MEMORY_ALLOCATION_ERROR
;
1211 } else if (fType
== SYMPTR_NS
&& fPtr
.ns
== nullptr) {
1212 status
= U_MEMORY_ALLOCATION_ERROR
;
1219 enum SymbolsPointerType
{
1220 SYMPTR_NONE
, SYMPTR_DFS
, SYMPTR_NS
, SYMPTR_DFS_SHALLOWCOPY
// Apple <rdar://problem/49955427> add SHALLOWCOPY
1224 const DecimalFormatSymbols
*dfs
;
1225 const NumberingSystem
*ns
;
1228 void doCopyFrom(const SymbolsWrapper
&other
);
1230 void doMoveFrom(SymbolsWrapper
&& src
);
1235 // Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1237 class U_I18N_API Grouper
: public UMemory
{
1239 #ifndef U_HIDE_INTERNAL_API
1241 static Grouper
forStrategy(UNumberGroupingStrategy grouping
);
1244 * Resolve the values in Properties to a Grouper object.
1247 static Grouper
forProperties(const DecimalFormatProperties
& properties
);
1249 // Future: static Grouper forProperties(DecimalFormatProperties& properties);
1252 Grouper(int16_t grouping1
, int16_t grouping2
, int16_t minGrouping
, UNumberGroupingStrategy strategy
)
1253 : fGrouping1(grouping1
),
1254 fGrouping2(grouping2
),
1255 fMinGrouping(minGrouping
),
1256 fStrategy(strategy
) {}
1257 #endif // U_HIDE_INTERNAL_API
1260 int16_t getPrimary() const;
1263 int16_t getSecondary() const;
1267 * The grouping sizes, with the following special values:
1269 * <li>-1 = no grouping
1270 * <li>-2 = needs locale data
1271 * <li>-4 = fall back to Western grouping if not in locale
1278 * The minimum grouping size, with the following special values:
1280 * <li>-2 = needs locale data
1281 * <li>-3 = no less than 2
1284 int16_t fMinGrouping
;
1287 * The UNumberGroupingStrategy that was used to create this Grouper, or UNUM_GROUPING_COUNT if this
1288 * was not created from a UNumberGroupingStrategy.
1290 UNumberGroupingStrategy fStrategy
;
1292 Grouper() : fGrouping1(-3) {}
1294 bool isBogus() const {
1295 return fGrouping1
== -3;
1298 /** NON-CONST: mutates the current instance. */
1299 void setLocaleData(const impl::ParsedPatternInfo
&patternInfo
, const Locale
& locale
);
1301 bool groupAtPosition(int32_t position
, const impl::DecimalQuantity
&value
) const;
1303 // To allow MacroProps/MicroProps to initialize empty instances:
1304 friend struct MacroProps
;
1305 friend struct MicroProps
;
1307 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1308 friend class NumberFormatterImpl
;
1310 // To allow NumberParserImpl to perform setLocaleData():
1311 friend class ::icu::numparse::impl::NumberParserImpl
;
1313 // To allow access to the skeleton generation code:
1314 friend class impl::GeneratorHelpers
;
1317 // Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1319 class U_I18N_API Padder
: public UMemory
{
1321 #ifndef U_HIDE_INTERNAL_API
1323 static Padder
none();
1326 static Padder
codePoints(UChar32 cp
, int32_t targetWidth
, UNumberFormatPadPosition position
);
1327 #endif // U_HIDE_INTERNAL_API
1330 static Padder
forProperties(const DecimalFormatProperties
& properties
);
1333 UChar32 fWidth
; // -3 = error; -2 = bogus; -1 = no padding
1337 UNumberFormatPadPosition fPosition
;
1339 UErrorCode errorCode
;
1342 Padder(UChar32 cp
, int32_t width
, UNumberFormatPadPosition position
);
1344 Padder(int32_t width
);
1346 Padder(UErrorCode errorCode
) : fWidth(-3) { // NOLINT
1347 fUnion
.errorCode
= errorCode
;
1350 Padder() : fWidth(-2) {} // NOLINT
1352 bool isBogus() const {
1353 return fWidth
== -2;
1356 UBool
copyErrorTo(UErrorCode
&status
) const {
1358 status
= fUnion
.errorCode
;
1364 bool isValid() const {
1368 int32_t padAndApply(const impl::Modifier
&mod1
, const impl::Modifier
&mod2
,
1369 FormattedStringBuilder
&string
, int32_t leftIndex
, int32_t rightIndex
,
1370 UErrorCode
&status
) const;
1372 // To allow MacroProps/MicroProps to initialize empty instances:
1373 friend struct MacroProps
;
1374 friend struct MicroProps
;
1376 // To allow NumberFormatterImpl to access isBogus() and perform other operations:
1377 friend class impl::NumberFormatterImpl
;
1379 // To allow access to the skeleton generation code:
1380 friend class impl::GeneratorHelpers
;
1383 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
1385 struct U_I18N_API MacroProps
: public UMemory
{
1390 MeasureUnit unit
; // = NoUnit::base();
1393 MeasureUnit perUnit
; // = NoUnit::base();
1396 Precision precision
; // = Precision(); (bogus)
1399 UNumberFormatRoundingMode roundingMode
= UNUM_ROUND_HALFEVEN
;
1402 Grouper grouper
; // = Grouper(); (bogus)
1405 Padder padder
; // = Padder(); (bogus)
1408 IntegerWidth integerWidth
; // = IntegerWidth(); (bogus)
1411 SymbolsWrapper symbols
;
1413 // UNUM_XYZ_COUNT denotes null (bogus) values.
1416 UNumberUnitWidth unitWidth
= UNUM_UNIT_WIDTH_COUNT
;
1419 UNumberSignDisplay sign
= UNUM_SIGN_COUNT
;
1422 UNumberDecimalSeparatorDisplay decimal
= UNUM_DECIMAL_SEPARATOR_COUNT
;
1425 Scale scale
; // = Scale(); (benign value)
1428 const AffixPatternProvider
* affixProvider
= nullptr; // no ownership
1431 const PluralRules
* rules
= nullptr; // no ownership
1434 const CurrencySymbols
* currencySymbols
= nullptr; // no ownership
1437 int32_t threshold
= kInternalDefaultThreshold
;
1439 /** @internal Apple addition for <rdar://problem/39240173> */
1440 bool adjustDoublePrecision
= false;
1445 // NOTE: Uses default copy and move constructors.
1448 * Check all members for errors.
1451 bool copyErrorTo(UErrorCode
&status
) const {
1452 return notation
.copyErrorTo(status
) || precision
.copyErrorTo(status
) ||
1453 padder
.copyErrorTo(status
) || integerWidth
.copyErrorTo(status
) ||
1454 symbols
.copyErrorTo(status
) || scale
.copyErrorTo(status
);
1461 * An abstract base class for specifying settings related to number formatting. This class is implemented by
1462 * {@link UnlocalizedNumberFormatter} and {@link LocalizedNumberFormatter}. This class is not intended for
1463 * public subclassing.
1465 template<typename Derived
>
1466 class U_I18N_API NumberFormatterSettings
{
1469 * Specifies the notation style (simple, scientific, or compact) for rendering numbers.
1472 * <li>Simple notation: "12,300"
1473 * <li>Scientific notation: "1.23E4"
1474 * <li>Compact notation: "12K"
1478 * All notation styles will be properly localized with locale data, and all notation styles are compatible with
1479 * units, rounding precisions, and other number formatter settings.
1482 * Pass this method the return value of a {@link Notation} factory method. For example:
1485 * NumberFormatter::with().notation(Notation::compactShort())
1488 * The default is to use simple notation.
1491 * The notation strategy to use.
1492 * @return The fluent chain.
1496 Derived
notation(const Notation
¬ation
) const &;
1499 * Overload of notation() for use on an rvalue reference.
1502 * The notation strategy to use.
1503 * @return The fluent chain.
1507 Derived
notation(const Notation
¬ation
) &&;
1510 * Specifies the unit (unit of measure, currency, or percent) to associate with rendered numbers.
1513 * <li>Unit of measure: "12.3 meters"
1514 * <li>Currency: "$12.30"
1515 * <li>Percent: "12.3%"
1518 * All units will be properly localized with locale data, and all units are compatible with notation styles,
1519 * rounding precisions, and other number formatter settings.
1521 * Pass this method any instance of {@link MeasureUnit}. For units of measure:
1524 * NumberFormatter::with().unit(MeasureUnit::getMeter())
1530 * NumberFormatter::with().unit(CurrencyUnit(u"USD", status))
1536 * NumberFormatter::with().unit(NoUnit.percent())
1539 * See {@link #perUnit} for information on how to format strings like "5 meters per second".
1541 * The default is to render without units (equivalent to NoUnit.base()).
1544 * The unit to render.
1545 * @return The fluent chain.
1552 Derived
unit(const icu::MeasureUnit
&unit
) const &;
1555 * Overload of unit() for use on an rvalue reference.
1558 * The unit to render.
1559 * @return The fluent chain.
1563 Derived
unit(const icu::MeasureUnit
&unit
) &&;
1566 * Like unit(), but takes ownership of a pointer. Convenient for use with the MeasureFormat factory
1567 * methods that return pointers that need ownership.
1569 * Note: consider using the MeasureFormat factory methods that return by value.
1572 * The unit to render.
1573 * @return The fluent chain.
1578 Derived
adoptUnit(icu::MeasureUnit
*unit
) const &;
1581 * Overload of adoptUnit() for use on an rvalue reference.
1584 * The unit to render.
1585 * @return The fluent chain.
1589 Derived
adoptUnit(icu::MeasureUnit
*unit
) &&;
1592 * Sets a unit to be used in the denominator. For example, to format "3 m/s", pass METER to the unit and SECOND to
1595 * Pass this method any instance of {@link MeasureUnit}. Example:
1598 * NumberFormatter::with()
1599 * .unit(MeasureUnit::getMeter())
1600 * .perUnit(MeasureUnit::getSecond())
1603 * The default is not to display any unit in the denominator.
1605 * If a per-unit is specified without a primary unit via {@link #unit}, the behavior is undefined.
1608 * The unit to render in the denominator.
1609 * @return The fluent chain
1613 Derived
perUnit(const icu::MeasureUnit
&perUnit
) const &;
1616 * Overload of perUnit() for use on an rvalue reference.
1619 * The unit to render in the denominator.
1620 * @return The fluent chain.
1624 Derived
perUnit(const icu::MeasureUnit
&perUnit
) &&;
1627 * Like perUnit(), but takes ownership of a pointer. Convenient for use with the MeasureFormat factory
1628 * methods that return pointers that need ownership.
1630 * Note: consider using the MeasureFormat factory methods that return by value.
1633 * The unit to render in the denominator.
1634 * @return The fluent chain.
1639 Derived
adoptPerUnit(icu::MeasureUnit
*perUnit
) const &;
1642 * Overload of adoptPerUnit() for use on an rvalue reference.
1645 * The unit to render in the denominator.
1646 * @return The fluent chain.
1647 * @see #adoptPerUnit
1650 Derived
adoptPerUnit(icu::MeasureUnit
*perUnit
) &&;
1653 * Specifies the rounding precision to use when formatting numbers.
1656 * <li>Round to 3 decimal places: "3.142"
1657 * <li>Round to 3 significant figures: "3.14"
1658 * <li>Round to the closest nickel: "3.15"
1659 * <li>Do not perform rounding: "3.1415926..."
1663 * Pass this method the return value of one of the factory methods on {@link Precision}. For example:
1666 * NumberFormatter::with().precision(Precision::fixedFraction(2))
1670 * In most cases, the default rounding strategy is to round to 6 fraction places; i.e.,
1671 * <code>Precision.maxFraction(6)</code>. The exceptions are if compact notation is being used, then the compact
1672 * notation rounding strategy is used (see {@link Notation#compactShort} for details), or if the unit is a currency,
1673 * then standard currency rounding is used, which varies from currency to currency (see {@link Precision#currency} for
1677 * The rounding precision to use.
1678 * @return The fluent chain.
1682 Derived
precision(const Precision
& precision
) const &;
1685 * Overload of precision() for use on an rvalue reference.
1688 * The rounding precision to use.
1689 * @return The fluent chain.
1693 Derived
precision(const Precision
& precision
) &&;
1696 * Specifies how to determine the direction to round a number when it has more digits than fit in the
1697 * desired precision. When formatting 1.235:
1700 * <li>Ceiling rounding mode with integer precision: "2"
1701 * <li>Half-down rounding mode with 2 fixed fraction digits: "1.23"
1702 * <li>Half-up rounding mode with 2 fixed fraction digits: "1.24"
1705 * The default is HALF_EVEN. For more information on rounding mode, see the ICU userguide here:
1707 * http://userguide.icu-project.org/formatparse/numbers/rounding-modes
1709 * @param roundingMode The rounding mode to use.
1710 * @return The fluent chain.
1713 Derived
roundingMode(UNumberFormatRoundingMode roundingMode
) const &;
1716 * Overload of roundingMode() for use on an rvalue reference.
1718 * @param roundingMode The rounding mode to use.
1719 * @return The fluent chain.
1720 * @see #roundingMode
1723 Derived
roundingMode(UNumberFormatRoundingMode roundingMode
) &&;
1726 * Specifies the grouping strategy to use when formatting numbers.
1729 * <li>Default grouping: "12,300" and "1,230"
1730 * <li>Grouping with at least 2 digits: "12,300" and "1230"
1731 * <li>No grouping: "12300" and "1230"
1735 * The exact grouping widths will be chosen based on the locale.
1738 * Pass this method an element from the {@link UNumberGroupingStrategy} enum. For example:
1741 * NumberFormatter::with().grouping(UNUM_GROUPING_MIN2)
1744 * The default is to perform grouping according to locale data; most locales, but not all locales,
1745 * enable it by default.
1748 * The grouping strategy to use.
1749 * @return The fluent chain.
1752 Derived
grouping(UNumberGroupingStrategy strategy
) const &;
1755 * Overload of grouping() for use on an rvalue reference.
1758 * The grouping strategy to use.
1759 * @return The fluent chain.
1763 Derived
grouping(UNumberGroupingStrategy strategy
) &&;
1766 * Specifies the minimum and maximum number of digits to render before the decimal mark.
1769 * <li>Zero minimum integer digits: ".08"
1770 * <li>One minimum integer digit: "0.08"
1771 * <li>Two minimum integer digits: "00.08"
1775 * Pass this method the return value of {@link IntegerWidth#zeroFillTo}. For example:
1778 * NumberFormatter::with().integerWidth(IntegerWidth::zeroFillTo(2))
1781 * The default is to have one minimum integer digit.
1784 * The integer width to use.
1785 * @return The fluent chain.
1789 Derived
integerWidth(const IntegerWidth
&style
) const &;
1792 * Overload of integerWidth() for use on an rvalue reference.
1795 * The integer width to use.
1796 * @return The fluent chain.
1797 * @see #integerWidth
1800 Derived
integerWidth(const IntegerWidth
&style
) &&;
1803 * Specifies the symbols (decimal separator, grouping separator, percent sign, numerals, etc.) to use when rendering
1807 * <li><em>en_US</em> symbols: "12,345.67"
1808 * <li><em>fr_FR</em> symbols: "12 345,67"
1809 * <li><em>de_CH</em> symbols: "12’345.67"
1810 * <li><em>my_MY</em> symbols: "၁၂,၃၄၅.၆၇"
1814 * Pass this method an instance of {@link DecimalFormatSymbols}. For example:
1817 * NumberFormatter::with().symbols(DecimalFormatSymbols(Locale("de_CH"), status))
1821 * <strong>Note:</strong> DecimalFormatSymbols automatically chooses the best numbering system based on the locale.
1822 * In the examples above, the first three are using the Latin numbering system, and the fourth is using the Myanmar
1826 * <strong>Note:</strong> The instance of DecimalFormatSymbols will be copied: changes made to the symbols object
1827 * after passing it into the fluent chain will not be seen.
1830 * <strong>Note:</strong> Calling this method will override any previously specified DecimalFormatSymbols
1831 * or NumberingSystem.
1834 * The default is to choose the symbols based on the locale specified in the fluent chain.
1837 * The DecimalFormatSymbols to use.
1838 * @return The fluent chain.
1839 * @see DecimalFormatSymbols
1842 Derived
symbols(const DecimalFormatSymbols
&symbols
) const &;
1845 * Overload of symbols() for use on an rvalue reference.
1848 * The DecimalFormatSymbols to use.
1849 * @return The fluent chain.
1853 Derived
symbols(const DecimalFormatSymbols
&symbols
) &&;
1856 * Specifies that the given numbering system should be used when fetching symbols.
1859 * <li>Latin numbering system: "12,345"
1860 * <li>Myanmar numbering system: "၁၂,၃၄၅"
1861 * <li>Math Sans Bold numbering system: "𝟭𝟮,𝟯𝟰𝟱"
1865 * Pass this method an instance of {@link NumberingSystem}. For example, to force the locale to always use the Latin
1866 * alphabet numbering system (ASCII digits):
1869 * NumberFormatter::with().adoptSymbols(NumberingSystem::createInstanceByName("latn", status))
1873 * <strong>Note:</strong> Calling this method will override any previously specified DecimalFormatSymbols
1874 * or NumberingSystem.
1877 * The default is to choose the best numbering system for the locale.
1880 * This method takes ownership of a pointer in order to work nicely with the NumberingSystem factory methods.
1883 * The NumberingSystem to use.
1884 * @return The fluent chain.
1885 * @see NumberingSystem
1888 Derived
adoptSymbols(NumberingSystem
*symbols
) const &;
1891 * Overload of adoptSymbols() for use on an rvalue reference.
1894 * The NumberingSystem to use.
1895 * @return The fluent chain.
1896 * @see #adoptSymbols
1899 Derived
adoptSymbols(NumberingSystem
*symbols
) &&;
1902 * Sets the width of the unit (measure unit or currency). Most common values:
1905 * <li>Short: "$12.00", "12 m"
1906 * <li>ISO Code: "USD 12.00"
1907 * <li>Full name: "12.00 US dollars", "12 meters"
1911 * Pass an element from the {@link UNumberUnitWidth} enum to this setter. For example:
1914 * NumberFormatter::with().unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME)
1918 * The default is the SHORT width.
1921 * The width to use when rendering numbers.
1922 * @return The fluent chain
1923 * @see UNumberUnitWidth
1926 Derived
unitWidth(UNumberUnitWidth width
) const &;
1929 * Overload of unitWidth() for use on an rvalue reference.
1932 * The width to use when rendering numbers.
1933 * @return The fluent chain.
1937 Derived
unitWidth(UNumberUnitWidth width
) &&;
1940 * Sets the plus/minus sign display strategy. Most common values:
1943 * <li>Auto: "123", "-123"
1944 * <li>Always: "+123", "-123"
1945 * <li>Accounting: "$123", "($123)"
1949 * Pass an element from the {@link UNumberSignDisplay} enum to this setter. For example:
1952 * NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS)
1956 * The default is AUTO sign display.
1959 * The sign display strategy to use when rendering numbers.
1960 * @return The fluent chain
1961 * @see UNumberSignDisplay
1964 Derived
sign(UNumberSignDisplay style
) const &;
1967 * Overload of sign() for use on an rvalue reference.
1970 * The sign display strategy to use when rendering numbers.
1971 * @return The fluent chain.
1975 Derived
sign(UNumberSignDisplay style
) &&;
1978 * Sets the decimal separator display strategy. This affects integer numbers with no fraction part. Most common
1987 * Pass an element from the {@link UNumberDecimalSeparatorDisplay} enum to this setter. For example:
1990 * NumberFormatter::with().decimal(UNumberDecimalSeparatorDisplay::UNUM_DECIMAL_SEPARATOR_ALWAYS)
1994 * The default is AUTO decimal separator display.
1997 * The decimal separator display strategy to use when rendering numbers.
1998 * @return The fluent chain
1999 * @see UNumberDecimalSeparatorDisplay
2002 Derived
decimal(UNumberDecimalSeparatorDisplay style
) const &;
2005 * Overload of decimal() for use on an rvalue reference.
2008 * The decimal separator display strategy to use when rendering numbers.
2009 * @return The fluent chain.
2013 Derived
decimal(UNumberDecimalSeparatorDisplay style
) &&;
2016 * Sets a scale (multiplier) to be used to scale the number by an arbitrary amount before formatting.
2017 * Most common values:
2020 * <li>Multiply by 100: useful for percentages.
2021 * <li>Multiply by an arbitrary value: useful for unit conversions.
2025 * Pass an element from a {@link Scale} factory method to this setter. For example:
2028 * NumberFormatter::with().scale(Scale::powerOfTen(2))
2032 * The default is to not apply any multiplier.
2035 * The scale to apply when rendering numbers.
2036 * @return The fluent chain
2039 Derived
scale(const Scale
&scale
) const &;
2042 * Overload of scale() for use on an rvalue reference.
2045 * The scale to apply when rendering numbers.
2046 * @return The fluent chain.
2050 Derived
scale(const Scale
&scale
) &&;
2052 #ifndef U_HIDE_INTERNAL_API
2055 * Set the padding strategy. May be added in the future; see #13338.
2057 * @internal ICU 60: This API is ICU internal only.
2059 Derived
padding(const impl::Padder
&padder
) const &;
2062 Derived
padding(const impl::Padder
&padder
) &&;
2065 * Internal fluent setter to support a custom regulation threshold. A threshold of 1 causes the data structures to
2066 * be built right away. A threshold of 0 prevents the data structures from being built.
2068 * @internal ICU 60: This API is ICU internal only.
2070 Derived
threshold(int32_t threshold
) const &;
2073 Derived
threshold(int32_t threshold
) &&;
2076 * Internal fluent setter to overwrite the entire macros object.
2078 * @internal ICU 60: This API is ICU internal only.
2080 Derived
macros(const impl::MacroProps
& macros
) const &;
2083 Derived
macros(const impl::MacroProps
& macros
) &&;
2086 Derived
macros(impl::MacroProps
&& macros
) const &;
2089 Derived
macros(impl::MacroProps
&& macros
) &&;
2091 #endif /* U_HIDE_INTERNAL_API */
2094 * Creates a skeleton string representation of this number formatter. A skeleton string is a
2095 * locale-agnostic serialized form of a number formatter.
2097 * Not all options are capable of being represented in the skeleton string; for example, a
2098 * DecimalFormatSymbols object. If any such option is encountered, the error code is set to
2099 * U_UNSUPPORTED_ERROR.
2101 * The returned skeleton is in normalized form, such that two number formatters with equivalent
2102 * behavior should produce the same skeleton.
2104 * @return A number skeleton string with behavior corresponding to this number formatter.
2107 UnicodeString
toSkeleton(UErrorCode
& status
) const;
2109 #ifndef U_HIDE_DRAFT_API
2111 * Returns the current (Un)LocalizedNumberFormatter as a LocalPointer
2112 * wrapping a heap-allocated copy of the current object.
2114 * This is equivalent to new-ing the move constructor with a value object
2117 * @return A wrapped (Un)LocalizedNumberFormatter pointer, or a wrapped
2118 * nullptr on failure.
2121 LocalPointer
<Derived
> clone() const &;
2124 * Overload of clone for use on an rvalue reference.
2126 * @return A wrapped (Un)LocalizedNumberFormatter pointer, or a wrapped
2127 * nullptr on failure.
2130 LocalPointer
<Derived
> clone() &&;
2131 #endif /* U_HIDE_DRAFT_API */
2134 * Sets the UErrorCode if an error occurred in the fluent chain.
2135 * Preserves older error codes in the outErrorCode.
2136 * @return TRUE if U_FAILURE(outErrorCode)
2139 UBool
copyErrorTo(UErrorCode
&outErrorCode
) const {
2140 if (U_FAILURE(outErrorCode
)) {
2141 // Do not overwrite the older error code
2144 fMacros
.copyErrorTo(outErrorCode
);
2145 return U_FAILURE(outErrorCode
);
2148 // NOTE: Uses default copy and move constructors.
2151 impl::MacroProps fMacros
;
2153 // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
2154 NumberFormatterSettings() = default;
2156 friend class LocalizedNumberFormatter
;
2157 friend class UnlocalizedNumberFormatter
;
2159 // Give NumberRangeFormatter access to the MacroProps
2160 friend void impl::touchRangeLocales(impl::RangeMacroProps
& macros
);
2161 friend class impl::NumberRangeFormatterImpl
;
2165 * A NumberFormatter that does not yet have a locale. In order to format numbers, a locale must be specified.
2167 * Instances of this class are immutable and thread-safe.
2169 * @see NumberFormatter
2172 class U_I18N_API UnlocalizedNumberFormatter
2173 : public NumberFormatterSettings
<UnlocalizedNumberFormatter
>, public UMemory
{
2177 * Associate the given locale with the number formatter. The locale is used for picking the appropriate symbols,
2178 * formats, and other data for number display.
2181 * The locale to use when loading data for number formatting.
2182 * @return The fluent chain.
2185 LocalizedNumberFormatter
locale(const icu::Locale
&locale
) const &;
2188 * Overload of locale() for use on an rvalue reference.
2191 * The locale to use when loading data for number formatting.
2192 * @return The fluent chain.
2196 LocalizedNumberFormatter
locale(const icu::Locale
&locale
) &&;
2199 * Default constructor: puts the formatter into a valid but undefined state.
2203 UnlocalizedNumberFormatter() = default;
2206 * Returns a copy of this UnlocalizedNumberFormatter.
2209 UnlocalizedNumberFormatter(const UnlocalizedNumberFormatter
&other
);
2213 * The source UnlocalizedNumberFormatter will be left in a valid but undefined state.
2216 UnlocalizedNumberFormatter(UnlocalizedNumberFormatter
&& src
) U_NOEXCEPT
;
2219 * Copy assignment operator.
2222 UnlocalizedNumberFormatter
& operator=(const UnlocalizedNumberFormatter
& other
);
2225 * Move assignment operator:
2226 * The source UnlocalizedNumberFormatter will be left in a valid but undefined state.
2229 UnlocalizedNumberFormatter
& operator=(UnlocalizedNumberFormatter
&& src
) U_NOEXCEPT
;
2232 explicit UnlocalizedNumberFormatter(const NumberFormatterSettings
<UnlocalizedNumberFormatter
>& other
);
2234 explicit UnlocalizedNumberFormatter(
2235 NumberFormatterSettings
<UnlocalizedNumberFormatter
>&& src
) U_NOEXCEPT
;
2237 // To give the fluent setters access to this class's constructor:
2238 friend class NumberFormatterSettings
<UnlocalizedNumberFormatter
>;
2240 // To give NumberFormatter::with() access to this class's constructor:
2241 friend class NumberFormatter
;
2245 * A NumberFormatter that has a locale associated with it; this means .format() methods are available.
2247 * Instances of this class are immutable and thread-safe.
2249 * @see NumberFormatter
2252 class U_I18N_API LocalizedNumberFormatter
2253 : public NumberFormatterSettings
<LocalizedNumberFormatter
>, public UMemory
{
2256 * Format the given integer number to a string using the settings specified in the NumberFormatter fluent
2260 * The number to format.
2262 * Set to an ErrorCode if one occurred in the setter chain or during formatting.
2263 * @return A FormattedNumber object; call .toString() to get the string.
2266 FormattedNumber
formatInt(int64_t value
, UErrorCode
&status
) const;
2269 * Format the given float or double to a string using the settings specified in the NumberFormatter fluent setting
2273 * The number to format.
2275 * Set to an ErrorCode if one occurred in the setter chain or during formatting.
2276 * @return A FormattedNumber object; call .toString() to get the string.
2279 FormattedNumber
formatDouble(double value
, UErrorCode
&status
) const;
2282 * Format the given decimal number to a string using the settings
2283 * specified in the NumberFormatter fluent setting chain.
2284 * The syntax of the unformatted number is a "numeric string"
2285 * as defined in the Decimal Arithmetic Specification, available at
2286 * http://speleotrove.com/decimal
2289 * The number to format.
2291 * Set to an ErrorCode if one occurred in the setter chain or during formatting.
2292 * @return A FormattedNumber object; call .toString() to get the string.
2295 FormattedNumber
formatDecimal(StringPiece value
, UErrorCode
& status
) const;
2297 #ifndef U_HIDE_INTERNAL_API
2300 * Set whether DecimalFormatSymbols copy is deep (clone)
2301 * or shallow (pointer copy). Apple <rdar://problem/49955427>
2304 void setDFSShallowCopy(UBool shallow
);
2306 /** Internal method.
2309 FormattedNumber
formatDecimalQuantity(const impl::DecimalQuantity
& dq
, UErrorCode
& status
) const;
2311 /** Internal method for DecimalFormat compatibility.
2314 void getAffixImpl(bool isPrefix
, bool isNegative
, UnicodeString
& result
, UErrorCode
& status
) const;
2317 * Internal method for testing.
2320 const impl::NumberFormatterImpl
* getCompiled() const;
2323 * Internal method for testing.
2326 int32_t getCallCount() const;
2328 #endif /* U_HIDE_INTERNAL_API */
2331 * Creates a representation of this LocalizedNumberFormat as an icu::Format, enabling the use
2332 * of this number formatter with APIs that need an object of that type, such as MessageFormat.
2334 * This API is not intended to be used other than for enabling API compatibility. The formatDouble,
2335 * formatInt, and formatDecimal methods should normally be used when formatting numbers, not the Format
2336 * object returned by this method.
2338 * The caller owns the returned object and must delete it when finished.
2340 * @return A Format wrapping this LocalizedNumberFormatter.
2343 Format
* toFormat(UErrorCode
& status
) const;
2346 * Default constructor: puts the formatter into a valid but undefined state.
2350 LocalizedNumberFormatter() = default;
2353 * Returns a copy of this LocalizedNumberFormatter.
2356 LocalizedNumberFormatter(const LocalizedNumberFormatter
&other
);
2360 * The source LocalizedNumberFormatter will be left in a valid but undefined state.
2363 LocalizedNumberFormatter(LocalizedNumberFormatter
&& src
) U_NOEXCEPT
;
2366 * Copy assignment operator.
2369 LocalizedNumberFormatter
& operator=(const LocalizedNumberFormatter
& other
);
2372 * Move assignment operator:
2373 * The source LocalizedNumberFormatter will be left in a valid but undefined state.
2376 LocalizedNumberFormatter
& operator=(LocalizedNumberFormatter
&& src
) U_NOEXCEPT
;
2378 #ifndef U_HIDE_INTERNAL_API
2381 * This is the core entrypoint to the number formatting pipeline. It performs self-regulation: a static code path
2382 * for the first few calls, and compiling a more efficient data structure if called repeatedly.
2385 * This function is very hot, being called in every call to the number formatting pipeline.
2388 * The results object. This method will mutate it to save the results.
2392 void formatImpl(impl::UFormattedNumberData
*results
, UErrorCode
&status
) const;
2394 #endif /* U_HIDE_INTERNAL_API */
2397 * Destruct this LocalizedNumberFormatter, cleaning up any memory it might own.
2400 ~LocalizedNumberFormatter();
2403 // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal
2404 // header, and LocalPointer needs the full class definition in order to delete the instance.
2405 const impl::NumberFormatterImpl
* fCompiled
{nullptr};
2406 char fUnsafeCallCount
[8] {}; // internally cast to u_atomic_int32_t
2408 explicit LocalizedNumberFormatter(const NumberFormatterSettings
<LocalizedNumberFormatter
>& other
);
2410 explicit LocalizedNumberFormatter(NumberFormatterSettings
<LocalizedNumberFormatter
>&& src
) U_NOEXCEPT
;
2412 LocalizedNumberFormatter(const impl::MacroProps
¯os
, const Locale
&locale
);
2414 LocalizedNumberFormatter(impl::MacroProps
&¯os
, const Locale
&locale
);
2418 void lnfMoveHelper(LocalizedNumberFormatter
&& src
);
2421 * @return true if the compiled formatter is available.
2423 bool computeCompiled(UErrorCode
& status
) const;
2425 // To give the fluent setters access to this class's constructor:
2426 friend class NumberFormatterSettings
<UnlocalizedNumberFormatter
>;
2427 friend class NumberFormatterSettings
<LocalizedNumberFormatter
>;
2429 // To give UnlocalizedNumberFormatter::locale() access to this class's constructor:
2430 friend class UnlocalizedNumberFormatter
;
2434 * The result of a number formatting operation. This class allows the result to be exported in several data types,
2435 * including a UnicodeString and a FieldPositionIterator.
2437 * Instances of this class are immutable and thread-safe.
2441 class U_I18N_API FormattedNumber
: public UMemory
, public FormattedValue
{
2444 // Default constructor cannot have #ifndef U_HIDE_DRAFT_API
2445 #ifndef U_FORCE_HIDE_DRAFT_API
2447 * Default constructor; makes an empty FormattedNumber.
2451 : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR
) {}
2452 #endif // U_FORCE_HIDE_DRAFT_API
2455 * Move constructor: Leaves the source FormattedNumber in an undefined state.
2458 FormattedNumber(FormattedNumber
&& src
) U_NOEXCEPT
;
2461 * Destruct an instance of FormattedNumber.
2464 virtual ~FormattedNumber() U_OVERRIDE
;
2466 /** Copying not supported; use move constructor instead. */
2467 FormattedNumber(const FormattedNumber
&) = delete;
2469 /** Copying not supported; use move assignment instead. */
2470 FormattedNumber
& operator=(const FormattedNumber
&) = delete;
2473 * Move assignment: Leaves the source FormattedNumber in an undefined state.
2476 FormattedNumber
& operator=(FormattedNumber
&& src
) U_NOEXCEPT
;
2478 // Copybrief: this method is older than the parent method
2480 * @copybrief FormattedValue::toString()
2482 * For more information, see FormattedValue::toString()
2486 UnicodeString
toString(UErrorCode
& status
) const U_OVERRIDE
;
2488 // Copydoc: this method is new in ICU 64
2489 /** @copydoc FormattedValue::toTempString() */
2490 UnicodeString
toTempString(UErrorCode
& status
) const U_OVERRIDE
;
2492 // Copybrief: this method is older than the parent method
2494 * @copybrief FormattedValue::appendTo()
2496 * For more information, see FormattedValue::appendTo()
2500 Appendable
&appendTo(Appendable
& appendable
, UErrorCode
& status
) const U_OVERRIDE
;
2502 // Copydoc: this method is new in ICU 64
2503 /** @copydoc FormattedValue::nextPosition() */
2504 UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const U_OVERRIDE
;
2506 #ifndef U_HIDE_DRAFT_API
2508 * Determines the start (inclusive) and end (exclusive) indices of the next occurrence of the given
2509 * <em>field</em> in the output string. This allows you to determine the locations of, for example,
2510 * the integer part, fraction part, or symbols.
2512 * This is a simpler but less powerful alternative to {@link #nextPosition}.
2514 * If a field occurs just once, calling this method will find that occurrence and return it. If a
2515 * field occurs multiple times, this method may be called repeatedly with the following pattern:
2518 * FieldPosition fpos(UNUM_GROUPING_SEPARATOR_FIELD);
2519 * while (formattedNumber.nextFieldPosition(fpos, status)) {
2520 * // do something with fpos.
2524 * This method is useful if you know which field to query. If you want all available field position
2525 * information, use {@link #nextPosition} or {@link #getAllFieldPositions}.
2527 * @param fieldPosition
2528 * Input+output variable. On input, the "field" property determines which field to look
2529 * up, and the "beginIndex" and "endIndex" properties determine where to begin the search.
2530 * On output, the "beginIndex" is set to the beginning of the first occurrence of the
2531 * field with either begin or end indices after the input indices; "endIndex" is set to
2532 * the end of that occurrence of the field (exclusive index). If a field position is not
2533 * found, the method returns FALSE and the FieldPosition may or may not be changed.
2535 * Set if an error occurs while populating the FieldPosition.
2536 * @return TRUE if a new occurrence of the field was found; FALSE otherwise.
2538 * @see UNumberFormatFields
2540 UBool
nextFieldPosition(FieldPosition
& fieldPosition
, UErrorCode
& status
) const;
2543 * Export the formatted number to a FieldPositionIterator. This allows you to determine which characters in
2544 * the output string correspond to which <em>fields</em>, such as the integer part, fraction part, and sign.
2546 * This is an alternative to the more powerful #nextPosition() API.
2548 * If information on only one field is needed, use #nextPosition() or #nextFieldPosition() instead.
2551 * The FieldPositionIterator to populate with all of the fields present in the formatted number.
2553 * Set if an error occurs while populating the FieldPositionIterator.
2555 * @see UNumberFormatFields
2557 void getAllFieldPositions(FieldPositionIterator
&iterator
, UErrorCode
&status
) const;
2558 #endif /* U_HIDE_DRAFT_API */
2560 #ifndef U_HIDE_DRAFT_API
2562 * Export the formatted number as a "numeric string" conforming to the
2563 * syntax defined in the Decimal Arithmetic Specification, available at
2564 * http://speleotrove.com/decimal
2566 * This endpoint is useful for obtaining the exact number being printed
2567 * after scaling and rounding have been applied by the number formatter.
2569 * Example call site:
2571 * auto decimalNumber = fn.toDecimalNumber<std::string>(status);
2573 * @tparam StringClass A string class compatible with StringByteSink;
2574 * for example, std::string.
2575 * @param status Set if an error occurs.
2576 * @return A StringClass containing the numeric string.
2579 template<typename StringClass
>
2580 inline StringClass
toDecimalNumber(UErrorCode
& status
) const;
2581 #endif // U_HIDE_DRAFT_API
2583 #ifndef U_HIDE_INTERNAL_API
2586 * Gets the raw DecimalQuantity for plural rule selection.
2589 void getDecimalQuantity(impl::DecimalQuantity
& output
, UErrorCode
& status
) const;
2592 * Populates the mutable builder type FieldPositionIteratorHandler.
2595 void getAllFieldPositionsImpl(FieldPositionIteratorHandler
& fpih
, UErrorCode
& status
) const;
2597 #endif /* U_HIDE_INTERNAL_API */
2600 // Can't use LocalPointer because UFormattedNumberData is forward-declared
2601 const impl::UFormattedNumberData
*fData
;
2603 // Error code for the terminal methods
2604 UErrorCode fErrorCode
;
2607 * Internal constructor from data type. Adopts the data pointer.
2610 explicit FormattedNumber(impl::UFormattedNumberData
*results
)
2611 : fData(results
), fErrorCode(U_ZERO_ERROR
) {}
2613 explicit FormattedNumber(UErrorCode errorCode
)
2614 : fData(nullptr), fErrorCode(errorCode
) {}
2616 // TODO(ICU-20775): Propose this as API.
2617 void toDecimalNumber(ByteSink
& sink
, UErrorCode
& status
) const;
2619 // To give LocalizedNumberFormatter format methods access to this class's constructor:
2620 friend class LocalizedNumberFormatter
;
2622 // To give C API access to internals
2623 friend struct impl::UFormattedNumberImpl
;
2626 #ifndef U_HIDE_DRAFT_API
2627 // Note: This is draft ICU 65
2628 template<typename StringClass
>
2629 StringClass
FormattedNumber::toDecimalNumber(UErrorCode
& status
) const {
2631 StringByteSink
<StringClass
> sink(&result
);
2632 toDecimalNumber(sink
, status
);
2635 #endif // U_HIDE_DRAFT_API
2638 * See the main description in numberformatter.h for documentation and examples.
2642 class U_I18N_API NumberFormatter final
{
2645 * Call this method at the beginning of a NumberFormatter fluent chain in which the locale is not currently known at
2648 * @return An {@link UnlocalizedNumberFormatter}, to be used for chaining.
2651 static UnlocalizedNumberFormatter
with();
2654 * Call this method at the beginning of a NumberFormatter fluent chain in which the locale is known at the call
2658 * The locale from which to load formats and symbols for number formatting.
2659 * @return A {@link LocalizedNumberFormatter}, to be used for chaining.
2662 static LocalizedNumberFormatter
withLocale(const Locale
&locale
);
2665 * Call this method at the beginning of a NumberFormatter fluent chain to create an instance based
2666 * on a given number skeleton string.
2668 * It is possible for an error to occur while parsing. See the overload of this method if you are
2669 * interested in the location of a possible parse error.
2672 * The skeleton string off of which to base this NumberFormatter.
2674 * Set to U_NUMBER_SKELETON_SYNTAX_ERROR if the skeleton was invalid.
2675 * @return An UnlocalizedNumberFormatter, to be used for chaining.
2678 static UnlocalizedNumberFormatter
forSkeleton(const UnicodeString
& skeleton
, UErrorCode
& status
);
2680 #ifndef U_HIDE_DRAFT_API
2682 * Call this method at the beginning of a NumberFormatter fluent chain to create an instance based
2683 * on a given number skeleton string.
2685 * If an error occurs while parsing the skeleton string, the offset into the skeleton string at
2686 * which the error occurred will be saved into the UParseError, if provided.
2689 * The skeleton string off of which to base this NumberFormatter.
2691 * A parse error struct populated if an error occurs when parsing.
2692 * If no error occurs, perror.offset will be set to -1.
2694 * Set to U_NUMBER_SKELETON_SYNTAX_ERROR if the skeleton was invalid.
2695 * @return An UnlocalizedNumberFormatter, to be used for chaining.
2698 static UnlocalizedNumberFormatter
forSkeleton(const UnicodeString
& skeleton
,
2699 UParseError
& perror
, UErrorCode
& status
);
2703 * Use factory methods instead of the constructor to create a NumberFormatter.
2705 NumberFormatter() = delete;
2708 } // namespace number
2711 #endif /* #if !UCONFIG_NO_FORMATTING */
2713 #endif /* U_SHOW_CPLUSPLUS_API */
2715 #endif // __NUMBERFORMATTER_H__