number_asformat.h (3657B)
1 // © 2017 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 4 #include "unicode/utypes.h" 5 6 #if !UCONFIG_NO_FORMATTING 7 #ifndef __NUMBER_ASFORMAT_H__ 8 #define __NUMBER_ASFORMAT_H__ 9 10 #include "unicode/numberformatter.h" 11 #include "number_types.h" 12 #include "number_decimalquantity.h" 13 #include "number_scientific.h" 14 #include "number_patternstring.h" 15 #include "number_modifiers.h" 16 #include "number_multiplier.h" 17 #include "number_roundingutils.h" 18 #include "decNumber.h" 19 #include "charstr.h" 20 21 U_NAMESPACE_BEGIN 22 namespace number::impl { 23 24 /** 25 * A wrapper around LocalizedNumberFormatter implementing the Format interface, enabling improved 26 * compatibility with other APIs. 27 * 28 * @see NumberFormatter 29 */ 30 class U_I18N_API_CLASS LocalizedNumberFormatterAsFormat : public Format { 31 public: 32 U_I18N_API LocalizedNumberFormatterAsFormat(const LocalizedNumberFormatter& formatter, 33 const Locale& locale); 34 35 /** 36 * Destructor. 37 */ 38 U_I18N_API ~LocalizedNumberFormatterAsFormat() override; 39 40 /** 41 * Equals operator. 42 */ 43 U_I18N_API bool operator==(const Format& other) const override; 44 45 /** 46 * Creates a copy of this object. 47 */ 48 U_I18N_API LocalizedNumberFormatterAsFormat* clone() const override; 49 50 /** 51 * Formats a Number using the wrapped LocalizedNumberFormatter. The provided formattable must be a 52 * number type. 53 */ 54 U_I18N_API UnicodeString& format(const Formattable& obj, 55 UnicodeString& appendTo, 56 FieldPosition& pos, 57 UErrorCode& status) const override; 58 59 /** 60 * Formats a Number using the wrapped LocalizedNumberFormatter. The provided formattable must be a 61 * number type. 62 */ 63 U_I18N_API UnicodeString& format(const Formattable& obj, 64 UnicodeString& appendTo, 65 FieldPositionIterator* posIter, 66 UErrorCode& status) const override; 67 68 /** 69 * Not supported: sets an error index and returns. 70 */ 71 U_I18N_API void parseObject(const UnicodeString& source, 72 Formattable& result, 73 ParsePosition& parse_pos) const override; 74 75 /** 76 * Gets the LocalizedNumberFormatter that this wrapper class uses to format numbers. 77 * 78 * For maximum efficiency, this function returns by const reference. You must copy the return value 79 * into a local variable if you want to use it beyond the lifetime of the current object: 80 * 81 * <pre> 82 * LocalizedNumberFormatter localFormatter = fmt->getNumberFormatter(); 83 * </pre> 84 * 85 * You can however use the return value directly when chaining: 86 * 87 * <pre> 88 * FormattedNumber result = fmt->getNumberFormatter().formatDouble(514.23, status); 89 * </pre> 90 * 91 * @return The unwrapped LocalizedNumberFormatter. 92 */ 93 U_I18N_API const LocalizedNumberFormatter& getNumberFormatter() const; 94 95 U_I18N_API UClassID getDynamicClassID() const override; 96 U_I18N_API static UClassID getStaticClassID(); 97 98 private: 99 LocalizedNumberFormatter fFormatter; 100 101 // Even though the locale is inside the LocalizedNumberFormatter, we have to keep it here, too, because 102 // LocalizedNumberFormatter doesn't have a getLocale() method, and ICU-TC didn't want to add one. 103 Locale fLocale; 104 }; 105 106 } // namespace number::impl 107 U_NAMESPACE_END 108 109 #endif // __NUMBER_ASFORMAT_H__ 110 111 #endif /* #if !UCONFIG_NO_FORMATTING */