ICU 74.1 74.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"
14 #include "unicode/usimplenumberformatter.h"
15 #include "unicode/formattednumber.h"
16
31 U_NAMESPACE_BEGIN
32
33
34 namespace number { // icu::number
35
36
37 namespace impl {
38 class UFormattedNumberData;
39 struct SimpleMicroProps;
40 class AdoptingSignumModifierStore;
41 } // icu::number::impl
42
43
44 #ifndef U_HIDE_DRAFT_API
45
46
54 class U_I18N_API SimpleNumber : public UMemory {
55 public:
61 static SimpleNumber forInt64(int64_t value, UErrorCode& status);
62
70 void multiplyByPowerOfTen(int32_t power, UErrorCode& status);
71
79 void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode& status);
80
88 void truncateStart(uint32_t maximumIntegerDigits, UErrorCode& status);
89
97 void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
98
106 void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
107
117 void setSign(USimpleNumberSign sign, UErrorCode& status);
118
126 SimpleNumber() = default;
127
133 ~SimpleNumber() {
134 cleanup();
135 }
136
142 SimpleNumber(SimpleNumber&& other) noexcept {
143 fData = other.fData;
144 fSign = other.fSign;
145 other.fData = nullptr;
146 }
147
153 SimpleNumber& operator=(SimpleNumber&& other) noexcept {
154 cleanup();
155 fData = other.fData;
156 fSign = other.fSign;
157 other.fData = nullptr;
158 return *this;
159 }
160
161 private:
162 SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
163 SimpleNumber(const SimpleNumber&) = delete;
164 SimpleNumber& operator=(const SimpleNumber&) = delete;
165
166 void cleanup();
167
168 impl::UFormattedNumberData* fData = nullptr;
169 USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
170
171 friend class SimpleNumberFormatter;
172 };
173
174
185 class U_I18N_API SimpleNumberFormatter : public UMemory {
186 public:
192 static SimpleNumberFormatter forLocale(
193 const icu::Locale &locale,
194 UErrorCode &status);
195
201 static SimpleNumberFormatter forLocaleAndGroupingStrategy(
202 const icu::Locale &locale,
203 UNumberGroupingStrategy groupingStrategy,
204 UErrorCode &status);
205
214 static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
215 const icu::Locale &locale,
216 const DecimalFormatSymbols &symbols,
217 UNumberGroupingStrategy groupingStrategy,
218 UErrorCode &status);
219
228 FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
229
237 FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
238 return format(SimpleNumber::forInt64(value, status), status);
239 }
240
241 #ifndef U_HIDE_INTERNAL_API
246 void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
247 #endif // U_HIDE_INTERNAL_API
248
254 ~SimpleNumberFormatter() {
255 cleanup();
256 }
257
263 SimpleNumberFormatter() = default;
264
270 SimpleNumberFormatter(SimpleNumberFormatter&& other) noexcept {
271 fGroupingStrategy = other.fGroupingStrategy;
272 fOwnedSymbols = other.fOwnedSymbols;
273 fMicros = other.fMicros;
274 fPatternModifier = other.fPatternModifier;
275 other.fOwnedSymbols = nullptr;
276 other.fMicros = nullptr;
277 other.fPatternModifier = nullptr;
278 }
279
285 SimpleNumberFormatter& operator=(SimpleNumberFormatter&& other) noexcept {
286 cleanup();
287 fGroupingStrategy = other.fGroupingStrategy;
288 fOwnedSymbols = other.fOwnedSymbols;
289 fMicros = other.fMicros;
290 fPatternModifier = other.fPatternModifier;
291 other.fOwnedSymbols = nullptr;
292 other.fMicros = nullptr;
293 other.fPatternModifier = nullptr;
294 return *this;
295 }
296
297 private:
298 void initialize(
299 const icu::Locale &locale,
300 const DecimalFormatSymbols &symbols,
301 UNumberGroupingStrategy groupingStrategy,
302 UErrorCode &status);
303
304 void cleanup();
305
306 SimpleNumberFormatter(const SimpleNumberFormatter&) = delete;
307
308 SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
309
310 UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
311
312 // Owned Pointers:
313 DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
314 impl::SimpleMicroProps* fMicros = nullptr;
315 impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
316 };
317
318
319 #endif // U_HIDE_DRAFT_API
320
321 } // namespace number
322 U_NAMESPACE_END
323
324 #endif /* #if !UCONFIG_NO_FORMATTING */
325
326 #endif /* U_SHOW_CPLUSPLUS_API */
327
328 #endif // __SIMPLENUMBERFORMATTERH__
329
icu::DecimalFormatSymbols
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition: dcfmtsym.h:86
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::number::FormattedNumber
The result of a number formatting operation.
Definition: formattednumber.h:47
icu::number::SimpleNumberFormatter
A special NumberFormatter focused on smaller binary size and memory use.
Definition: simplenumberformatter.h:185
icu::number::SimpleNumberFormatter::formatInt64
FormattedNumber formatInt64(int64_t value, UErrorCode &status) const
Formats an integer using this SimpleNumberFormatter.
Definition: simplenumberformatter.h:237
icu::number::SimpleNumberFormatter::SimpleNumberFormatter
SimpleNumberFormatter(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move constructor.
Definition: simplenumberformatter.h:270
icu::number::SimpleNumberFormatter::~SimpleNumberFormatter
~SimpleNumberFormatter()
Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
Definition: simplenumberformatter.h:254
icu::number::SimpleNumberFormatter::forLocaleAndGroupingStrategy
static SimpleNumberFormatter forLocaleAndGroupingStrategy(const icu::Locale &locale, UNumberGroupingStrategy groupingStrategy, UErrorCode &status)
Creates a new SimpleNumberFormatter, overriding the grouping strategy.
icu::number::SimpleNumberFormatter::formatImpl
void formatImpl(impl::UFormattedNumberData *data, USimpleNumberSign sign, UErrorCode &status) const
Run the formatter with the internal types.
icu::number::SimpleNumberFormatter::format
FormattedNumber format(SimpleNumber value, UErrorCode &status) const
Formats a value using this SimpleNumberFormatter.
icu::number::SimpleNumberFormatter::operator=
SimpleNumberFormatter & operator=(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move assignment.
Definition: simplenumberformatter.h:285
icu::number::SimpleNumberFormatter::SimpleNumberFormatter
SimpleNumberFormatter()=default
Creates a shell, initialized but non-functional SimpleNumberFormatter.
icu::number::SimpleNumberFormatter::forLocale
static SimpleNumberFormatter forLocale(const icu::Locale &locale, UErrorCode &status)
Creates a new SimpleNumberFormatter with all locale defaults.
icu::number::SimpleNumberFormatter::forLocaleAndSymbolsAndGroupingStrategy
static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(const icu::Locale &locale, const DecimalFormatSymbols &symbols, UNumberGroupingStrategy groupingStrategy, UErrorCode &status)
Creates a new SimpleNumberFormatter, overriding the grouping strategy and symbols.
icu::number::SimpleNumber
An input type for SimpleNumberFormatter.
Definition: simplenumberformatter.h:54
icu::number::SimpleNumber::setSign
void setSign(USimpleNumberSign sign, UErrorCode &status)
Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
icu::number::SimpleNumber::SimpleNumber
SimpleNumber(SimpleNumber &&other) noexcept
SimpleNumber move constructor.
Definition: simplenumberformatter.h:142
icu::number::SimpleNumber::setMinimumIntegerDigits
void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode &status)
Pads the beginning of the number with zeros up to the given minimum number of integer digits.
icu::number::SimpleNumber::setMinimumFractionDigits
void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode &status)
Pads the end of the number with zeros up to the given minimum number of fraction digits.
icu::number::SimpleNumber::truncateStart
void truncateStart(uint32_t maximumIntegerDigits, UErrorCode &status)
Truncates the most significant digits to the given maximum number of integer digits.
icu::number::SimpleNumber::~SimpleNumber
~SimpleNumber()
Destruct this SimpleNumber, cleaning up any memory it might own.
Definition: simplenumberformatter.h:133
icu::number::SimpleNumber::operator=
SimpleNumber & operator=(SimpleNumber &&other) noexcept
SimpleNumber move assignment.
Definition: simplenumberformatter.h:153
icu::number::SimpleNumber::multiplyByPowerOfTen
void multiplyByPowerOfTen(int32_t power, UErrorCode &status)
Changes the value of the SimpleNumber by a power of 10.
icu::number::SimpleNumber::SimpleNumber
SimpleNumber()=default
Creates a new, empty SimpleNumber that does not contain a value.
icu::number::SimpleNumber::forInt64
static SimpleNumber forInt64(int64_t value, UErrorCode &status)
Creates a SimpleNumber for an integer.
icu::number::SimpleNumber::roundTo
void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode &status)
Rounds the value currently stored in the SimpleNumber to the given power of 10.
dcfmtsym.h
C++ API: Symbols for formatting numbers.
formattednumber.h
C API: Formatted number result from various number formatting functions.
UNumberFormatRoundingMode
UNumberFormatRoundingMode
The possible number format rounding modes.
Definition: unumberoptions.h:28
UNumberGroupingStrategy
UNumberGroupingStrategy
An enum declaring the strategy for when and how to display grouping separators (i....
Definition: unumberoptions.h:96
UNUM_GROUPING_AUTO
@ UNUM_GROUPING_AUTO
Display grouping using the default strategy for all locales.
Definition: unumberoptions.h:133
usimplenumberformatter.h
C API: Simple number formatting focused on low memory and code size.
USimpleNumberSign
USimpleNumberSign
An explicit sign option for a SimpleNumber.
Definition: usimplenumberformatter.h:61
UNUM_SIMPLE_NUMBER_NO_SIGN
@ UNUM_SIMPLE_NUMBER_NO_SIGN
Render no sign.
Definition: usimplenumberformatter.h:73
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301