ICU 77.1  77.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
86  void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode& status);
87 
93  void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
94 
100  void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
101 
111  void setSign(USimpleNumberSign sign, UErrorCode& status);
112 
120  SimpleNumber() = default;
121 
128  cleanup();
129  }
130 
136  SimpleNumber(SimpleNumber&& other) noexcept {
137  fData = other.fData;
138  fSign = other.fSign;
139  other.fData = nullptr;
140  }
141 
147  SimpleNumber& operator=(SimpleNumber&& other) noexcept {
148  cleanup();
149  fData = other.fData;
150  fSign = other.fSign;
151  other.fData = nullptr;
152  return *this;
153  }
154 
155  private:
156  SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
157  SimpleNumber(const SimpleNumber&) = delete;
158  SimpleNumber& operator=(const SimpleNumber&) = delete;
159 
160  void cleanup();
161 
162  impl::UFormattedNumberData* fData = nullptr;
164 
165  friend class SimpleNumberFormatter;
166 
167  // Uses the private constructor to avoid a heap allocation
168  friend class icu::SimpleDateFormat;
169 };
170 
171 
183  public:
190  const icu::Locale &locale,
191  UErrorCode &status);
192 
199  const icu::Locale &locale,
200  UNumberGroupingStrategy groupingStrategy,
201  UErrorCode &status);
202 
212  const icu::Locale &locale,
213  const DecimalFormatSymbols &symbols,
214  UNumberGroupingStrategy groupingStrategy,
215  UErrorCode &status);
216 
226 
234  FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
235  return format(SimpleNumber::forInt64(value, status), status);
236  }
237 
238 #ifndef U_HIDE_INTERNAL_API
243  void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
244 #endif // U_HIDE_INTERNAL_API
245 
252  cleanup();
253  }
254 
261 
268  fGroupingStrategy = other.fGroupingStrategy;
269  fOwnedSymbols = other.fOwnedSymbols;
270  fMicros = other.fMicros;
271  fPatternModifier = other.fPatternModifier;
272  other.fOwnedSymbols = nullptr;
273  other.fMicros = nullptr;
274  other.fPatternModifier = nullptr;
275  }
276 
283  cleanup();
284  fGroupingStrategy = other.fGroupingStrategy;
285  fOwnedSymbols = other.fOwnedSymbols;
286  fMicros = other.fMicros;
287  fPatternModifier = other.fPatternModifier;
288  other.fOwnedSymbols = nullptr;
289  other.fMicros = nullptr;
290  other.fPatternModifier = nullptr;
291  return *this;
292  }
293 
294  private:
295  void initialize(
296  const icu::Locale &locale,
297  const DecimalFormatSymbols &symbols,
298  UNumberGroupingStrategy groupingStrategy,
299  UErrorCode &status);
300 
301  void cleanup();
302 
304 
305  SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
306 
307  UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
308 
309  // Owned Pointers:
310  DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
311  impl::SimpleMicroProps* fMicros = nullptr;
312  impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
313 };
314 
315 
316 } // namespace number
317 U_NAMESPACE_END
318 
319 #endif /* #if !UCONFIG_NO_FORMATTING */
320 
321 #endif /* U_SHOW_CPLUSPLUS_API */
322 
323 #endif // __SIMPLENUMBERFORMATTERH__
324 
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:87
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:385
UMemory is the common ICU base class.
Definition: uobject.h:115
The result of a number formatting operation.
A special NumberFormatter focused on smaller binary size and memory use.
FormattedNumber formatInt64(int64_t value, UErrorCode &status) const
Formats an integer using this SimpleNumberFormatter.
SimpleNumberFormatter(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move constructor.
~SimpleNumberFormatter()
Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
static SimpleNumberFormatter forLocaleAndGroupingStrategy(const icu::Locale &locale, UNumberGroupingStrategy groupingStrategy, UErrorCode &status)
Creates a new SimpleNumberFormatter, overriding the grouping strategy.
void formatImpl(impl::UFormattedNumberData *data, USimpleNumberSign sign, UErrorCode &status) const
Run the formatter with the internal types.
FormattedNumber format(SimpleNumber value, UErrorCode &status) const
Formats a value using this SimpleNumberFormatter.
SimpleNumberFormatter & operator=(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move assignment.
SimpleNumberFormatter()=default
Creates a shell, initialized but non-functional SimpleNumberFormatter.
static SimpleNumberFormatter forLocale(const icu::Locale &locale, UErrorCode &status)
Creates a new SimpleNumberFormatter with all locale defaults.
static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(const icu::Locale &locale, const DecimalFormatSymbols &symbols, UNumberGroupingStrategy groupingStrategy, UErrorCode &status)
Creates a new SimpleNumberFormatter, overriding the grouping strategy and symbols.
An input type for SimpleNumberFormatter.
void setSign(USimpleNumberSign sign, UErrorCode &status)
Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
SimpleNumber(SimpleNumber &&other) noexcept
SimpleNumber move constructor.
void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode &status)
Pads the beginning of the number with zeros up to the given minimum number of integer digits.
void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode &status)
Sets the number of integer digits to the given amount, truncating if necessary.
void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode &status)
Pads the end of the number with zeros up to the given minimum number of fraction digits.
~SimpleNumber()
Destruct this SimpleNumber, cleaning up any memory it might own.
SimpleNumber & operator=(SimpleNumber &&other) noexcept
SimpleNumber move assignment.
void multiplyByPowerOfTen(int32_t power, UErrorCode &status)
Changes the value of the SimpleNumber by a power of 10.
SimpleNumber()=default
Creates a new, empty SimpleNumber that does not contain a value.
static SimpleNumber forInt64(int64_t value, UErrorCode &status)
Creates a SimpleNumber for an integer.
void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode &status)
Rounds the value currently stored in the SimpleNumber to the given power of 10, which can be before o...
C++ API: Symbols for formatting numbers.
C API: Formatted number result from various number formatting functions.
UNumberFormatRoundingMode
The possible number format rounding modes.
UNumberGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i....
@ UNUM_GROUPING_AUTO
Display grouping using the default strategy for all locales.
C API: Simple number formatting focused on low memory and code size.
USimpleNumberSign
An explicit sign option for a SimpleNumber.
@ UNUM_SIMPLE_NUMBER_NO_SIGN
Render no sign.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:316