ICU 76.1  76.1
simplenumberformatter.h
Go to the documentation of this file.
1 // © 2022 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #ifndef __SIMPLENUMBERFORMATTERH__
5 #define __SIMPLENUMBERFORMATTERH__
6 
7 #include "unicode/utypes.h"
8 
9 #if U_SHOW_CPLUSPLUS_API
10 
11 #if !UCONFIG_NO_FORMATTING
12 
13 #include "unicode/dcfmtsym.h"
16 
31 U_NAMESPACE_BEGIN
32 
33 /* forward declaration */
34 class SimpleDateFormat;
35 
36 namespace number { // icu::number
37 
38 
39 namespace impl {
40 class UFormattedNumberData;
41 struct SimpleMicroProps;
42 class AdoptingSignumModifierStore;
43 } // icu::number::impl
44 
45 
54  public:
60  static SimpleNumber forInt64(int64_t value, UErrorCode& status);
61 
69  void multiplyByPowerOfTen(int32_t power, UErrorCode& status);
70 
79  void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode& status);
80 
81 #ifndef U_HIDE_DRAFT_API
82 
87  void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode& status);
88 #endif // U_HIDE_DRAFT_API
89 
95  void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
96 
102  void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
103 
113  void setSign(USimpleNumberSign sign, UErrorCode& status);
114 
122  SimpleNumber() = default;
123 
130  cleanup();
131  }
132 
138  SimpleNumber(SimpleNumber&& other) noexcept {
139  fData = other.fData;
140  fSign = other.fSign;
141  other.fData = nullptr;
142  }
143 
149  SimpleNumber& operator=(SimpleNumber&& other) noexcept {
150  cleanup();
151  fData = other.fData;
152  fSign = other.fSign;
153  other.fData = nullptr;
154  return *this;
155  }
156 
157  private:
158  SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
159  SimpleNumber(const SimpleNumber&) = delete;
160  SimpleNumber& operator=(const SimpleNumber&) = delete;
161 
162  void cleanup();
163 
164  impl::UFormattedNumberData* fData = nullptr;
166 
167  friend class SimpleNumberFormatter;
168 
169  // Uses the private constructor to avoid a heap allocation
170  friend class icu::SimpleDateFormat;
171 };
172 
173 
185  public:
191  static SimpleNumberFormatter forLocale(
192  const icu::Locale &locale,
193  UErrorCode &status);
194 
200  static SimpleNumberFormatter forLocaleAndGroupingStrategy(
201  const icu::Locale &locale,
202  UNumberGroupingStrategy groupingStrategy,
203  UErrorCode &status);
204 
213  static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
214  const icu::Locale &locale,
215  const DecimalFormatSymbols &symbols,
216  UNumberGroupingStrategy groupingStrategy,
217  UErrorCode &status);
218 
227  FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
228 
236  FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
237  return format(SimpleNumber::forInt64(value, status), status);
238  }
239 
240 #ifndef U_HIDE_INTERNAL_API
241 
245  void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
246 #endif // U_HIDE_INTERNAL_API
247 
254  cleanup();
255  }
256 
262  SimpleNumberFormatter() = default;
263 
270  fGroupingStrategy = other.fGroupingStrategy;
271  fOwnedSymbols = other.fOwnedSymbols;
272  fMicros = other.fMicros;
273  fPatternModifier = other.fPatternModifier;
274  other.fOwnedSymbols = nullptr;
275  other.fMicros = nullptr;
276  other.fPatternModifier = nullptr;
277  }
278 
285  cleanup();
286  fGroupingStrategy = other.fGroupingStrategy;
287  fOwnedSymbols = other.fOwnedSymbols;
288  fMicros = other.fMicros;
289  fPatternModifier = other.fPatternModifier;
290  other.fOwnedSymbols = nullptr;
291  other.fMicros = nullptr;
292  other.fPatternModifier = nullptr;
293  return *this;
294  }
295 
296  private:
297  void initialize(
298  const icu::Locale &locale,
299  const DecimalFormatSymbols &symbols,
300  UNumberGroupingStrategy groupingStrategy,
301  UErrorCode &status);
302 
303  void cleanup();
304 
306 
307  SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
308 
309  UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
310 
311  // Owned Pointers:
312  DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
313  impl::SimpleMicroProps* fMicros = nullptr;
314  impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
315 };
316 
317 
318 } // namespace number
319 U_NAMESPACE_END
320 
321 #endif /* #if !UCONFIG_NO_FORMATTING */
322 
323 #endif /* U_SHOW_CPLUSPLUS_API */
324 
325 #endif // __SIMPLENUMBERFORMATTERH__
326 
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:86
SimpleNumber & operator=(SimpleNumber &&other) noexcept
SimpleNumber move assignment.
C API: Simple number formatting focused on low memory and code size.
FormattedNumber formatInt64(int64_t value, UErrorCode &status) const
Formats an integer using this SimpleNumberFormatter.
UNumberGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i.e., the separator, often a comma or period, after every 2-3 powers of ten).
C API: Formatted number result from various number formatting functions.
SimpleNumberFormatter(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move constructor.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
~SimpleNumber()
Destruct this SimpleNumber, cleaning up any memory it might own.
SimpleNumberFormatter & operator=(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move assignment.
~SimpleNumberFormatter()
Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
UNumberFormatRoundingMode
The possible number format rounding modes.
An input type for SimpleNumberFormatter.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:385
C++ API: Symbols for formatting numbers.
Basic definitions for ICU, for both C and C++ APIs.
USimpleNumberSign
An explicit sign option for a SimpleNumber.
Display grouping using the default strategy for all locales.
The result of a number formatting operation.
SimpleNumber(SimpleNumber &&other) noexcept
SimpleNumber move constructor.
UMemory is the common ICU base class.
Definition: uobject.h:115
A special NumberFormatter focused on smaller binary size and memory use.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195