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" 17 #include "unicode/displayoptions.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" 26 #include "unicode/unumberformatter.h" 27 #include "unicode/uobject.h" 28 #include "unicode/unumberoptions.h" 29 #include "unicode/formattednumber.h" 30 31 /** 32 * \file 33 * \brief C++ API: All-in-one formatter for localized numbers, currencies, and units. 34 * 35 * For a full list of options, see icu::number::NumberFormatterSettings. 36 * 37 * <pre> 38 * // Most basic usage: 39 * NumberFormatter::withLocale(...).format(123).toString(); // 1,234 in en-US 40 * 41 * // Custom notation, unit, and rounding precision: 42 * NumberFormatter::with() 43 * .notation(Notation::compactShort()) 44 * .unit(CurrencyUnit("EUR", status)) 45 * .precision(Precision::maxDigits(2)) 46 * .locale(...) 47 * .format(1234) 48 * .toString(); // €1.2K in en-US 49 * 50 * // Create a formatter in a singleton by value for use later: 51 * static const LocalizedNumberFormatter formatter = NumberFormatter::withLocale(...) 52 * .unit(NoUnit::percent()) 53 * .precision(Precision::fixedFraction(3)); 54 * formatter.format(5.9831).toString(); // 5.983% in en-US 55 * 56 * // Create a "template" in a singleton unique_ptr but without setting a locale until the call site: 57 * std::unique_ptr<UnlocalizedNumberFormatter> template = NumberFormatter::with() 58 * .sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS) 59 * .unit(MeasureUnit::getMeter()) 60 * .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME) 61 * .clone(); 62 * template->locale(...).format(1234).toString(); // +1,234 meters in en-US 63 * </pre> 64 * 65 * <p> 66 * This API offers more features than DecimalFormat and is geared toward new users of ICU. 67 * 68 * <p> 69 * NumberFormatter instances (i.e., LocalizedNumberFormatter and UnlocalizedNumberFormatter) 70 * are immutable and thread safe. This means that invoking a configuration method has no 71 * effect on the receiving instance; you must store and use the new number formatter instance it returns instead. 72 * 73 * <pre> 74 * UnlocalizedNumberFormatter formatter = UnlocalizedNumberFormatter::with().notation(Notation::scientific()); 75 * formatter.precision(Precision.maxFraction(2)); // does nothing! 76 * formatter.locale(Locale.getEnglish()).format(9.8765).toString(); // prints "9.8765E0", not "9.88E0" 77 * </pre> 78 * 79 * <p> 80 * This API is based on the <em>fluent</em> design pattern popularized by libraries such as Google's Guava. For 81 * extensive details on the design of this API, read <a href="https://goo.gl/szi5VB">the design doc</a>. 82 * 83 * @author Shane Carr 84 */ 85 86 U_NAMESPACE_BEGIN 87 88 // Forward declarations: 89 class IFixedDecimal; 90 class FieldPositionIteratorHandler; 91 class FormattedStringBuilder; 92 93 namespace numparse { 94 namespace impl { 95 96 // Forward declarations: 97 class NumberParserImpl; 98 class MultiplierParseHandler; 99 100 } 101 } 102 103 namespace units { 104 105 // Forward declarations: 106 class UnitsRouter; 107 108 } // namespace units 109 110 namespace number { // icu::number 111 112 // Forward declarations: 113 class UnlocalizedNumberFormatter; 114 class LocalizedNumberFormatter; 115 class SimpleNumberFormatter; 116 class FormattedNumber; 117 class Notation; 118 class ScientificNotation; 119 class Precision; 120 class FractionPrecision; 121 class CurrencyPrecision; 122 class IncrementPrecision; 123 class IntegerWidth; 124 125 namespace impl { 126 127 // can't be #ifndef U_HIDE_INTERNAL_API; referenced throughout this file in public classes 128 /** 129 * Datatype for minimum/maximum fraction digits. Must be able to hold kMaxIntFracSig. 130 * 131 * @internal 132 */ 133 typedef int16_t digits_t; 134 135 // can't be #ifndef U_HIDE_INTERNAL_API; needed for struct initialization 136 /** 137 * Use a default threshold of 3. This means that the third time .format() is called, the data structures get built 138 * using the "safe" code path. The first two calls to .format() will trigger the unsafe code path. 139 * 140 * @internal 141 */ 142 static constexpr int32_t kInternalDefaultThreshold = 3; 143 144 // Forward declarations: 145 class Padder; 146 struct MacroProps; 147 struct MicroProps; 148 class DecimalQuantity; 149 class UFormattedNumberData; 150 class NumberFormatterImpl; 151 struct ParsedPatternInfo; 152 class ScientificModifier; 153 class MultiplierProducer; 154 class RoundingImpl; 155 class ScientificHandler; 156 class Modifier; 157 class AffixPatternProvider; 158 class NumberPropertyMapper; 159 struct DecimalFormatProperties; 160 class MultiplierFormatHandler; 161 class CurrencySymbols; 162 class GeneratorHelpers; 163 class DecNum; 164 class NumberRangeFormatterImpl; 165 struct RangeMacroProps; 166 struct UFormattedNumberImpl; 167 class MutablePatternModifier; 168 class ImmutablePatternModifier; 169 struct DecimalFormatWarehouse; 170 struct SimpleMicroProps; 171 class AdoptingSignumModifierStore; 172 173 /** 174 * Used for NumberRangeFormatter and implemented in numrange_fluent.cpp. 175 * Declared here so it can be friended. 176 * 177 * @internal 178 */ 179 void touchRangeLocales(impl::RangeMacroProps& macros); 180 181 } // namespace impl 182 183 /** 184 * Extra name reserved in case it is needed in the future. 185 * 186 * @stable ICU 63 187 */ 188 typedef Notation CompactNotation; 189 190 /** 191 * Extra name reserved in case it is needed in the future. 192 * 193 * @stable ICU 63 194 */ 195 typedef Notation SimpleNotation; 196 197 /** 198 * A class that defines the notation style to be used when formatting numbers in NumberFormatter. 199 * 200 * @stable ICU 60 201 */ 202 class U_I18N_API Notation : public UMemory { 203 public: 204 /** 205 * Print the number using scientific notation (also known as scientific form, standard index form, or standard form 206 * in the UK). The format for scientific notation varies by locale; for example, many Western locales display the 207 * number in the form "#E0", where the number is displayed with one digit before the decimal separator, zero or more 208 * digits after the decimal separator, and the corresponding power of 10 displayed after the "E". 209 * 210 * <p> 211 * Example outputs in <em>en-US</em> when printing 8.765E4 through 8.765E-3: 212 * 213 * <pre> 214 * 8.765E4 215 * 8.765E3 216 * 8.765E2 217 * 8.765E1 218 * 8.765E0 219 * 8.765E-1 220 * 8.765E-2 221 * 8.765E-3 222 * 0E0 223 * </pre> 224 * 225 * @return A ScientificNotation for chaining or passing to the NumberFormatter notation() setter. 226 * @stable ICU 60 227 */ 228 static ScientificNotation scientific(); 229 230 /** 231 * Print the number using engineering notation, a variant of scientific notation in which the exponent must be 232 * divisible by 3. 233 * 234 * <p> 235 * Example outputs in <em>en-US</em> when printing 8.765E4 through 8.765E-3: 236 * 237 * <pre> 238 * 87.65E3 239 * 8.765E3 240 * 876.5E0 241 * 87.65E0 242 * 8.765E0 243 * 876.5E-3 244 * 87.65E-3 245 * 8.765E-3 246 * 0E0 247 * </pre> 248 * 249 * @return A ScientificNotation for chaining or passing to the NumberFormatter notation() setter. 250 * @stable ICU 60 251 */ 252 static ScientificNotation engineering(); 253 254 /** 255 * Print the number using short-form compact notation. 256 * 257 * <p> 258 * <em>Compact notation</em>, defined in Unicode Technical Standard #35 Part 3 Section 2.4.1, prints numbers with 259 * localized prefixes or suffixes corresponding to different powers of ten. Compact notation is similar to 260 * engineering notation in how it scales numbers. 261 * 262 * <p> 263 * Compact notation is ideal for displaying large numbers (over ~1000) to humans while at the same time minimizing 264 * screen real estate. 265 * 266 * <p> 267 * In short form, the powers of ten are abbreviated. In <em>en-US</em>, the abbreviations are "K" for thousands, "M" 268 * for millions, "B" for billions, and "T" for trillions. Example outputs in <em>en-US</em> when printing 8.765E7 269 * through 8.765E0: 270 * 271 * <pre> 272 * 88M 273 * 8.8M 274 * 876K 275 * 88K 276 * 8.8K 277 * 876 278 * 88 279 * 8.8 280 * </pre> 281 * 282 * <p> 283 * When compact notation is specified without an explicit rounding precision, numbers are rounded off to the closest 284 * integer after scaling the number by the corresponding power of 10, but with a digit shown after the decimal 285 * separator if there is only one digit before the decimal separator. The default compact notation rounding precision 286 * is equivalent to: 287 * 288 * <pre> 289 * Precision::integer().withMinDigits(2) 290 * </pre> 291 * 292 * @return A CompactNotation for passing to the NumberFormatter notation() setter. 293 * @stable ICU 60 294 */ 295 static CompactNotation compactShort(); 296 297 /** 298 * Print the number using long-form compact notation. For more information on compact notation, see 299 * {@link #compactShort}. 300 * 301 * <p> 302 * In long form, the powers of ten are spelled out fully. Example outputs in <em>en-US</em> when printing 8.765E7 303 * through 8.765E0: 304 * 305 * <pre> 306 * 88 million 307 * 8.8 million 308 * 876 thousand 309 * 88 thousand 310 * 8.8 thousand 311 * 876 312 * 88 313 * 8.8 314 * </pre> 315 * 316 * @return A CompactNotation for passing to the NumberFormatter notation() setter. 317 * @stable ICU 60 318 */ 319 static CompactNotation compactLong(); 320 321 /** 322 * Print the number using simple notation without any scaling by powers of ten. This is the default behavior. 323 * 324 * <p> 325 * Since this is the default behavior, this method needs to be called only when it is necessary to override a 326 * previous setting. 327 * 328 * <p> 329 * Example outputs in <em>en-US</em> when printing 8.765E7 through 8.765E0: 330 * 331 * <pre> 332 * 87,650,000 333 * 8,765,000 334 * 876,500 335 * 87,650 336 * 8,765 337 * 876.5 338 * 87.65 339 * 8.765 340 * </pre> 341 * 342 * @return A SimpleNotation for passing to the NumberFormatter notation() setter. 343 * @stable ICU 60 344 */ 345 static SimpleNotation simple(); 346 347 private: 348 enum NotationType { 349 NTN_SCIENTIFIC, NTN_COMPACT, NTN_SIMPLE, NTN_ERROR 350 } fType; 351 352 union NotationUnion { 353 // For NTN_SCIENTIFIC 354 /** @internal (private) */ 355 struct ScientificSettings { 356 /** @internal (private) */ 357 int8_t fEngineeringInterval; 358 /** @internal (private) */ 359 bool fRequireMinInt; 360 /** @internal (private) */ 361 impl::digits_t fMinExponentDigits; 362 /** @internal (private) */ 363 UNumberSignDisplay fExponentSignDisplay; 364 } scientific; 365 366 // For NTN_COMPACT 367 UNumberCompactStyle compactStyle; 368 369 // For NTN_ERROR 370 UErrorCode errorCode; 371 } fUnion; 372 373 typedef NotationUnion::ScientificSettings ScientificSettings; 374 Notation(const NotationType & type,const NotationUnion & union_)375 Notation(const NotationType &type, const NotationUnion &union_) : fType(type), fUnion(union_) {} 376 Notation(UErrorCode errorCode)377 Notation(UErrorCode errorCode) : fType(NTN_ERROR) { 378 fUnion.errorCode = errorCode; 379 } 380 Notation()381 Notation() : fType(NTN_SIMPLE), fUnion() {} 382 copyErrorTo(UErrorCode & status)383 UBool copyErrorTo(UErrorCode &status) const { 384 if (fType == NTN_ERROR) { 385 status = fUnion.errorCode; 386 return true; 387 } 388 return false; 389 } 390 391 // To allow MacroProps to initialize empty instances: 392 friend struct impl::MacroProps; 393 friend class ScientificNotation; 394 395 // To allow implementation to access internal types: 396 friend class impl::NumberFormatterImpl; 397 friend class impl::ScientificModifier; 398 friend class impl::ScientificHandler; 399 400 // To allow access to the skeleton generation code: 401 friend class impl::GeneratorHelpers; 402 }; 403 404 /** 405 * A class that defines the scientific notation style to be used when formatting numbers in NumberFormatter. 406 * 407 * <p> 408 * To create a ScientificNotation, use one of the factory methods in {@link Notation}. 409 * 410 * @stable ICU 60 411 */ 412 class U_I18N_API ScientificNotation : public Notation { 413 public: 414 /** 415 * Sets the minimum number of digits to show in the exponent of scientific notation, padding with zeros if 416 * necessary. Useful for fixed-width display. 417 * 418 * <p> 419 * For example, with minExponentDigits=2, the number 123 will be printed as "1.23E02" in <em>en-US</em> instead of 420 * the default "1.23E2". 421 * 422 * @param minExponentDigits 423 * The minimum number of digits to show in the exponent. 424 * @return A ScientificNotation, for chaining. 425 * @stable ICU 60 426 */ 427 ScientificNotation withMinExponentDigits(int32_t minExponentDigits) const; 428 429 /** 430 * Sets whether to show the sign on positive and negative exponents in scientific notation. The default is AUTO, 431 * showing the minus sign but not the plus sign. 432 * 433 * <p> 434 * For example, with exponentSignDisplay=ALWAYS, the number 123 will be printed as "1.23E+2" in <em>en-US</em> 435 * instead of the default "1.23E2". 436 * 437 * @param exponentSignDisplay 438 * The strategy for displaying the sign in the exponent. 439 * @return A ScientificNotation, for chaining. 440 * @stable ICU 60 441 */ 442 ScientificNotation withExponentSignDisplay(UNumberSignDisplay exponentSignDisplay) const; 443 444 private: 445 // Inherit constructor 446 using Notation::Notation; 447 448 // Raw constructor for NumberPropertyMapper 449 ScientificNotation(int8_t fEngineeringInterval, bool fRequireMinInt, impl::digits_t fMinExponentDigits, 450 UNumberSignDisplay fExponentSignDisplay); 451 452 friend class Notation; 453 454 // So that NumberPropertyMapper can create instances 455 friend class impl::NumberPropertyMapper; 456 }; 457 458 /** 459 * Extra name reserved in case it is needed in the future. 460 * 461 * @stable ICU 63 462 */ 463 typedef Precision SignificantDigitsPrecision; 464 465 /** 466 * A class that defines the rounding precision to be used when formatting numbers in NumberFormatter. 467 * 468 * <p> 469 * To create a Precision, use one of the factory methods. 470 * 471 * @stable ICU 60 472 */ 473 class U_I18N_API Precision : public UMemory { 474 475 public: 476 /** 477 * Show all available digits to full precision. 478 * 479 * <p> 480 * <strong>NOTE:</strong> When formatting a <em>double</em>, this method, along with {@link #minFraction} and 481 * {@link #minSignificantDigits}, will trigger complex algorithm similar to <em>Dragon4</em> to determine the 482 * low-order digits and the number of digits to display based on the value of the double. 483 * If the number of fraction places or significant digits can be bounded, consider using {@link #maxFraction} 484 * or {@link #maxSignificantDigits} instead to maximize performance. 485 * For more information, read the following blog post. 486 * 487 * <p> 488 * http://www.serpentine.com/blog/2011/06/29/here-be-dragons-advances-in-problems-you-didnt-even-know-you-had/ 489 * 490 * @return A Precision for chaining or passing to the NumberFormatter precision() setter. 491 * @stable ICU 60 492 */ 493 static Precision unlimited(); 494 495 /** 496 * Show numbers rounded if necessary to the nearest integer. 497 * 498 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter. 499 * @stable ICU 60 500 */ 501 static FractionPrecision integer(); 502 503 /** 504 * Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator). 505 * Additionally, pad with zeros to ensure that this number of places are always shown. 506 * 507 * <p> 508 * Example output with minMaxFractionPlaces = 3: 509 * 510 * <p> 511 * 87,650.000<br> 512 * 8,765.000<br> 513 * 876.500<br> 514 * 87.650<br> 515 * 8.765<br> 516 * 0.876<br> 517 * 0.088<br> 518 * 0.009<br> 519 * 0.000 (zero) 520 * 521 * <p> 522 * This method is equivalent to {@link #minMaxFraction} with both arguments equal. 523 * 524 * @param minMaxFractionPlaces 525 * The minimum and maximum number of numerals to display after the decimal separator (rounding if too 526 * long or padding with zeros if too short). 527 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter. 528 * @stable ICU 60 529 */ 530 static FractionPrecision fixedFraction(int32_t minMaxFractionPlaces); 531 532 /** 533 * Always show at least a certain number of fraction places after the decimal separator, padding with zeros if 534 * necessary. Do not perform rounding (display numbers to their full precision). 535 * 536 * <p> 537 * <strong>NOTE:</strong> If you are formatting <em>doubles</em>, see the performance note in {@link #unlimited}. 538 * 539 * @param minFractionPlaces 540 * The minimum number of numerals to display after the decimal separator (padding with zeros if 541 * necessary). 542 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter. 543 * @stable ICU 60 544 */ 545 static FractionPrecision minFraction(int32_t minFractionPlaces); 546 547 /** 548 * Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator). 549 * Unlike the other fraction rounding strategies, this strategy does <em>not</em> pad zeros to the end of the 550 * number. 551 * 552 * @param maxFractionPlaces 553 * The maximum number of numerals to display after the decimal mark (rounding if necessary). 554 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter. 555 * @stable ICU 60 556 */ 557 static FractionPrecision maxFraction(int32_t maxFractionPlaces); 558 559 /** 560 * Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator); 561 * in addition, always show at least a certain number of places after the decimal separator, padding with zeros if 562 * necessary. 563 * 564 * @param minFractionPlaces 565 * The minimum number of numerals to display after the decimal separator (padding with zeros if 566 * necessary). 567 * @param maxFractionPlaces 568 * The maximum number of numerals to display after the decimal separator (rounding if necessary). 569 * @return A FractionPrecision for chaining or passing to the NumberFormatter precision() setter. 570 * @stable ICU 60 571 */ 572 static FractionPrecision minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces); 573 574 /** 575 * Show numbers rounded if necessary to a certain number of significant digits or significant figures. Additionally, 576 * pad with zeros to ensure that this number of significant digits/figures are always shown. 577 * 578 * <p> 579 * This method is equivalent to {@link #minMaxSignificantDigits} with both arguments equal. 580 * 581 * @param minMaxSignificantDigits 582 * The minimum and maximum number of significant digits to display (rounding if too long or padding with 583 * zeros if too short). 584 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 585 * @stable ICU 62 586 */ 587 static SignificantDigitsPrecision fixedSignificantDigits(int32_t minMaxSignificantDigits); 588 589 /** 590 * Always show at least a certain number of significant digits/figures, padding with zeros if necessary. Do not 591 * perform rounding (display numbers to their full precision). 592 * 593 * <p> 594 * <strong>NOTE:</strong> If you are formatting <em>doubles</em>, see the performance note in {@link #unlimited}. 595 * 596 * @param minSignificantDigits 597 * The minimum number of significant digits to display (padding with zeros if too short). 598 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 599 * @stable ICU 62 600 */ 601 static SignificantDigitsPrecision minSignificantDigits(int32_t minSignificantDigits); 602 603 /** 604 * Show numbers rounded if necessary to a certain number of significant digits/figures. 605 * 606 * @param maxSignificantDigits 607 * The maximum number of significant digits to display (rounding if too long). 608 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 609 * @stable ICU 62 610 */ 611 static SignificantDigitsPrecision maxSignificantDigits(int32_t maxSignificantDigits); 612 613 /** 614 * Show numbers rounded if necessary to a certain number of significant digits/figures; in addition, always show at 615 * least a certain number of significant digits, padding with zeros if necessary. 616 * 617 * @param minSignificantDigits 618 * The minimum number of significant digits to display (padding with zeros if necessary). 619 * @param maxSignificantDigits 620 * The maximum number of significant digits to display (rounding if necessary). 621 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 622 * @stable ICU 62 623 */ 624 static SignificantDigitsPrecision minMaxSignificantDigits(int32_t minSignificantDigits, 625 int32_t maxSignificantDigits); 626 627 /** 628 * Show numbers rounded if necessary to the closest multiple of a certain rounding increment. For example, if the 629 * rounding increment is 0.5, then round 1.2 to 1 and round 1.3 to 1.5. 630 * 631 * <p> 632 * In order to ensure that numbers are padded to the appropriate number of fraction places, call 633 * withMinFraction() on the return value of this method. 634 * For example, to round to the nearest 0.5 and always display 2 numerals after the 635 * decimal separator (to display 1.2 as "1.00" and 1.3 as "1.50"), you can run: 636 * 637 * <pre> 638 * Precision::increment(0.5).withMinFraction(2) 639 * </pre> 640 * 641 * @param roundingIncrement 642 * The increment to which to round numbers. 643 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 644 * @stable ICU 60 645 */ 646 static IncrementPrecision increment(double roundingIncrement); 647 648 /** 649 * Version of `Precision::increment()` that takes an integer at a particular power of 10. 650 * 651 * To round to the nearest 0.5 and display 2 fraction digits, with this function, you should write one of the following: 652 * 653 * <pre> 654 * Precision::incrementExact(5, -1).withMinFraction(2) 655 * Precision::incrementExact(50, -2).withMinFraction(2) 656 * Precision::incrementExact(50, -2) 657 * </pre> 658 * 659 * This is analagous to ICU4J `Precision.increment(new BigDecimal("0.50"))`. 660 * 661 * This behavior is modeled after ECMA-402. For more information, see: 662 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingincrement 663 * 664 * @param mantissa 665 * The increment to which to round numbers. 666 * @param magnitude 667 * The power of 10 of the ones digit of the mantissa. 668 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 669 * @stable ICU 71 670 */ 671 static IncrementPrecision incrementExact(uint64_t mantissa, int16_t magnitude); 672 673 /** 674 * Show numbers rounded and padded according to the rules for the currency unit. The most common 675 * rounding precision settings for currencies include <code>Precision::fixedFraction(2)</code>, 676 * <code>Precision::integer()</code>, and <code>Precision::increment(0.05)</code> for cash transactions 677 * ("nickel rounding"). 678 * 679 * <p> 680 * The exact rounding details will be resolved at runtime based on the currency unit specified in the 681 * NumberFormatter chain. To round according to the rules for one currency while displaying the symbol for another 682 * currency, the withCurrency() method can be called on the return value of this method. 683 * 684 * @param currencyUsage 685 * Either STANDARD (for digital transactions) or CASH (for transactions where the rounding increment may 686 * be limited by the available denominations of cash or coins). 687 * @return A CurrencyPrecision for chaining or passing to the NumberFormatter precision() setter. 688 * @stable ICU 60 689 */ 690 static CurrencyPrecision currency(UCurrencyUsage currencyUsage); 691 692 /** 693 * Configure how trailing zeros are displayed on numbers. For example, to hide trailing zeros 694 * when the number is an integer, use UNUM_TRAILING_ZERO_HIDE_IF_WHOLE. 695 * 696 * @param trailingZeroDisplay Option to configure the display of trailing zeros. 697 * @stable ICU 69 698 */ 699 Precision trailingZeroDisplay(UNumberTrailingZeroDisplay trailingZeroDisplay) const; 700 701 private: 702 enum PrecisionType { 703 RND_BOGUS, 704 RND_NONE, 705 RND_FRACTION, 706 RND_SIGNIFICANT, 707 RND_FRACTION_SIGNIFICANT, 708 709 // Used for strange increments like 3.14. 710 RND_INCREMENT, 711 712 // Used for increments with 1 as the only digit. This is different than fraction 713 // rounding because it supports having additional trailing zeros. For example, this 714 // class is used to round with the increment 0.010. 715 RND_INCREMENT_ONE, 716 717 // Used for increments with 5 as the only digit (nickel rounding). 718 RND_INCREMENT_FIVE, 719 720 RND_CURRENCY, 721 RND_ERROR 722 } fType; 723 724 union PrecisionUnion { 725 /** @internal (private) */ 726 struct FractionSignificantSettings { 727 // For RND_FRACTION, RND_SIGNIFICANT, and RND_FRACTION_SIGNIFICANT 728 /** @internal (private) */ 729 impl::digits_t fMinFrac; 730 /** @internal (private) */ 731 impl::digits_t fMaxFrac; 732 /** @internal (private) */ 733 impl::digits_t fMinSig; 734 /** @internal (private) */ 735 impl::digits_t fMaxSig; 736 /** @internal (private) */ 737 UNumberRoundingPriority fPriority; 738 /** 739 * Whether to retain trailing zeros based on the looser strategy. 740 * @internal (private) 741 */ 742 bool fRetain; 743 } fracSig; 744 /** @internal (private) */ 745 struct IncrementSettings { 746 // For RND_INCREMENT, RND_INCREMENT_ONE, and RND_INCREMENT_FIVE 747 // Note: This is a union, so we shouldn't own memory, since 748 // the default destructor would leak it. 749 /** @internal (private) */ 750 uint64_t fIncrement; 751 /** @internal (private) */ 752 impl::digits_t fIncrementMagnitude; 753 /** @internal (private) */ 754 impl::digits_t fMinFrac; 755 } increment; 756 UCurrencyUsage currencyUsage; // For RND_CURRENCY 757 UErrorCode errorCode; // For RND_ERROR 758 } fUnion; 759 760 UNumberTrailingZeroDisplay fTrailingZeroDisplay = UNUM_TRAILING_ZERO_AUTO; 761 762 typedef PrecisionUnion::FractionSignificantSettings FractionSignificantSettings; 763 typedef PrecisionUnion::IncrementSettings IncrementSettings; 764 Precision(const PrecisionType & type,const PrecisionUnion & union_)765 Precision(const PrecisionType& type, const PrecisionUnion& union_) 766 : fType(type), fUnion(union_) {} 767 Precision(UErrorCode errorCode)768 Precision(UErrorCode errorCode) : fType(RND_ERROR) { 769 fUnion.errorCode = errorCode; 770 } 771 Precision()772 Precision() : fType(RND_BOGUS) {} 773 isBogus()774 bool isBogus() const { 775 return fType == RND_BOGUS; 776 } 777 copyErrorTo(UErrorCode & status)778 UBool copyErrorTo(UErrorCode &status) const { 779 if (fType == RND_ERROR) { 780 status = fUnion.errorCode; 781 return true; 782 } 783 return false; 784 } 785 786 // On the parent type so that this method can be called internally on Precision instances. 787 Precision withCurrency(const CurrencyUnit ¤cy, UErrorCode &status) const; 788 789 static FractionPrecision constructFraction(int32_t minFrac, int32_t maxFrac); 790 791 static Precision constructSignificant(int32_t minSig, int32_t maxSig); 792 793 static Precision constructFractionSignificant( 794 const FractionPrecision &base, 795 int32_t minSig, 796 int32_t maxSig, 797 UNumberRoundingPriority priority, 798 bool retain); 799 800 static IncrementPrecision constructIncrement(uint64_t increment, impl::digits_t magnitude); 801 802 static CurrencyPrecision constructCurrency(UCurrencyUsage usage); 803 804 // To allow MacroProps/MicroProps to initialize bogus instances: 805 friend struct impl::MacroProps; 806 friend struct impl::MicroProps; 807 808 // To allow NumberFormatterImpl to access isBogus() and other internal methods: 809 friend class impl::NumberFormatterImpl; 810 811 // To allow NumberPropertyMapper to create instances from DecimalFormatProperties: 812 friend class impl::NumberPropertyMapper; 813 814 // To allow access to the main implementation class: 815 friend class impl::RoundingImpl; 816 817 // To allow child classes to call private methods: 818 friend class FractionPrecision; 819 friend class CurrencyPrecision; 820 friend class IncrementPrecision; 821 822 // To allow access to the skeleton generation code: 823 friend class impl::GeneratorHelpers; 824 825 // To allow access to isBogus and the default (bogus) constructor: 826 friend class units::UnitsRouter; 827 }; 828 829 /** 830 * A class that defines a rounding precision based on a number of fraction places and optionally significant digits to be 831 * used when formatting numbers in NumberFormatter. 832 * 833 * <p> 834 * To create a FractionPrecision, use one of the factory methods on Precision. 835 * 836 * @stable ICU 60 837 */ 838 class U_I18N_API FractionPrecision : public Precision { 839 public: 840 /** 841 * Override maximum fraction digits with maximum significant digits depending on the magnitude 842 * of the number. See UNumberRoundingPriority. 843 * 844 * @param minSignificantDigits 845 * Pad trailing zeros to achieve this minimum number of significant digits. 846 * @param maxSignificantDigits 847 * Round the number to achieve this maximum number of significant digits. 848 * @param priority 849 * How to disambiguate between fraction digits and significant digits. 850 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 851 * 852 * @stable ICU 69 853 */ 854 Precision withSignificantDigits( 855 int32_t minSignificantDigits, 856 int32_t maxSignificantDigits, 857 UNumberRoundingPriority priority) const; 858 859 /** 860 * Ensure that no less than this number of significant digits are retained when rounding 861 * according to fraction rules. 862 * 863 * For example, with integer rounding, the number 3.141 becomes "3". However, with minimum 864 * figures set to 2, 3.141 becomes "3.1" instead. 865 * 866 * This setting does not affect the number of trailing zeros. For example, 3.01 would print as 867 * "3", not "3.0". 868 * 869 * This is equivalent to `withSignificantDigits(1, minSignificantDigits, RELAXED)`. 870 * 871 * @param minSignificantDigits 872 * The number of significant figures to guarantee. 873 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 874 * @stable ICU 60 875 */ 876 Precision withMinDigits(int32_t minSignificantDigits) const; 877 878 /** 879 * Ensure that no more than this number of significant digits are retained when rounding 880 * according to fraction rules. 881 * 882 * For example, with integer rounding, the number 123.4 becomes "123". However, with maximum 883 * figures set to 2, 123.4 becomes "120" instead. 884 * 885 * This setting does not affect the number of trailing zeros. For example, with fixed fraction 886 * of 2, 123.4 would become "120.00". 887 * 888 * This is equivalent to `withSignificantDigits(1, maxSignificantDigits, STRICT)`. 889 * 890 * @param maxSignificantDigits 891 * Round the number to no more than this number of significant figures. 892 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 893 * @stable ICU 60 894 */ 895 Precision withMaxDigits(int32_t maxSignificantDigits) const; 896 897 private: 898 // Inherit constructor 899 using Precision::Precision; 900 901 // To allow parent class to call this class's constructor: 902 friend class Precision; 903 }; 904 905 /** 906 * A class that defines a rounding precision parameterized by a currency to be used when formatting numbers in 907 * NumberFormatter. 908 * 909 * <p> 910 * To create a CurrencyPrecision, use one of the factory methods on Precision. 911 * 912 * @stable ICU 60 913 */ 914 class U_I18N_API CurrencyPrecision : public Precision { 915 public: 916 /** 917 * Associates a currency with this rounding precision. 918 * 919 * <p> 920 * <strong>Calling this method is <em>not required</em></strong>, because the currency specified in unit() 921 * is automatically applied to currency rounding precisions. However, 922 * this method enables you to override that automatic association. 923 * 924 * <p> 925 * This method also enables numbers to be formatted using currency rounding rules without explicitly using a 926 * currency format. 927 * 928 * @param currency 929 * The currency to associate with this rounding precision. 930 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 931 * @stable ICU 60 932 */ 933 Precision withCurrency(const CurrencyUnit ¤cy) const; 934 935 private: 936 // Inherit constructor 937 using Precision::Precision; 938 939 // To allow parent class to call this class's constructor: 940 friend class Precision; 941 }; 942 943 /** 944 * A class that defines a rounding precision parameterized by a rounding increment to be used when formatting numbers in 945 * NumberFormatter. 946 * 947 * <p> 948 * To create an IncrementPrecision, use one of the factory methods on Precision. 949 * 950 * @stable ICU 60 951 */ 952 class U_I18N_API IncrementPrecision : public Precision { 953 public: 954 /** 955 * Specifies the minimum number of fraction digits to render after the decimal separator, padding with zeros if 956 * necessary. By default, no trailing zeros are added. 957 * 958 * <p> 959 * For example, if the rounding increment is 0.5 and minFrac is 2, then the resulting strings include "0.00", 960 * "0.50", "1.00", and "1.50". 961 * 962 * <p> 963 * Note: In ICU4J, this functionality is accomplished via the scale of the BigDecimal rounding increment. 964 * 965 * @param minFrac The minimum number of digits after the decimal separator. 966 * @return A precision for chaining or passing to the NumberFormatter precision() setter. 967 * @stable ICU 60 968 */ 969 Precision withMinFraction(int32_t minFrac) const; 970 971 private: 972 // Inherit constructor 973 using Precision::Precision; 974 975 // To allow parent class to call this class's constructor: 976 friend class Precision; 977 }; 978 979 /** 980 * A class that defines the strategy for padding and truncating integers before the decimal separator. 981 * 982 * <p> 983 * To create an IntegerWidth, use one of the factory methods. 984 * 985 * @stable ICU 60 986 * @see NumberFormatter 987 */ 988 class U_I18N_API IntegerWidth : public UMemory { 989 public: 990 /** 991 * Pad numbers at the beginning with zeros to guarantee a certain number of numerals before the decimal separator. 992 * 993 * <p> 994 * For example, with minInt=3, the number 55 will get printed as "055". 995 * 996 * @param minInt 997 * The minimum number of places before the decimal separator. 998 * @return An IntegerWidth for chaining or passing to the NumberFormatter integerWidth() setter. 999 * @stable ICU 60 1000 */ 1001 static IntegerWidth zeroFillTo(int32_t minInt); 1002 1003 /** 1004 * Truncate numbers exceeding a certain number of numerals before the decimal separator. 1005 * 1006 * For example, with maxInt=3, the number 1234 will get printed as "234". 1007 * 1008 * @param maxInt 1009 * The maximum number of places before the decimal separator. maxInt == -1 means no 1010 * truncation. 1011 * @return An IntegerWidth for passing to the NumberFormatter integerWidth() setter. 1012 * @stable ICU 60 1013 */ 1014 IntegerWidth truncateAt(int32_t maxInt); 1015 1016 private: 1017 union { 1018 struct { 1019 impl::digits_t fMinInt; 1020 impl::digits_t fMaxInt; 1021 bool fFormatFailIfMoreThanMaxDigits; 1022 } minMaxInt; 1023 UErrorCode errorCode; 1024 } fUnion; 1025 bool fHasError = false; 1026 1027 IntegerWidth(impl::digits_t minInt, impl::digits_t maxInt, bool formatFailIfMoreThanMaxDigits); 1028 IntegerWidth(UErrorCode errorCode)1029 IntegerWidth(UErrorCode errorCode) { // NOLINT 1030 fUnion.errorCode = errorCode; 1031 fHasError = true; 1032 } 1033 IntegerWidth()1034 IntegerWidth() { // NOLINT 1035 fUnion.minMaxInt.fMinInt = -1; 1036 } 1037 1038 /** Returns the default instance. */ standard()1039 static IntegerWidth standard() { 1040 return IntegerWidth::zeroFillTo(1); 1041 } 1042 isBogus()1043 bool isBogus() const { 1044 return !fHasError && fUnion.minMaxInt.fMinInt == -1; 1045 } 1046 copyErrorTo(UErrorCode & status)1047 UBool copyErrorTo(UErrorCode &status) const { 1048 if (fHasError) { 1049 status = fUnion.errorCode; 1050 return true; 1051 } 1052 return false; 1053 } 1054 1055 void apply(impl::DecimalQuantity &quantity, UErrorCode &status) const; 1056 1057 bool operator==(const IntegerWidth& other) const; 1058 1059 // To allow MacroProps/MicroProps to initialize empty instances: 1060 friend struct impl::MacroProps; 1061 friend struct impl::MicroProps; 1062 1063 // To allow NumberFormatterImpl to access isBogus(): 1064 friend class impl::NumberFormatterImpl; 1065 1066 // To allow the use of this class when formatting: 1067 friend class impl::MutablePatternModifier; 1068 friend class impl::ImmutablePatternModifier; 1069 1070 // So that NumberPropertyMapper can create instances 1071 friend class impl::NumberPropertyMapper; 1072 1073 // To allow access to the skeleton generation code: 1074 friend class impl::GeneratorHelpers; 1075 }; 1076 1077 /** 1078 * A class that defines a quantity by which a number should be multiplied when formatting. 1079 * 1080 * <p> 1081 * To create a Scale, use one of the factory methods. 1082 * 1083 * @stable ICU 62 1084 */ 1085 class U_I18N_API Scale : public UMemory { 1086 public: 1087 /** 1088 * Do not change the value of numbers when formatting or parsing. 1089 * 1090 * @return A Scale to prevent any multiplication. 1091 * @stable ICU 62 1092 */ 1093 static Scale none(); 1094 1095 /** 1096 * Multiply numbers by a power of ten before formatting. Useful for combining with a percent unit: 1097 * 1098 * <pre> 1099 * NumberFormatter::with().unit(NoUnit::percent()).multiplier(Scale::powerOfTen(2)) 1100 * </pre> 1101 * 1102 * @return A Scale for passing to the setter in NumberFormatter. 1103 * @stable ICU 62 1104 */ 1105 static Scale powerOfTen(int32_t power); 1106 1107 /** 1108 * Multiply numbers by an arbitrary value before formatting. Useful for unit conversions. 1109 * 1110 * This method takes a string in a decimal number format with syntax 1111 * as defined in the Decimal Arithmetic Specification, available at 1112 * http://speleotrove.com/decimal 1113 * 1114 * Also see the version of this method that takes a double. 1115 * 1116 * @return A Scale for passing to the setter in NumberFormatter. 1117 * @stable ICU 62 1118 */ 1119 static Scale byDecimal(StringPiece multiplicand); 1120 1121 /** 1122 * Multiply numbers by an arbitrary value before formatting. Useful for unit conversions. 1123 * 1124 * This method takes a double; also see the version of this method that takes an exact decimal. 1125 * 1126 * @return A Scale for passing to the setter in NumberFormatter. 1127 * @stable ICU 62 1128 */ 1129 static Scale byDouble(double multiplicand); 1130 1131 /** 1132 * Multiply a number by both a power of ten and by an arbitrary double value. 1133 * 1134 * @return A Scale for passing to the setter in NumberFormatter. 1135 * @stable ICU 62 1136 */ 1137 static Scale byDoubleAndPowerOfTen(double multiplicand, int32_t power); 1138 1139 // We need a custom destructor for the DecNum, which means we need to declare 1140 // the copy/move constructor/assignment quartet. 1141 1142 /** @stable ICU 62 */ 1143 Scale(const Scale& other); 1144 1145 /** @stable ICU 62 */ 1146 Scale& operator=(const Scale& other); 1147 1148 /** @stable ICU 62 */ 1149 Scale(Scale&& src) noexcept; 1150 1151 /** @stable ICU 62 */ 1152 Scale& operator=(Scale&& src) noexcept; 1153 1154 /** @stable ICU 62 */ 1155 ~Scale(); 1156 1157 #ifndef U_HIDE_INTERNAL_API 1158 /** @internal */ 1159 Scale(int32_t magnitude, impl::DecNum* arbitraryToAdopt); 1160 #endif /* U_HIDE_INTERNAL_API */ 1161 1162 private: 1163 int32_t fMagnitude; 1164 impl::DecNum* fArbitrary; 1165 UErrorCode fError; 1166 Scale(UErrorCode error)1167 Scale(UErrorCode error) : fMagnitude(0), fArbitrary(nullptr), fError(error) {} 1168 Scale()1169 Scale() : fMagnitude(0), fArbitrary(nullptr), fError(U_ZERO_ERROR) {} 1170 isValid()1171 bool isValid() const { 1172 return fMagnitude != 0 || fArbitrary != nullptr; 1173 } 1174 copyErrorTo(UErrorCode & status)1175 UBool copyErrorTo(UErrorCode &status) const { 1176 if (U_FAILURE(fError)) { 1177 status = fError; 1178 return true; 1179 } 1180 return false; 1181 } 1182 1183 void applyTo(impl::DecimalQuantity& quantity) const; 1184 1185 void applyReciprocalTo(impl::DecimalQuantity& quantity) const; 1186 1187 // To allow MacroProps/MicroProps to initialize empty instances: 1188 friend struct impl::MacroProps; 1189 friend struct impl::MicroProps; 1190 1191 // To allow NumberFormatterImpl to access isBogus() and perform other operations: 1192 friend class impl::NumberFormatterImpl; 1193 1194 // To allow the helper class MultiplierFormatHandler access to private fields: 1195 friend class impl::MultiplierFormatHandler; 1196 1197 // To allow access to the skeleton generation code: 1198 friend class impl::GeneratorHelpers; 1199 1200 // To allow access to parsing code: 1201 friend class ::icu::numparse::impl::NumberParserImpl; 1202 friend class ::icu::numparse::impl::MultiplierParseHandler; 1203 }; 1204 1205 namespace impl { 1206 1207 // Do not enclose entire StringProp with #ifndef U_HIDE_INTERNAL_API, needed for a protected field. 1208 // And do not enclose its class boilerplate within #ifndef U_HIDE_INTERNAL_API. 1209 /** 1210 * Manages NumberFormatterSettings::usage()'s char* instance on the heap. 1211 * @internal 1212 */ 1213 class U_I18N_API StringProp : public UMemory { 1214 1215 public: 1216 /** @internal */ 1217 ~StringProp(); 1218 1219 /** @internal */ 1220 StringProp(const StringProp &other); 1221 1222 /** @internal */ 1223 StringProp &operator=(const StringProp &other); 1224 1225 #ifndef U_HIDE_INTERNAL_API 1226 1227 /** @internal */ 1228 StringProp(StringProp &&src) noexcept; 1229 1230 /** @internal */ 1231 StringProp &operator=(StringProp &&src) noexcept; 1232 1233 /** @internal */ length()1234 int16_t length() const { 1235 return fLength; 1236 } 1237 1238 /** @internal 1239 * Makes a copy of value. Set to "" to unset. 1240 */ 1241 void set(StringPiece value); 1242 1243 /** @internal */ isSet()1244 bool isSet() const { 1245 return fLength > 0; 1246 } 1247 1248 #endif // U_HIDE_INTERNAL_API 1249 1250 private: 1251 char *fValue; 1252 int16_t fLength; 1253 UErrorCode fError; 1254 StringProp()1255 StringProp() : fValue(nullptr), fLength(0), fError(U_ZERO_ERROR) { 1256 } 1257 1258 /** @internal (private) */ copyErrorTo(UErrorCode & status)1259 UBool copyErrorTo(UErrorCode &status) const { 1260 if (U_FAILURE(fError)) { 1261 status = fError; 1262 return true; 1263 } 1264 return false; 1265 } 1266 1267 // Allow NumberFormatterImpl to access fValue. 1268 friend class impl::NumberFormatterImpl; 1269 1270 // Allow skeleton generation code to access private members. 1271 friend class impl::GeneratorHelpers; 1272 1273 // Allow MacroProps/MicroProps to initialize empty instances and to call 1274 // copyErrorTo(). 1275 friend struct impl::MacroProps; 1276 }; 1277 1278 // Do not enclose entire SymbolsWrapper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field 1279 /** @internal */ 1280 class U_I18N_API SymbolsWrapper : public UMemory { 1281 public: 1282 /** @internal */ SymbolsWrapper()1283 SymbolsWrapper() : fType(SYMPTR_NONE), fPtr{nullptr} {} 1284 1285 /** @internal */ 1286 SymbolsWrapper(const SymbolsWrapper &other); 1287 1288 /** @internal */ 1289 SymbolsWrapper &operator=(const SymbolsWrapper &other); 1290 1291 /** @internal */ 1292 SymbolsWrapper(SymbolsWrapper&& src) noexcept; 1293 1294 /** @internal */ 1295 SymbolsWrapper &operator=(SymbolsWrapper&& src) noexcept; 1296 1297 /** @internal */ 1298 ~SymbolsWrapper(); 1299 1300 #ifndef U_HIDE_INTERNAL_API 1301 1302 /** 1303 * The provided object is copied, but we do not adopt it. 1304 * @internal 1305 */ 1306 void setTo(const DecimalFormatSymbols &dfs); 1307 1308 /** 1309 * Adopt the provided object. 1310 * @internal 1311 */ 1312 void setTo(const NumberingSystem *ns); 1313 1314 /** 1315 * Whether the object is currently holding a DecimalFormatSymbols. 1316 * @internal 1317 */ 1318 bool isDecimalFormatSymbols() const; 1319 1320 /** 1321 * Whether the object is currently holding a NumberingSystem. 1322 * @internal 1323 */ 1324 bool isNumberingSystem() const; 1325 1326 /** 1327 * Get the DecimalFormatSymbols pointer. No ownership change. 1328 * @internal 1329 */ 1330 const DecimalFormatSymbols *getDecimalFormatSymbols() const; 1331 1332 /** 1333 * Get the NumberingSystem pointer. No ownership change. 1334 * @internal 1335 */ 1336 const NumberingSystem *getNumberingSystem() const; 1337 1338 #endif // U_HIDE_INTERNAL_API 1339 1340 /** @internal */ copyErrorTo(UErrorCode & status)1341 UBool copyErrorTo(UErrorCode &status) const { 1342 if (fType == SYMPTR_DFS && fPtr.dfs == nullptr) { 1343 status = U_MEMORY_ALLOCATION_ERROR; 1344 return true; 1345 } else if (fType == SYMPTR_NS && fPtr.ns == nullptr) { 1346 status = U_MEMORY_ALLOCATION_ERROR; 1347 return true; 1348 } 1349 return false; 1350 } 1351 1352 private: 1353 enum SymbolsPointerType { 1354 SYMPTR_NONE, SYMPTR_DFS, SYMPTR_NS 1355 } fType; 1356 1357 union { 1358 const DecimalFormatSymbols *dfs; 1359 const NumberingSystem *ns; 1360 } fPtr; 1361 1362 void doCopyFrom(const SymbolsWrapper &other); 1363 1364 void doMoveFrom(SymbolsWrapper&& src); 1365 1366 void doCleanup(); 1367 }; 1368 1369 // Do not enclose entire Grouper with #ifndef U_HIDE_INTERNAL_API, needed for a protected field 1370 /** @internal */ 1371 class U_I18N_API Grouper : public UMemory { 1372 public: 1373 #ifndef U_HIDE_INTERNAL_API 1374 /** @internal */ 1375 static Grouper forStrategy(UNumberGroupingStrategy grouping); 1376 1377 /** 1378 * Resolve the values in Properties to a Grouper object. 1379 * @internal 1380 */ 1381 static Grouper forProperties(const DecimalFormatProperties& properties); 1382 1383 // Future: static Grouper forProperties(DecimalFormatProperties& properties); 1384 1385 /** @internal */ Grouper(int16_t grouping1,int16_t grouping2,int16_t minGrouping,UNumberGroupingStrategy strategy)1386 Grouper(int16_t grouping1, int16_t grouping2, int16_t minGrouping, UNumberGroupingStrategy strategy) 1387 : fGrouping1(grouping1), 1388 fGrouping2(grouping2), 1389 fMinGrouping(minGrouping), 1390 fStrategy(strategy) {} 1391 1392 /** @internal */ 1393 int16_t getPrimary() const; 1394 1395 /** @internal */ 1396 int16_t getSecondary() const; 1397 #endif // U_HIDE_INTERNAL_API 1398 1399 private: 1400 /** 1401 * The grouping sizes, with the following special values: 1402 * <ul> 1403 * <li>-1 = no grouping 1404 * <li>-2 = needs locale data 1405 * <li>-4 = fall back to Western grouping if not in locale 1406 * </ul> 1407 */ 1408 int16_t fGrouping1; 1409 int16_t fGrouping2; 1410 1411 /** 1412 * The minimum grouping size, with the following special values: 1413 * <ul> 1414 * <li>-2 = needs locale data 1415 * <li>-3 = no less than 2 1416 * </ul> 1417 */ 1418 int16_t fMinGrouping; 1419 1420 /** 1421 * The UNumberGroupingStrategy that was used to create this Grouper, or UNUM_GROUPING_COUNT if this 1422 * was not created from a UNumberGroupingStrategy. 1423 */ 1424 UNumberGroupingStrategy fStrategy; 1425 Grouper()1426 Grouper() : fGrouping1(-3) {} 1427 isBogus()1428 bool isBogus() const { 1429 return fGrouping1 == -3; 1430 } 1431 1432 /** NON-CONST: mutates the current instance. */ 1433 void setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale); 1434 1435 bool groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const; 1436 1437 // To allow MacroProps/MicroProps to initialize empty instances: 1438 friend struct MacroProps; 1439 friend struct MicroProps; 1440 friend struct SimpleMicroProps; 1441 1442 // To allow NumberFormatterImpl to access isBogus() and perform other operations: 1443 friend class NumberFormatterImpl; 1444 friend class ::icu::number::SimpleNumberFormatter; 1445 1446 // To allow NumberParserImpl to perform setLocaleData(): 1447 friend class ::icu::numparse::impl::NumberParserImpl; 1448 1449 // To allow access to the skeleton generation code: 1450 friend class impl::GeneratorHelpers; 1451 }; 1452 1453 // Do not enclose entire Padder with #ifndef U_HIDE_INTERNAL_API, needed for a protected field 1454 /** @internal */ 1455 class U_I18N_API Padder : public UMemory { 1456 public: 1457 #ifndef U_HIDE_INTERNAL_API 1458 /** @internal */ 1459 static Padder none(); 1460 1461 /** @internal */ 1462 static Padder codePoints(UChar32 cp, int32_t targetWidth, UNumberFormatPadPosition position); 1463 1464 /** @internal */ 1465 static Padder forProperties(const DecimalFormatProperties& properties); 1466 #endif // U_HIDE_INTERNAL_API 1467 1468 private: 1469 UChar32 fWidth; // -3 = error; -2 = bogus; -1 = no padding 1470 union { 1471 struct { 1472 int32_t fCp; 1473 UNumberFormatPadPosition fPosition; 1474 } padding; 1475 UErrorCode errorCode; 1476 } fUnion; 1477 1478 Padder(UChar32 cp, int32_t width, UNumberFormatPadPosition position); 1479 1480 Padder(int32_t width); 1481 Padder(UErrorCode errorCode)1482 Padder(UErrorCode errorCode) : fWidth(-3) { // NOLINT 1483 fUnion.errorCode = errorCode; 1484 } 1485 Padder()1486 Padder() : fWidth(-2) {} // NOLINT 1487 isBogus()1488 bool isBogus() const { 1489 return fWidth == -2; 1490 } 1491 copyErrorTo(UErrorCode & status)1492 UBool copyErrorTo(UErrorCode &status) const { 1493 if (fWidth == -3) { 1494 status = fUnion.errorCode; 1495 return true; 1496 } 1497 return false; 1498 } 1499 isValid()1500 bool isValid() const { 1501 return fWidth > 0; 1502 } 1503 1504 int32_t padAndApply(const impl::Modifier &mod1, const impl::Modifier &mod2, 1505 FormattedStringBuilder &string, int32_t leftIndex, int32_t rightIndex, 1506 UErrorCode &status) const; 1507 1508 // To allow MacroProps/MicroProps to initialize empty instances: 1509 friend struct MacroProps; 1510 friend struct MicroProps; 1511 1512 // To allow NumberFormatterImpl to access isBogus() and perform other operations: 1513 friend class impl::NumberFormatterImpl; 1514 1515 // To allow access to the skeleton generation code: 1516 friend class impl::GeneratorHelpers; 1517 }; 1518 1519 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field 1520 /** @internal */ 1521 struct U_I18N_API MacroProps : public UMemory { 1522 /** @internal */ 1523 Notation notation; 1524 1525 /** @internal */ 1526 MeasureUnit unit; // = MeasureUnit(); (the base dimensionless unit) 1527 1528 /** @internal */ 1529 MeasureUnit perUnit; // = MeasureUnit(); (the base dimensionless unit) 1530 1531 /** @internal */ 1532 Precision precision; // = Precision(); (bogus) 1533 1534 /** @internal */ 1535 UNumberFormatRoundingMode roundingMode = UNUM_ROUND_HALFEVEN; 1536 1537 /** @internal */ 1538 Grouper grouper; // = Grouper(); (bogus) 1539 1540 /** @internal */ 1541 Padder padder; // = Padder(); (bogus) 1542 1543 /** @internal */ 1544 IntegerWidth integerWidth; // = IntegerWidth(); (bogus) 1545 1546 /** @internal */ 1547 SymbolsWrapper symbols; 1548 1549 // UNUM_XYZ_COUNT denotes null (bogus) values. 1550 1551 /** @internal */ 1552 UNumberUnitWidth unitWidth = UNUM_UNIT_WIDTH_COUNT; 1553 1554 /** @internal */ 1555 UNumberSignDisplay sign = UNUM_SIGN_COUNT; 1556 1557 /** @internal */ 1558 bool approximately = false; 1559 1560 /** @internal */ 1561 UNumberDecimalSeparatorDisplay decimal = UNUM_DECIMAL_SEPARATOR_COUNT; 1562 1563 /** @internal */ 1564 Scale scale; // = Scale(); (benign value) 1565 1566 /** @internal */ 1567 StringProp usage; // = StringProp(); (no usage) 1568 1569 /** @internal */ 1570 StringProp unitDisplayCase; // = StringProp(); (nominative) 1571 1572 /** @internal */ 1573 const AffixPatternProvider* affixProvider = nullptr; // no ownership 1574 1575 /** @internal */ 1576 const PluralRules* rules = nullptr; // no ownership 1577 1578 /** @internal */ 1579 int32_t threshold = kInternalDefaultThreshold; 1580 1581 /** @internal */ 1582 Locale locale; 1583 1584 // NOTE: Uses default copy and move constructors. 1585 1586 /** 1587 * Check all members for errors. 1588 * @internal 1589 */ copyErrorToMacroProps1590 bool copyErrorTo(UErrorCode &status) const { 1591 return notation.copyErrorTo(status) || precision.copyErrorTo(status) || 1592 padder.copyErrorTo(status) || integerWidth.copyErrorTo(status) || 1593 symbols.copyErrorTo(status) || scale.copyErrorTo(status) || usage.copyErrorTo(status) || 1594 unitDisplayCase.copyErrorTo(status); 1595 } 1596 }; 1597 1598 } // namespace impl 1599 1600 #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER) 1601 // Ignore MSVC warning 4661. This is generated for NumberFormatterSettings<>::toSkeleton() as this method 1602 // is defined elsewhere (in number_skeletons.cpp). The compiler is warning that the explicit template instantiation 1603 // inside this single translation unit (CPP file) is incomplete, and thus it isn't sure if the template class is 1604 // fully defined. However, since each translation unit explicitly instantiates all the necessary template classes, 1605 // they will all be passed to the linker, and the linker will still find and export all the class members. 1606 #pragma warning(push) 1607 #pragma warning(disable: 4661) 1608 #endif 1609 1610 /** 1611 * An abstract base class for specifying settings related to number formatting. This class is implemented by 1612 * {@link UnlocalizedNumberFormatter} and {@link LocalizedNumberFormatter}. This class is not intended for 1613 * public subclassing. 1614 */ 1615 template<typename Derived> 1616 class U_I18N_API NumberFormatterSettings { 1617 public: 1618 /** 1619 * Specifies the notation style (simple, scientific, or compact) for rendering numbers. 1620 * 1621 * <ul> 1622 * <li>Simple notation: "12,300" 1623 * <li>Scientific notation: "1.23E4" 1624 * <li>Compact notation: "12K" 1625 * </ul> 1626 * 1627 * <p> 1628 * All notation styles will be properly localized with locale data, and all notation styles are compatible with 1629 * units, rounding precisions, and other number formatter settings. 1630 * 1631 * <p> 1632 * Pass this method the return value of a {@link Notation} factory method. For example: 1633 * 1634 * <pre> 1635 * NumberFormatter::with().notation(Notation::compactShort()) 1636 * </pre> 1637 * 1638 * The default is to use simple notation. 1639 * 1640 * @param notation 1641 * The notation strategy to use. 1642 * @return The fluent chain. 1643 * @see Notation 1644 * @stable ICU 60 1645 */ 1646 Derived notation(const Notation ¬ation) const &; 1647 1648 /** 1649 * Overload of notation() for use on an rvalue reference. 1650 * 1651 * @param notation 1652 * The notation strategy to use. 1653 * @return The fluent chain. 1654 * @see #notation 1655 * @stable ICU 62 1656 */ 1657 Derived notation(const Notation ¬ation) &&; 1658 1659 /** 1660 * Specifies the unit (unit of measure, currency, or percent) to associate with rendered numbers. 1661 * 1662 * <ul> 1663 * <li>Unit of measure: "12.3 meters" 1664 * <li>Currency: "$12.30" 1665 * <li>Percent: "12.3%" 1666 * </ul> 1667 * 1668 * All units will be properly localized with locale data, and all units are compatible with notation styles, 1669 * rounding precisions, and other number formatter settings. 1670 * 1671 * \note If the usage() is set, the output unit **will be changed** to 1672 * produce localised units, according to usage, locale and unit. See 1673 * FormattedNumber::getOutputUnit(). 1674 * 1675 * Pass this method any instance of {@link MeasureUnit}. For units of measure: 1676 * 1677 * <pre> 1678 * NumberFormatter::with().unit(MeasureUnit::getMeter()) 1679 * NumberFormatter::with().unit(MeasureUnit::forIdentifier("foot-per-second", status)) 1680 * </pre> 1681 * 1682 * Currency: 1683 * 1684 * <pre> 1685 * NumberFormatter::with().unit(CurrencyUnit(u"USD", status)) 1686 * </pre> 1687 * 1688 * Percent: 1689 * 1690 * <pre> 1691 * NumberFormatter::with().unit(NoUnit.percent()) 1692 * </pre> 1693 * 1694 * See {@link #perUnit} for information on how to format strings like "5 meters per second". 1695 * 1696 * The default is to render without units (equivalent to NoUnit.base()). 1697 * 1698 * @param unit 1699 * The unit to render. 1700 * @return The fluent chain. 1701 * @see MeasureUnit 1702 * @see Currency 1703 * @see NoUnit 1704 * @see #perUnit 1705 * @stable ICU 60 1706 */ 1707 Derived unit(const icu::MeasureUnit &unit) const &; 1708 1709 /** 1710 * Overload of unit() for use on an rvalue reference. 1711 * 1712 * @param unit 1713 * The unit to render. 1714 * @return The fluent chain. 1715 * @see #unit 1716 * @stable ICU 62 1717 */ 1718 Derived unit(const icu::MeasureUnit &unit) &&; 1719 1720 /** 1721 * Like unit(), but takes ownership of a pointer. Convenient for use with the MeasureFormat factory 1722 * methods that return pointers that need ownership. 1723 * 1724 * Note: consider using the MeasureFormat factory methods that return by value. 1725 * 1726 * @param unit 1727 * The unit to render. 1728 * @return The fluent chain. 1729 * @see #unit 1730 * @see MeasureUnit 1731 * @stable ICU 60 1732 */ 1733 Derived adoptUnit(icu::MeasureUnit *unit) const &; 1734 1735 /** 1736 * Overload of adoptUnit() for use on an rvalue reference. 1737 * 1738 * @param unit 1739 * The unit to render. 1740 * @return The fluent chain. 1741 * @see #adoptUnit 1742 * @stable ICU 62 1743 */ 1744 Derived adoptUnit(icu::MeasureUnit *unit) &&; 1745 1746 /** 1747 * Sets a unit to be used in the denominator. For example, to format "3 m/s", pass METER to the unit and SECOND to 1748 * the perUnit. 1749 * 1750 * Pass this method any instance of {@link MeasureUnit}. Example: 1751 * 1752 * <pre> 1753 * NumberFormatter::with() 1754 * .unit(MeasureUnit::getMeter()) 1755 * .perUnit(MeasureUnit::getSecond()) 1756 * </pre> 1757 * 1758 * The default is not to display any unit in the denominator. 1759 * 1760 * If a per-unit is specified without a primary unit via {@link #unit}, the behavior is undefined. 1761 * 1762 * @param perUnit 1763 * The unit to render in the denominator. 1764 * @return The fluent chain 1765 * @see #unit 1766 * @stable ICU 61 1767 */ 1768 Derived perUnit(const icu::MeasureUnit &perUnit) const &; 1769 1770 /** 1771 * Overload of perUnit() for use on an rvalue reference. 1772 * 1773 * @param perUnit 1774 * The unit to render in the denominator. 1775 * @return The fluent chain. 1776 * @see #perUnit 1777 * @stable ICU 62 1778 */ 1779 Derived perUnit(const icu::MeasureUnit &perUnit) &&; 1780 1781 /** 1782 * Like perUnit(), but takes ownership of a pointer. Convenient for use with the MeasureFormat factory 1783 * methods that return pointers that need ownership. 1784 * 1785 * Note: consider using the MeasureFormat factory methods that return by value. 1786 * 1787 * @param perUnit 1788 * The unit to render in the denominator. 1789 * @return The fluent chain. 1790 * @see #perUnit 1791 * @see MeasureUnit 1792 * @stable ICU 61 1793 */ 1794 Derived adoptPerUnit(icu::MeasureUnit *perUnit) const &; 1795 1796 /** 1797 * Overload of adoptPerUnit() for use on an rvalue reference. 1798 * 1799 * @param perUnit 1800 * The unit to render in the denominator. 1801 * @return The fluent chain. 1802 * @see #adoptPerUnit 1803 * @stable ICU 62 1804 */ 1805 Derived adoptPerUnit(icu::MeasureUnit *perUnit) &&; 1806 1807 /** 1808 * Specifies the rounding precision to use when formatting numbers. 1809 * 1810 * <ul> 1811 * <li>Round to 3 decimal places: "3.142" 1812 * <li>Round to 3 significant figures: "3.14" 1813 * <li>Round to the closest nickel: "3.15" 1814 * <li>Do not perform rounding: "3.1415926..." 1815 * </ul> 1816 * 1817 * <p> 1818 * Pass this method the return value of one of the factory methods on {@link Precision}. For example: 1819 * 1820 * <pre> 1821 * NumberFormatter::with().precision(Precision::fixedFraction(2)) 1822 * </pre> 1823 * 1824 * <p> 1825 * In most cases, the default rounding strategy is to round to 6 fraction places; i.e., 1826 * <code>Precision.maxFraction(6)</code>. The exceptions are if compact notation is being used, then the compact 1827 * notation rounding strategy is used (see {@link Notation#compactShort} for details), or if the unit is a currency, 1828 * then standard currency rounding is used, which varies from currency to currency (see {@link Precision#currency} for 1829 * details). 1830 * 1831 * @param precision 1832 * The rounding precision to use. 1833 * @return The fluent chain. 1834 * @see Precision 1835 * @stable ICU 62 1836 */ 1837 Derived precision(const Precision& precision) const &; 1838 1839 /** 1840 * Overload of precision() for use on an rvalue reference. 1841 * 1842 * @param precision 1843 * The rounding precision to use. 1844 * @return The fluent chain. 1845 * @see #precision 1846 * @stable ICU 62 1847 */ 1848 Derived precision(const Precision& precision) &&; 1849 1850 /** 1851 * Specifies how to determine the direction to round a number when it has more digits than fit in the 1852 * desired precision. When formatting 1.235: 1853 * 1854 * <ul> 1855 * <li>Ceiling rounding mode with integer precision: "2" 1856 * <li>Half-down rounding mode with 2 fixed fraction digits: "1.23" 1857 * <li>Half-up rounding mode with 2 fixed fraction digits: "1.24" 1858 * </ul> 1859 * 1860 * The default is HALF_EVEN. For more information on rounding mode, see the ICU userguide here: 1861 * 1862 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/rounding-modes 1863 * 1864 * @param roundingMode The rounding mode to use. 1865 * @return The fluent chain. 1866 * @stable ICU 62 1867 */ 1868 Derived roundingMode(UNumberFormatRoundingMode roundingMode) const &; 1869 1870 /** 1871 * Overload of roundingMode() for use on an rvalue reference. 1872 * 1873 * @param roundingMode The rounding mode to use. 1874 * @return The fluent chain. 1875 * @see #roundingMode 1876 * @stable ICU 62 1877 */ 1878 Derived roundingMode(UNumberFormatRoundingMode roundingMode) &&; 1879 1880 /** 1881 * Specifies the grouping strategy to use when formatting numbers. 1882 * 1883 * <ul> 1884 * <li>Default grouping: "12,300" and "1,230" 1885 * <li>Grouping with at least 2 digits: "12,300" and "1230" 1886 * <li>No grouping: "12300" and "1230" 1887 * </ul> 1888 * 1889 * <p> 1890 * The exact grouping widths will be chosen based on the locale. 1891 * 1892 * <p> 1893 * Pass this method an element from the {@link UNumberGroupingStrategy} enum. For example: 1894 * 1895 * <pre> 1896 * NumberFormatter::with().grouping(UNUM_GROUPING_MIN2) 1897 * </pre> 1898 * 1899 * The default is to perform grouping according to locale data; most locales, but not all locales, 1900 * enable it by default. 1901 * 1902 * @param strategy 1903 * The grouping strategy to use. 1904 * @return The fluent chain. 1905 * @stable ICU 61 1906 */ 1907 Derived grouping(UNumberGroupingStrategy strategy) const &; 1908 1909 /** 1910 * Overload of grouping() for use on an rvalue reference. 1911 * 1912 * @param strategy 1913 * The grouping strategy to use. 1914 * @return The fluent chain. 1915 * @see #grouping 1916 * @stable ICU 62 1917 */ 1918 Derived grouping(UNumberGroupingStrategy strategy) &&; 1919 1920 /** 1921 * Specifies the minimum and maximum number of digits to render before the decimal mark. 1922 * 1923 * <ul> 1924 * <li>Zero minimum integer digits: ".08" 1925 * <li>One minimum integer digit: "0.08" 1926 * <li>Two minimum integer digits: "00.08" 1927 * </ul> 1928 * 1929 * <p> 1930 * Pass this method the return value of {@link IntegerWidth#zeroFillTo}. For example: 1931 * 1932 * <pre> 1933 * NumberFormatter::with().integerWidth(IntegerWidth::zeroFillTo(2)) 1934 * </pre> 1935 * 1936 * The default is to have one minimum integer digit. 1937 * 1938 * @param style 1939 * The integer width to use. 1940 * @return The fluent chain. 1941 * @see IntegerWidth 1942 * @stable ICU 60 1943 */ 1944 Derived integerWidth(const IntegerWidth &style) const &; 1945 1946 /** 1947 * Overload of integerWidth() for use on an rvalue reference. 1948 * 1949 * @param style 1950 * The integer width to use. 1951 * @return The fluent chain. 1952 * @see #integerWidth 1953 * @stable ICU 62 1954 */ 1955 Derived integerWidth(const IntegerWidth &style) &&; 1956 1957 /** 1958 * Specifies the symbols (decimal separator, grouping separator, percent sign, numerals, etc.) to use when rendering 1959 * numbers. 1960 * 1961 * <ul> 1962 * <li><em>en_US</em> symbols: "12,345.67" 1963 * <li><em>fr_FR</em> symbols: "12 345,67" 1964 * <li><em>de_CH</em> symbols: "12’345.67" 1965 * <li><em>my_MY</em> symbols: "၁၂,၃၄၅.၆၇" 1966 * </ul> 1967 * 1968 * <p> 1969 * Pass this method an instance of {@link DecimalFormatSymbols}. For example: 1970 * 1971 * <pre> 1972 * NumberFormatter::with().symbols(DecimalFormatSymbols(Locale("de_CH"), status)) 1973 * </pre> 1974 * 1975 * <p> 1976 * <strong>Note:</strong> DecimalFormatSymbols automatically chooses the best numbering system based on the locale. 1977 * In the examples above, the first three are using the Latin numbering system, and the fourth is using the Myanmar 1978 * numbering system. 1979 * 1980 * <p> 1981 * <strong>Note:</strong> The instance of DecimalFormatSymbols will be copied: changes made to the symbols object 1982 * after passing it into the fluent chain will not be seen. 1983 * 1984 * <p> 1985 * <strong>Note:</strong> Calling this method will override any previously specified DecimalFormatSymbols 1986 * or NumberingSystem. 1987 * 1988 * <p> 1989 * The default is to choose the symbols based on the locale specified in the fluent chain. 1990 * 1991 * @param symbols 1992 * The DecimalFormatSymbols to use. 1993 * @return The fluent chain. 1994 * @see DecimalFormatSymbols 1995 * @stable ICU 60 1996 */ 1997 Derived symbols(const DecimalFormatSymbols &symbols) const &; 1998 1999 /** 2000 * Overload of symbols() for use on an rvalue reference. 2001 * 2002 * @param symbols 2003 * The DecimalFormatSymbols to use. 2004 * @return The fluent chain. 2005 * @see #symbols 2006 * @stable ICU 62 2007 */ 2008 Derived symbols(const DecimalFormatSymbols &symbols) &&; 2009 2010 /** 2011 * Specifies that the given numbering system should be used when fetching symbols. 2012 * 2013 * <ul> 2014 * <li>Latin numbering system: "12,345" 2015 * <li>Myanmar numbering system: "၁၂,၃၄၅" 2016 * <li>Math Sans Bold numbering system: "," 2017 * </ul> 2018 * 2019 * <p> 2020 * Pass this method an instance of {@link NumberingSystem}. For example, to force the locale to always use the Latin 2021 * alphabet numbering system (ASCII digits): 2022 * 2023 * <pre> 2024 * NumberFormatter::with().adoptSymbols(NumberingSystem::createInstanceByName("latn", status)) 2025 * </pre> 2026 * 2027 * <p> 2028 * <strong>Note:</strong> Calling this method will override any previously specified DecimalFormatSymbols 2029 * or NumberingSystem. 2030 * 2031 * <p> 2032 * The default is to choose the best numbering system for the locale. 2033 * 2034 * <p> 2035 * This method takes ownership of a pointer in order to work nicely with the NumberingSystem factory methods. 2036 * 2037 * @param symbols 2038 * The NumberingSystem to use. 2039 * @return The fluent chain. 2040 * @see NumberingSystem 2041 * @stable ICU 60 2042 */ 2043 Derived adoptSymbols(NumberingSystem *symbols) const &; 2044 2045 /** 2046 * Overload of adoptSymbols() for use on an rvalue reference. 2047 * 2048 * @param symbols 2049 * The NumberingSystem to use. 2050 * @return The fluent chain. 2051 * @see #adoptSymbols 2052 * @stable ICU 62 2053 */ 2054 Derived adoptSymbols(NumberingSystem *symbols) &&; 2055 2056 /** 2057 * Sets the width of the unit (measure unit or currency). Most common values: 2058 * 2059 * <ul> 2060 * <li>Short: "$12.00", "12 m" 2061 * <li>ISO Code: "USD 12.00" 2062 * <li>Full name: "12.00 US dollars", "12 meters" 2063 * </ul> 2064 * 2065 * <p> 2066 * Pass an element from the {@link UNumberUnitWidth} enum to this setter. For example: 2067 * 2068 * <pre> 2069 * NumberFormatter::with().unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME) 2070 * </pre> 2071 * 2072 * <p> 2073 * The default is the SHORT width. 2074 * 2075 * @param width 2076 * The width to use when rendering numbers. 2077 * @return The fluent chain 2078 * @see UNumberUnitWidth 2079 * @stable ICU 60 2080 */ 2081 Derived unitWidth(UNumberUnitWidth width) const &; 2082 2083 /** 2084 * Overload of unitWidth() for use on an rvalue reference. 2085 * 2086 * @param width 2087 * The width to use when rendering numbers. 2088 * @return The fluent chain. 2089 * @see #unitWidth 2090 * @stable ICU 62 2091 */ 2092 Derived unitWidth(UNumberUnitWidth width) &&; 2093 2094 /** 2095 * Sets the plus/minus sign display strategy. Most common values: 2096 * 2097 * <ul> 2098 * <li>Auto: "123", "-123" 2099 * <li>Always: "+123", "-123" 2100 * <li>Accounting: "$123", "($123)" 2101 * </ul> 2102 * 2103 * <p> 2104 * Pass an element from the {@link UNumberSignDisplay} enum to this setter. For example: 2105 * 2106 * <pre> 2107 * NumberFormatter::with().sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS) 2108 * </pre> 2109 * 2110 * <p> 2111 * The default is AUTO sign display. 2112 * 2113 * @param style 2114 * The sign display strategy to use when rendering numbers. 2115 * @return The fluent chain 2116 * @see UNumberSignDisplay 2117 * @stable ICU 60 2118 */ 2119 Derived sign(UNumberSignDisplay style) const &; 2120 2121 /** 2122 * Overload of sign() for use on an rvalue reference. 2123 * 2124 * @param style 2125 * The sign display strategy to use when rendering numbers. 2126 * @return The fluent chain. 2127 * @see #sign 2128 * @stable ICU 62 2129 */ 2130 Derived sign(UNumberSignDisplay style) &&; 2131 2132 /** 2133 * Sets the decimal separator display strategy. This affects integer numbers with no fraction part. Most common 2134 * values: 2135 * 2136 * <ul> 2137 * <li>Auto: "1" 2138 * <li>Always: "1." 2139 * </ul> 2140 * 2141 * <p> 2142 * Pass an element from the {@link UNumberDecimalSeparatorDisplay} enum to this setter. For example: 2143 * 2144 * <pre> 2145 * NumberFormatter::with().decimal(UNumberDecimalSeparatorDisplay::UNUM_DECIMAL_SEPARATOR_ALWAYS) 2146 * </pre> 2147 * 2148 * <p> 2149 * The default is AUTO decimal separator display. 2150 * 2151 * @param style 2152 * The decimal separator display strategy to use when rendering numbers. 2153 * @return The fluent chain 2154 * @see UNumberDecimalSeparatorDisplay 2155 * @stable ICU 60 2156 */ 2157 Derived decimal(UNumberDecimalSeparatorDisplay style) const &; 2158 2159 /** 2160 * Overload of decimal() for use on an rvalue reference. 2161 * 2162 * @param style 2163 * The decimal separator display strategy to use when rendering numbers. 2164 * @return The fluent chain. 2165 * @see #decimal 2166 * @stable ICU 62 2167 */ 2168 Derived decimal(UNumberDecimalSeparatorDisplay style) &&; 2169 2170 /** 2171 * Sets a scale (multiplier) to be used to scale the number by an arbitrary amount before formatting. 2172 * Most common values: 2173 * 2174 * <ul> 2175 * <li>Multiply by 100: useful for percentages. 2176 * <li>Multiply by an arbitrary value: useful for unit conversions. 2177 * </ul> 2178 * 2179 * <p> 2180 * Pass an element from a {@link Scale} factory method to this setter. For example: 2181 * 2182 * <pre> 2183 * NumberFormatter::with().scale(Scale::powerOfTen(2)) 2184 * </pre> 2185 * 2186 * <p> 2187 * The default is to not apply any multiplier. 2188 * 2189 * @param scale 2190 * The scale to apply when rendering numbers. 2191 * @return The fluent chain 2192 * @stable ICU 62 2193 */ 2194 Derived scale(const Scale &scale) const &; 2195 2196 /** 2197 * Overload of scale() for use on an rvalue reference. 2198 * 2199 * @param scale 2200 * The scale to apply when rendering numbers. 2201 * @return The fluent chain. 2202 * @see #scale 2203 * @stable ICU 62 2204 */ 2205 Derived scale(const Scale &scale) &&; 2206 2207 /** 2208 * Specifies the usage for which numbers will be formatted ("person-height", 2209 * "road", "rainfall", etc.) 2210 * 2211 * When a `usage` is specified, the output unit will change depending on the 2212 * `Locale` and the unit quantity. For example, formatting length 2213 * measurements specified in meters: 2214 * 2215 * `NumberFormatter::with().usage("person").unit(MeasureUnit::getMeter()).locale("en-US")` 2216 * * When formatting 0.25, the output will be "10 inches". 2217 * * When formatting 1.50, the output will be "4 feet and 11 inches". 2218 * 2219 * The input unit specified via unit() determines the type of measurement 2220 * being formatted (e.g. "length" when the unit is "foot"). The usage 2221 * requested will be looked for only within this category of measurement 2222 * units. 2223 * 2224 * The output unit can be found via FormattedNumber::getOutputUnit(). 2225 * 2226 * If the usage has multiple parts (e.g. "land-agriculture-grain") and does 2227 * not match a known usage preference, the last part will be dropped 2228 * repeatedly until a match is found (e.g. trying "land-agriculture", then 2229 * "land"). If a match is still not found, usage will fall back to 2230 * "default". 2231 * 2232 * Setting usage to an empty string clears the usage (disables usage-based 2233 * localized formatting). 2234 * 2235 * Setting a usage string but not a correct input unit will result in an 2236 * U_ILLEGAL_ARGUMENT_ERROR. 2237 * 2238 * When using usage, specifying rounding or precision is unnecessary. 2239 * Specifying a precision in some manner will override the default 2240 * formatting. 2241 * 2242 * @param usage A `usage` parameter from the units resource. See the 2243 * unitPreferenceData in *source/data/misc/units.txt*, generated from 2244 * `unitPreferenceData` in [CLDR's 2245 * supplemental/units.xml](https://github.com/unicode-org/cldr/blob/main/common/supplemental/units.xml). 2246 * @return The fluent chain. 2247 * @stable ICU 68 2248 */ 2249 Derived usage(StringPiece usage) const &; 2250 2251 /** 2252 * Overload of usage() for use on an rvalue reference. 2253 * 2254 * @param usage The unit `usage`. 2255 * @return The fluent chain. 2256 * @stable ICU 68 2257 */ 2258 Derived usage(StringPiece usage) &&; 2259 2260 #ifndef U_HIDE_DRAFT_API 2261 /** 2262 * Specifies the DisplayOptions. For example, UDisplayOptionsGrammaticalCase specifies 2263 * the desired case for a unit formatter's output (e.g. accusative, dative, genitive). 2264 * 2265 * @param displayOptions 2266 * @return The fluent chain. 2267 * @draft ICU 72 2268 */ 2269 Derived displayOptions(const DisplayOptions &displayOptions) const &; 2270 2271 /** 2272 * Overload of displayOptions() for use on an rvalue reference. 2273 * 2274 * @param displayOptions 2275 * @return The fluent chain. 2276 * @draft ICU 72 2277 */ 2278 Derived displayOptions(const DisplayOptions &displayOptions) &&; 2279 #endif // U_HIDE_DRAFT_API 2280 2281 #ifndef U_HIDE_INTERNAL_API 2282 /** 2283 * NOTE: Use `displayOptions` instead. This method was part of 2284 * an internal technology preview in ICU 69, but will be removed 2285 * in ICU 73, in favor of `displayOptions` 2286 * 2287 * Specifies the desired case for a unit formatter's output (e.g. 2288 * accusative, dative, genitive). 2289 * 2290 * @internal 2291 */ 2292 Derived unitDisplayCase(StringPiece unitDisplayCase) const &; 2293 2294 /** 2295 * NOTE: Use `displayOptions` instead. This method was part of 2296 * an internal technology preview in ICU 69, but will be removed 2297 * in ICU 73, in favor of `displayOptions` 2298 * 2299 * Overload of unitDisplayCase() for use on an rvalue reference. 2300 * 2301 * @internal 2302 */ 2303 Derived unitDisplayCase(StringPiece unitDisplayCase) &&; 2304 #endif // U_HIDE_INTERNAL_API 2305 2306 #ifndef U_HIDE_INTERNAL_API 2307 2308 /** 2309 * Set the padding strategy. May be added in the future; see #13338. 2310 * 2311 * @internal ICU 60: This API is ICU internal only. 2312 */ 2313 Derived padding(const impl::Padder &padder) const &; 2314 2315 /** @internal */ 2316 Derived padding(const impl::Padder &padder) &&; 2317 2318 /** 2319 * Internal fluent setter to support a custom regulation threshold. A threshold of 1 causes the data structures to 2320 * be built right away. A threshold of 0 prevents the data structures from being built. 2321 * 2322 * @internal ICU 60: This API is ICU internal only. 2323 */ 2324 Derived threshold(int32_t threshold) const &; 2325 2326 /** @internal */ 2327 Derived threshold(int32_t threshold) &&; 2328 2329 /** 2330 * Internal fluent setter to overwrite the entire macros object. 2331 * 2332 * @internal ICU 60: This API is ICU internal only. 2333 */ 2334 Derived macros(const impl::MacroProps& macros) const &; 2335 2336 /** @internal */ 2337 Derived macros(const impl::MacroProps& macros) &&; 2338 2339 /** @internal */ 2340 Derived macros(impl::MacroProps&& macros) const &; 2341 2342 /** @internal */ 2343 Derived macros(impl::MacroProps&& macros) &&; 2344 2345 #endif /* U_HIDE_INTERNAL_API */ 2346 2347 /** 2348 * Creates a skeleton string representation of this number formatter. A skeleton string is a 2349 * locale-agnostic serialized form of a number formatter. 2350 * 2351 * Not all options are capable of being represented in the skeleton string; for example, a 2352 * DecimalFormatSymbols object. If any such option is encountered, the error code is set to 2353 * U_UNSUPPORTED_ERROR. 2354 * 2355 * The returned skeleton is in normalized form, such that two number formatters with equivalent 2356 * behavior should produce the same skeleton. 2357 * 2358 * For more information on number skeleton strings, see: 2359 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html 2360 * 2361 * @return A number skeleton string with behavior corresponding to this number formatter. 2362 * @stable ICU 62 2363 */ 2364 UnicodeString toSkeleton(UErrorCode& status) const; 2365 2366 /** 2367 * Returns the current (Un)LocalizedNumberFormatter as a LocalPointer 2368 * wrapping a heap-allocated copy of the current object. 2369 * 2370 * This is equivalent to new-ing the move constructor with a value object 2371 * as the argument. 2372 * 2373 * @return A wrapped (Un)LocalizedNumberFormatter pointer, or a wrapped 2374 * nullptr on failure. 2375 * @stable ICU 64 2376 */ 2377 LocalPointer<Derived> clone() const &; 2378 2379 /** 2380 * Overload of clone for use on an rvalue reference. 2381 * 2382 * @return A wrapped (Un)LocalizedNumberFormatter pointer, or a wrapped 2383 * nullptr on failure. 2384 * @stable ICU 64 2385 */ 2386 LocalPointer<Derived> clone() &&; 2387 2388 /** 2389 * Sets the UErrorCode if an error occurred in the fluent chain. 2390 * Preserves older error codes in the outErrorCode. 2391 * @return true if U_FAILURE(outErrorCode) 2392 * @stable ICU 60 2393 */ copyErrorTo(UErrorCode & outErrorCode)2394 UBool copyErrorTo(UErrorCode &outErrorCode) const { 2395 if (U_FAILURE(outErrorCode)) { 2396 // Do not overwrite the older error code 2397 return true; 2398 } 2399 fMacros.copyErrorTo(outErrorCode); 2400 return U_FAILURE(outErrorCode); 2401 } 2402 2403 // NOTE: Uses default copy and move constructors. 2404 2405 private: 2406 impl::MacroProps fMacros; 2407 2408 // Don't construct me directly! Use (Un)LocalizedNumberFormatter. 2409 NumberFormatterSettings() = default; 2410 2411 friend class LocalizedNumberFormatter; 2412 friend class UnlocalizedNumberFormatter; 2413 2414 // Give NumberRangeFormatter access to the MacroProps 2415 friend void impl::touchRangeLocales(impl::RangeMacroProps& macros); 2416 friend class impl::NumberRangeFormatterImpl; 2417 }; 2418 2419 // Explicit instantiations in source/i18n/number_fluent.cpp. 2420 // (MSVC treats imports/exports of explicit instantiations differently.) 2421 #ifndef _MSC_VER 2422 extern template class NumberFormatterSettings<UnlocalizedNumberFormatter>; 2423 extern template class NumberFormatterSettings<LocalizedNumberFormatter>; 2424 #endif 2425 2426 /** 2427 * A NumberFormatter that does not yet have a locale. In order to format numbers, a locale must be specified. 2428 * 2429 * Instances of this class are immutable and thread-safe. 2430 * 2431 * @see NumberFormatter 2432 * @stable ICU 60 2433 */ 2434 class U_I18N_API UnlocalizedNumberFormatter 2435 : public NumberFormatterSettings<UnlocalizedNumberFormatter>, public UMemory { 2436 2437 public: 2438 /** 2439 * Associate the given locale with the number formatter. The locale is used for picking the appropriate symbols, 2440 * formats, and other data for number display. 2441 * 2442 * @param locale 2443 * The locale to use when loading data for number formatting. 2444 * @return The fluent chain. 2445 * @stable ICU 60 2446 */ 2447 LocalizedNumberFormatter locale(const icu::Locale &locale) const &; 2448 2449 /** 2450 * Overload of locale() for use on an rvalue reference. 2451 * 2452 * @param locale 2453 * The locale to use when loading data for number formatting. 2454 * @return The fluent chain. 2455 * @see #locale 2456 * @stable ICU 62 2457 */ 2458 LocalizedNumberFormatter locale(const icu::Locale &locale) &&; 2459 2460 /** 2461 * Default constructor: puts the formatter into a valid but undefined state. 2462 * 2463 * @stable ICU 62 2464 */ 2465 UnlocalizedNumberFormatter() = default; 2466 2467 /** 2468 * Returns a copy of this UnlocalizedNumberFormatter. 2469 * @stable ICU 60 2470 */ 2471 UnlocalizedNumberFormatter(const UnlocalizedNumberFormatter &other); 2472 2473 /** 2474 * Move constructor: 2475 * The source UnlocalizedNumberFormatter will be left in a valid but undefined state. 2476 * @stable ICU 62 2477 */ 2478 UnlocalizedNumberFormatter(UnlocalizedNumberFormatter&& src) noexcept; 2479 2480 /** 2481 * Copy assignment operator. 2482 * @stable ICU 62 2483 */ 2484 UnlocalizedNumberFormatter& operator=(const UnlocalizedNumberFormatter& other); 2485 2486 /** 2487 * Move assignment operator: 2488 * The source UnlocalizedNumberFormatter will be left in a valid but undefined state. 2489 * @stable ICU 62 2490 */ 2491 UnlocalizedNumberFormatter& operator=(UnlocalizedNumberFormatter&& src) noexcept; 2492 2493 private: 2494 explicit UnlocalizedNumberFormatter(const NumberFormatterSettings<UnlocalizedNumberFormatter>& other); 2495 2496 explicit UnlocalizedNumberFormatter( 2497 NumberFormatterSettings<UnlocalizedNumberFormatter>&& src) noexcept; 2498 2499 // To give the fluent setters access to this class's constructor: 2500 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>; 2501 2502 // To give NumberFormatter::with() access to this class's constructor: 2503 friend class NumberFormatter; 2504 }; 2505 2506 /** 2507 * A NumberFormatter that has a locale associated with it; this means .format() methods are available. 2508 * 2509 * Instances of this class are immutable and thread-safe. 2510 * 2511 * @see NumberFormatter 2512 * @stable ICU 60 2513 */ 2514 class U_I18N_API LocalizedNumberFormatter 2515 : public NumberFormatterSettings<LocalizedNumberFormatter>, public UMemory { 2516 public: 2517 /** 2518 * Format the given integer number to a string using the settings specified in the NumberFormatter fluent 2519 * setting chain. 2520 * 2521 * @param value 2522 * The number to format. 2523 * @param status 2524 * Set to an ErrorCode if one occurred in the setter chain or during formatting. 2525 * @return A FormattedNumber object; call .toString() to get the string. 2526 * @stable ICU 60 2527 */ 2528 FormattedNumber formatInt(int64_t value, UErrorCode &status) const; 2529 2530 /** 2531 * Format the given float or double to a string using the settings specified in the NumberFormatter fluent setting 2532 * chain. 2533 * 2534 * @param value 2535 * The number to format. 2536 * @param status 2537 * Set to an ErrorCode if one occurred in the setter chain or during formatting. 2538 * @return A FormattedNumber object; call .toString() to get the string. 2539 * @stable ICU 60 2540 */ 2541 FormattedNumber formatDouble(double value, UErrorCode &status) const; 2542 2543 /** 2544 * Format the given decimal number to a string using the settings 2545 * specified in the NumberFormatter fluent setting chain. 2546 * The syntax of the unformatted number is a "numeric string" 2547 * as defined in the Decimal Arithmetic Specification, available at 2548 * http://speleotrove.com/decimal 2549 * 2550 * @param value 2551 * The number to format. 2552 * @param status 2553 * Set to an ErrorCode if one occurred in the setter chain or during formatting. 2554 * @return A FormattedNumber object; call .toString() to get the string. 2555 * @stable ICU 60 2556 */ 2557 FormattedNumber formatDecimal(StringPiece value, UErrorCode& status) const; 2558 2559 #ifndef U_HIDE_INTERNAL_API 2560 2561 2562 /** 2563 * @internal 2564 */ 2565 const DecimalFormatSymbols* getDecimalFormatSymbols() const; 2566 2567 /** Internal method. 2568 * @internal 2569 */ 2570 FormattedNumber formatDecimalQuantity(const impl::DecimalQuantity& dq, UErrorCode& status) const; 2571 2572 /** Internal method for DecimalFormat compatibility. 2573 * @internal 2574 */ 2575 void getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, UErrorCode& status) const; 2576 2577 /** 2578 * Internal method for testing. 2579 * @internal 2580 */ 2581 const impl::NumberFormatterImpl* getCompiled() const; 2582 2583 /** 2584 * Internal method for testing. 2585 * @internal 2586 */ 2587 int32_t getCallCount() const; 2588 2589 #endif /* U_HIDE_INTERNAL_API */ 2590 2591 /** 2592 * Creates a representation of this LocalizedNumberFormat as an icu::Format, enabling the use 2593 * of this number formatter with APIs that need an object of that type, such as MessageFormat. 2594 * 2595 * This API is not intended to be used other than for enabling API compatibility. The formatDouble, 2596 * formatInt, and formatDecimal methods should normally be used when formatting numbers, not the Format 2597 * object returned by this method. 2598 * 2599 * The caller owns the returned object and must delete it when finished. 2600 * 2601 * @return A Format wrapping this LocalizedNumberFormatter. 2602 * @stable ICU 62 2603 */ 2604 Format* toFormat(UErrorCode& status) const; 2605 2606 /** 2607 * Default constructor: puts the formatter into a valid but undefined state. 2608 * 2609 * @stable ICU 62 2610 */ 2611 LocalizedNumberFormatter() = default; 2612 2613 /** 2614 * Returns a copy of this LocalizedNumberFormatter. 2615 * @stable ICU 60 2616 */ 2617 LocalizedNumberFormatter(const LocalizedNumberFormatter &other); 2618 2619 /** 2620 * Move constructor: 2621 * The source LocalizedNumberFormatter will be left in a valid but undefined state. 2622 * @stable ICU 62 2623 */ 2624 LocalizedNumberFormatter(LocalizedNumberFormatter&& src) noexcept; 2625 2626 /** 2627 * Copy assignment operator. 2628 * @stable ICU 62 2629 */ 2630 LocalizedNumberFormatter& operator=(const LocalizedNumberFormatter& other); 2631 2632 /** 2633 * Move assignment operator: 2634 * The source LocalizedNumberFormatter will be left in a valid but undefined state. 2635 * @stable ICU 62 2636 */ 2637 LocalizedNumberFormatter& operator=(LocalizedNumberFormatter&& src) noexcept; 2638 2639 #ifndef U_HIDE_INTERNAL_API 2640 2641 /** 2642 * This is the core entrypoint to the number formatting pipeline. It performs self-regulation: a static code path 2643 * for the first few calls, and compiling a more efficient data structure if called repeatedly. 2644 * 2645 * <p> 2646 * This function is very hot, being called in every call to the number formatting pipeline. 2647 * 2648 * @param results 2649 * The results object. This method will mutate it to save the results. 2650 * @param status 2651 * @internal 2652 */ 2653 void formatImpl(impl::UFormattedNumberData *results, UErrorCode &status) const; 2654 2655 #endif /* U_HIDE_INTERNAL_API */ 2656 2657 /** 2658 * Destruct this LocalizedNumberFormatter, cleaning up any memory it might own. 2659 * @stable ICU 60 2660 */ 2661 ~LocalizedNumberFormatter(); 2662 2663 private: 2664 // Note: fCompiled can't be a LocalPointer because impl::NumberFormatterImpl is defined in an internal 2665 // header, and LocalPointer needs the full class definition in order to delete the instance. 2666 const impl::NumberFormatterImpl* fCompiled {nullptr}; 2667 char fUnsafeCallCount[8] {}; // internally cast to u_atomic_int32_t 2668 2669 // Owned pointer to a DecimalFormatWarehouse, used when copying a LocalizedNumberFormatter 2670 // from a DecimalFormat. 2671 const impl::DecimalFormatWarehouse* fWarehouse {nullptr}; 2672 2673 explicit LocalizedNumberFormatter(const NumberFormatterSettings<LocalizedNumberFormatter>& other); 2674 2675 explicit LocalizedNumberFormatter(NumberFormatterSettings<LocalizedNumberFormatter>&& src) noexcept; 2676 2677 LocalizedNumberFormatter(const impl::MacroProps ¯os, const Locale &locale); 2678 2679 LocalizedNumberFormatter(impl::MacroProps &¯os, const Locale &locale); 2680 2681 void resetCompiled(); 2682 2683 void lnfMoveHelper(LocalizedNumberFormatter&& src); 2684 2685 void lnfCopyHelper(const LocalizedNumberFormatter& src, UErrorCode& status); 2686 2687 /** 2688 * @return true if the compiled formatter is available. 2689 */ 2690 bool computeCompiled(UErrorCode& status) const; 2691 2692 // To give the fluent setters access to this class's constructor: 2693 friend class NumberFormatterSettings<UnlocalizedNumberFormatter>; 2694 friend class NumberFormatterSettings<LocalizedNumberFormatter>; 2695 2696 // To give UnlocalizedNumberFormatter::locale() access to this class's constructor: 2697 friend class UnlocalizedNumberFormatter; 2698 }; 2699 2700 #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER) 2701 // Warning 4661. 2702 #pragma warning(pop) 2703 #endif 2704 2705 /** 2706 * See the main description in numberformatter.h for documentation and examples. 2707 * 2708 * @stable ICU 60 2709 */ 2710 class U_I18N_API NumberFormatter final { 2711 public: 2712 /** 2713 * Call this method at the beginning of a NumberFormatter fluent chain in which the locale is not currently known at 2714 * the call site. 2715 * 2716 * @return An {@link UnlocalizedNumberFormatter}, to be used for chaining. 2717 * @stable ICU 60 2718 */ 2719 static UnlocalizedNumberFormatter with(); 2720 2721 /** 2722 * Call this method at the beginning of a NumberFormatter fluent chain in which the locale is known at the call 2723 * site. 2724 * 2725 * @param locale 2726 * The locale from which to load formats and symbols for number formatting. 2727 * @return A {@link LocalizedNumberFormatter}, to be used for chaining. 2728 * @stable ICU 60 2729 */ 2730 static LocalizedNumberFormatter withLocale(const Locale &locale); 2731 2732 /** 2733 * Call this method at the beginning of a NumberFormatter fluent chain to create an instance based 2734 * on a given number skeleton string. 2735 * 2736 * It is possible for an error to occur while parsing. See the overload of this method if you are 2737 * interested in the location of a possible parse error. 2738 * 2739 * For more information on number skeleton strings, see: 2740 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html 2741 * 2742 * @param skeleton 2743 * The skeleton string off of which to base this NumberFormatter. 2744 * @param status 2745 * Set to U_NUMBER_SKELETON_SYNTAX_ERROR if the skeleton was invalid. 2746 * @return An UnlocalizedNumberFormatter, to be used for chaining. 2747 * @stable ICU 62 2748 */ 2749 static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton, UErrorCode& status); 2750 2751 /** 2752 * Call this method at the beginning of a NumberFormatter fluent chain to create an instance based 2753 * on a given number skeleton string. 2754 * 2755 * If an error occurs while parsing the skeleton string, the offset into the skeleton string at 2756 * which the error occurred will be saved into the UParseError, if provided. 2757 * 2758 * For more information on number skeleton strings, see: 2759 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html 2760 * 2761 * @param skeleton 2762 * The skeleton string off of which to base this NumberFormatter. 2763 * @param perror 2764 * A parse error struct populated if an error occurs when parsing. 2765 * If no error occurs, perror.offset will be set to -1. 2766 * @param status 2767 * Set to U_NUMBER_SKELETON_SYNTAX_ERROR if the skeleton was invalid. 2768 * @return An UnlocalizedNumberFormatter, to be used for chaining. 2769 * @stable ICU 64 2770 */ 2771 static UnlocalizedNumberFormatter forSkeleton(const UnicodeString& skeleton, 2772 UParseError& perror, UErrorCode& status); 2773 2774 /** 2775 * Use factory methods instead of the constructor to create a NumberFormatter. 2776 */ 2777 NumberFormatter() = delete; 2778 }; 2779 2780 } // namespace number 2781 U_NAMESPACE_END 2782 2783 #endif /* #if !UCONFIG_NO_FORMATTING */ 2784 2785 #endif /* U_SHOW_CPLUSPLUS_API */ 2786 2787 #endif // __NUMBERFORMATTER_H__ 2788