Loading...
Searching...
No Matches
simplenumberformatter.h
Go to the documentation of this file.
// © 2022 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#ifndef __SIMPLENUMBERFORMATTERH__
#define __SIMPLENUMBERFORMATTERH__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#if !UCONFIG_NO_FORMATTING
#include "unicode/dcfmtsym.h"
#include "unicode/usimplenumberformatter.h"
#include "unicode/formattednumber.h"
U_NAMESPACE_BEGIN
/* forward declaration */
class SimpleDateFormat;
namespace number { // icu::number
namespace impl {
class UFormattedNumberData;
struct SimpleMicroProps;
class AdoptingSignumModifierStore;
} // icu::number::impl
class U_I18N_API SimpleNumber : public UMemory {
public:
static SimpleNumber forInt64(int64_t value, UErrorCode& status);
void multiplyByPowerOfTen(int32_t power, UErrorCode& status);
void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode& status);
void setMaximumIntegerDigits(uint32_t maximumIntegerDigits, UErrorCode& status);
void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
void setSign(USimpleNumberSign sign, UErrorCode& status);
SimpleNumber() = default;
~SimpleNumber() {
cleanup();
}
SimpleNumber(SimpleNumber&& other) noexcept {
fData = other.fData;
fSign = other.fSign;
other.fData = nullptr;
}
SimpleNumber& operator=(SimpleNumber&& other) noexcept {
cleanup();
fData = other.fData;
fSign = other.fSign;
other.fData = nullptr;
return *this;
}
private:
SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
SimpleNumber(const SimpleNumber&) = delete;
SimpleNumber& operator=(const SimpleNumber&) = delete;
void cleanup();
impl::UFormattedNumberData* fData = nullptr;
USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
friend class SimpleNumberFormatter;
// Uses the private constructor to avoid a heap allocation
friend class icu::SimpleDateFormat;
};
class U_I18N_API SimpleNumberFormatter : public UMemory {
public:
static SimpleNumberFormatter forLocale(
const icu::Locale &locale,
UErrorCode &status);
static SimpleNumberFormatter forLocaleAndGroupingStrategy(
const icu::Locale &locale,
UNumberGroupingStrategy groupingStrategy,
UErrorCode &status);
static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
const icu::Locale &locale,
const DecimalFormatSymbols &symbols,
UNumberGroupingStrategy groupingStrategy,
UErrorCode &status);
FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
return format(SimpleNumber::forInt64(value, status), status);
}
#ifndef U_HIDE_INTERNAL_API
void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
#endif // U_HIDE_INTERNAL_API
~SimpleNumberFormatter() {
cleanup();
}
SimpleNumberFormatter() = default;
SimpleNumberFormatter(SimpleNumberFormatter&& other) noexcept {
fGroupingStrategy = other.fGroupingStrategy;
fOwnedSymbols = other.fOwnedSymbols;
fMicros = other.fMicros;
fPatternModifier = other.fPatternModifier;
other.fOwnedSymbols = nullptr;
other.fMicros = nullptr;
other.fPatternModifier = nullptr;
}
SimpleNumberFormatter& operator=(SimpleNumberFormatter&& other) noexcept {
cleanup();
fGroupingStrategy = other.fGroupingStrategy;
fOwnedSymbols = other.fOwnedSymbols;
fMicros = other.fMicros;
fPatternModifier = other.fPatternModifier;
other.fOwnedSymbols = nullptr;
other.fMicros = nullptr;
other.fPatternModifier = nullptr;
return *this;
}
private:
void initialize(
const icu::Locale &locale,
const DecimalFormatSymbols &symbols,
UNumberGroupingStrategy groupingStrategy,
UErrorCode &status);
void cleanup();
SimpleNumberFormatter(const SimpleNumberFormatter&) = delete;
SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
// Owned Pointers:
DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
impl::SimpleMicroProps* fMicros = nullptr;
impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
};
} // namespace number
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // __SIMPLENUMBERFORMATTERH__
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:198
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:182
icu::number::SimpleNumberFormatter::formatInt64
FormattedNumber formatInt64(int64_t value, UErrorCode &status) const
Formats an integer using this SimpleNumberFormatter.
Definition simplenumberformatter.h:234
icu::number::SimpleNumberFormatter::SimpleNumberFormatter
SimpleNumberFormatter(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move constructor.
Definition simplenumberformatter.h:267
icu::number::SimpleNumberFormatter::operator=
SimpleNumberFormatter & operator=(SimpleNumberFormatter &&other) noexcept
SimpleNumberFormatter: Move assignment.
Definition simplenumberformatter.h:282
icu::number::SimpleNumberFormatter::~SimpleNumberFormatter
~SimpleNumberFormatter()
Destruct this SimpleNumberFormatter, cleaning up any memory it might own.
Definition simplenumberformatter.h:251
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::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::operator=
SimpleNumber & operator=(SimpleNumber &&other) noexcept
SimpleNumber move assignment.
Definition simplenumberformatter.h:147
icu::number::SimpleNumber::SimpleNumber
SimpleNumber(SimpleNumber &&other) noexcept
SimpleNumber move constructor.
Definition simplenumberformatter.h:136
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::~SimpleNumber
~SimpleNumber()
Destruct this SimpleNumber, cleaning up any memory it might own.
Definition simplenumberformatter.h:127
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:509
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:316