ICU 75.1 75.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 /* 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
53 class U_I18N_API SimpleNumber : public UMemory {
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
87 void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode& status);
88 #endif // U_HIDE_DRAFT_API
89
90 #ifndef U_HIDE_DEPRECATED_API
97 void truncateStart(uint32_t maximumIntegerDigits, UErrorCode& status);
98 #endif // U_HIDE_DEPRECATED_API
99
105 void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
106
112 void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
113
123 void setSign(USimpleNumberSign sign, UErrorCode& status);
124
132 SimpleNumber() = default;
133
139 ~SimpleNumber() {
140 cleanup();
141 }
142
148 SimpleNumber(SimpleNumber&& other) noexcept {
149 fData = other.fData;
150 fSign = other.fSign;
151 other.fData = nullptr;
152 }
153
159 SimpleNumber& operator=(SimpleNumber&& other) noexcept {
160 cleanup();
161 fData = other.fData;
162 fSign = other.fSign;
163 other.fData = nullptr;
164 return *this;
165 }
166
167 private:
168 SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
169 SimpleNumber(const SimpleNumber&) = delete;
170 SimpleNumber& operator=(const SimpleNumber&) = delete;
171
172 void cleanup();
173
174 impl::UFormattedNumberData* fData = nullptr;
175 USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
176
177 friend class SimpleNumberFormatter;
178
179 // Uses the private constructor to avoid a heap allocation
180 friend class icu::SimpleDateFormat;
181 };
182
183
194 class U_I18N_API SimpleNumberFormatter : public UMemory {
195 public:
201 static SimpleNumberFormatter forLocale(
202 const icu::Locale &locale,
203 UErrorCode &status);
204
210 static SimpleNumberFormatter forLocaleAndGroupingStrategy(
211 const icu::Locale &locale,
212 UNumberGroupingStrategy groupingStrategy,
213 UErrorCode &status);
214
223 static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
224 const icu::Locale &locale,
225 const DecimalFormatSymbols &symbols,
226 UNumberGroupingStrategy groupingStrategy,
227 UErrorCode &status);
228
237 FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
238
246 FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
247 return format(SimpleNumber::forInt64(value, status), status);
248 }
249
250 #ifndef U_HIDE_INTERNAL_API
255 void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
256 #endif // U_HIDE_INTERNAL_API
257
263 ~SimpleNumberFormatter() {
264 cleanup();
265 }
266
272 SimpleNumberFormatter() = default;
273
279 SimpleNumberFormatter(SimpleNumberFormatter&& other) noexcept {
280 fGroupingStrategy = other.fGroupingStrategy;
281 fOwnedSymbols = other.fOwnedSymbols;
282 fMicros = other.fMicros;
283 fPatternModifier = other.fPatternModifier;
284 other.fOwnedSymbols = nullptr;
285 other.fMicros = nullptr;
286 other.fPatternModifier = nullptr;
287 }
288
294 SimpleNumberFormatter& operator=(SimpleNumberFormatter&& other) noexcept {
295 cleanup();
296 fGroupingStrategy = other.fGroupingStrategy;
297 fOwnedSymbols = other.fOwnedSymbols;
298 fMicros = other.fMicros;
299 fPatternModifier = other.fPatternModifier;
300 other.fOwnedSymbols = nullptr;
301 other.fMicros = nullptr;
302 other.fPatternModifier = nullptr;
303 return *this;
304 }
305
306 private:
307 void initialize(
308 const icu::Locale &locale,
309 const DecimalFormatSymbols &symbols,
310 UNumberGroupingStrategy groupingStrategy,
311 UErrorCode &status);
312
313 void cleanup();
314
315 SimpleNumberFormatter(const SimpleNumberFormatter&) = delete;
316
317 SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
318
319 UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
320
321 // Owned Pointers:
322 DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
323 impl::SimpleMicroProps* fMicros = nullptr;
324 impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
325 };
326
327
328 } // namespace number
329 U_NAMESPACE_END
330
331 #endif /* #if !UCONFIG_NO_FORMATTING */
332
333 #endif /* U_SHOW_CPLUSPLUS_API */
334
335 #endif // __SIMPLENUMBERFORMATTERH__
336
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::SimpleDateFormat
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:385
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:48
icu::number::SimpleNumberFormatter
A special NumberFormatter focused on smaller binary size and memory use.
Definition: simplenumberformatter.h:194
icu::number::SimpleNumberFormatter::formatInt64
FormattedNumber formatInt64(int64_t value, UErrorCode &status) const
Formats an integer using this SimpleNumberFormatter.
Definition: simplenumberformatter.h:246
icu::number::SimpleNumberFormatter::SimpleNumberFormatter
SimpleNumberFormatter(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move constructor.
Definition: simplenumberformatter.h:279
icu::number::SimpleNumberFormatter::~SimpleNumberFormatter
~SimpleNumberFormatter()
Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
Definition: simplenumberformatter.h:263
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:294
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:53
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:148
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::setMaximumIntegerDigits
void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode &status)
Sets the number of integer digits to the given amount, truncating if necessary.
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)
Alias for setMaximumIntegerDigits.
icu::number::SimpleNumber::~SimpleNumber
~SimpleNumber()
Destruct this SimpleNumber, cleaning up any memory it might own.
Definition: simplenumberformatter.h:139
icu::number::SimpleNumber::operator=
SimpleNumber & operator=(SimpleNumber &&other) noexcept
SimpleNumber move assignment.
Definition: simplenumberformatter.h:159
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, which can be before o...
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:58
UNUM_SIMPLE_NUMBER_NO_SIGN
@ UNUM_SIMPLE_NUMBER_NO_SIGN
Render no sign.
Definition: usimplenumberformatter.h:70
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