ICU 76.1 76.1
Loading...
Searching...
No Matches
decimfmt.h
Go to the documentation of this file.
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4********************************************************************************
5* Copyright (C) 1997-2016, International Business Machines
6* Corporation and others. All Rights Reserved.
7********************************************************************************
8*
9* File DECIMFMT.H
10*
11* Modification History:
12*
13* Date Name Description
14* 02/19/97 aliu Converted from java.
15* 03/20/97 clhuang Updated per C++ implementation.
16* 04/03/97 aliu Rewrote parsing and formatting completely, and
17* cleaned up and debugged. Actually works now.
18* 04/17/97 aliu Changed DigitCount to int per code review.
19* 07/10/97 helena Made ParsePosition a class and get rid of the function
20* hiding problems.
21* 09/09/97 aliu Ported over support for exponential formats.
22* 07/20/98 stephen Changed documentation
23* 01/30/13 emmons Added Scaling methods
24********************************************************************************
25*/
26
27#ifndef DECIMFMT_H
28#define DECIMFMT_H
29
30#include "unicode/utypes.h"
31
32#if U_SHOW_CPLUSPLUS_API
33
39#if !UCONFIG_NO_FORMATTING
40
41#include "unicode/dcfmtsym.h"
42#include "unicode/numfmt.h"
43#include "unicode/locid.h"
44#include "unicode/fpositer.h"
45#include "unicode/stringpiece.h"
46#include "unicode/curramt.h"
47#include "unicode/enumset.h"
48
49U_NAMESPACE_BEGIN
50
51class CurrencyPluralInfo;
52class CompactDecimalFormat;
53
54namespace number {
55class LocalizedNumberFormatter;
56namespace impl {
57class DecimalQuantity;
58struct DecimalFormatFields;
59class UFormattedNumberData;
60}
61}
62
63namespace numparse::impl {
64class NumberParserImpl;
65}
66
667 public:
673 kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
674 };
675
693
712
735
736#ifndef U_HIDE_INTERNAL_API
737
752
753#if UCONFIG_HAVE_PARSEALLINPUT
754
759
760#endif
761
762#endif /* U_HIDE_INTERNAL_API */
763
764 private:
765
771
772 public:
773
785
796
797
805
813 void setParseIntegerOnly(UBool value) override;
814
822 void setLenient(UBool enable) override;
823
848
870
878
886
891 ~DecimalFormat() override;
892
900 DecimalFormat* clone() const override;
901
910 bool operator==(const Format& other) const override;
911
912
913 using NumberFormat::format;
914
926 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const override;
927
928#ifndef U_HIDE_INTERNAL_API
941 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
942 UErrorCode& status) const override;
943#endif /* U_HIDE_INTERNAL_API */
944
959 UErrorCode& status) const override;
960
972 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const override;
973
974#ifndef U_HIDE_INTERNAL_API
988 UErrorCode& status) const override;
989#endif /* U_HIDE_INTERNAL_API */
990
1005 UErrorCode& status) const override;
1006
1018 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const override;
1019
1020#ifndef U_HIDE_INTERNAL_API
1034 UErrorCode& status) const override;
1035#endif /* U_HIDE_INTERNAL_API */
1036
1051 UErrorCode& status) const override;
1052
1070 UErrorCode& status) const override;
1071
1072#ifndef U_HIDE_INTERNAL_API
1073
1089 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1091
1107 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1108 FieldPosition& pos, UErrorCode& status) const override;
1109
1110#endif // U_HIDE_INTERNAL_API
1111
1112 using NumberFormat::parse;
1113
1134 ParsePosition& parsePosition) const override;
1135
1155 CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const override;
1156
1165
1173
1180 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1181
1182
1190
1198
1206
1207
1217
1226
1236
1245
1255
1264
1274
1283
1293
1303
1317
1332
1346
1367
1377 virtual double getRoundingIncrement() const;
1378
1390 virtual void setRoundingIncrement(double newValue);
1391
1400 virtual ERoundingMode getRoundingMode() const override;
1401
1410 virtual void setRoundingMode(ERoundingMode roundingMode) override;
1411
1423 virtual int32_t getFormatWidth() const;
1424
1440
1454
1470
1487
1505
1517
1534
1546
1560
1574
1589
1602
1615
1635
1648
1673
1692
1702
1712
1720
1731
1740
1750
1759
1772
1782
1791
1803
1815
1846
1855 virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1856
1889
1900
1901
1912
1923
1934
1945
1954
1963
1976
1989
1997
2006
2019 void setCurrency(const char16_t* theCurrency, UErrorCode& ec) override;
2020
2021#ifndef U_FORCE_HIDE_DEPRECATED_API
2027 virtual void setCurrency(const char16_t* theCurrency);
2028#endif // U_FORCE_HIDE_DEPRECATED_API
2029
2039
2045
2046#ifndef U_HIDE_INTERNAL_API
2047
2053 void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
2054 UErrorCode& status) const;
2055
2062 void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
2063 UErrorCode& status) const;
2064
2065#endif /* U_HIDE_INTERNAL_API */
2066
2113
2126
2138 UClassID getDynamicClassID() const override;
2139
2140 private:
2141
2143 void touch(UErrorCode& status);
2144
2146 void touchNoError();
2147
2159 void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2161
2162 const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2163
2164 const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2165
2166 static void fieldPositionHelper(
2167 const number::impl::UFormattedNumberData& formatted,
2169 int32_t offset,
2171
2172 static void fieldPositionIteratorHelper(
2173 const number::impl::UFormattedNumberData& formatted,
2175 int32_t offset,
2177
2178 void setupFastFormat();
2179
2180 bool fastFormatDouble(double input, UnicodeString& output) const;
2181
2182 bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2183
2184 void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2185
2186 //=====================================================================================//
2187 // INSTANCE FIELDS //
2188 //=====================================================================================//
2189
2190
2191 // One instance field for the implementation, keep all fields inside of an implementation
2192 // class defined in number_mapper.h
2193 number::impl::DecimalFormatFields* fields = nullptr;
2194
2195 // Allow child class CompactDecimalFormat to access fProperties:
2196 friend class CompactDecimalFormat;
2197
2198 // Allow MeasureFormat to use fieldPositionHelper:
2199 friend class MeasureFormat;
2200
2201};
2202
2204
2205#endif /* #if !UCONFIG_NO_FORMATTING */
2206
2207#endif /* U_SHOW_CPLUSPLUS_API */
2208
2209#endif // _DECIMFMT
2210//eof
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case.
A currency together with a numeric amount, such as 200 USD.
Definition curramt.h:39
This class represents the information needed by DecimalFormat to format currency plural,...
Definition currpinf.h:48
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition dcfmtsym.h:86
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case.
Definition decimfmt.h:666
UnicodeString & getNegativePrefix(UnicodeString &result) const
Get the negative prefix.
UBool isDecimalSeparatorAlwaysShown() const
Allows you to get the behavior of the decimal separator with integers.
void setMinimumSignificantDigits(int32_t min)
Sets the minimum number of significant digits that will be displayed.
void setParseCaseSensitive(UBool value)
Whether to pay attention to case when parsing; default is to ignore case (perform case-folding).
DecimalFormat(UErrorCode &status)
Create a DecimalFormat using the default pattern and symbols for the default locale.
virtual void setSecondaryGroupingSize(int32_t newValue)
Set the secondary grouping size.
UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format a decimal number.
int32_t getMaximumSignificantDigits() const
Returns the maximum number of significant digits that will be displayed.
virtual void setDecimalPatternMatchRequired(UBool newValue)
Allows you to set the parse behavior of the pattern decimal mark.
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format an int64 number using base-10 representation.
void setMaximumIntegerDigits(int32_t newValue) override
Sets the maximum number of digits allowed in the integer portion of a number.
~DecimalFormat() override
Destructor.
virtual void applyLocalizedPattern(const UnicodeString &pattern, UErrorCode &status)
Apply the given pattern to this Format object.
virtual void setPositivePrefix(const UnicodeString &newValue)
Set the positive prefix.
virtual void setDecimalSeparatorAlwaysShown(UBool newValue)
Allows you to set the behavior of the decimal separator with integers.
virtual void applyPattern(const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
Apply the given pattern to this Format object.
UnicodeString & format(double number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format a double or long number using base-10 representation.
int32_t getGroupingSize() const
Return the grouping size.
UBool isFormatFailIfMoreThanMaxDigits() const
Returns whether truncation of high-order integer digits should result in an error.
virtual EPadPosition getPadPosition() const
Get the position at which padding will take place.
UnicodeString & getPositivePrefix(UnicodeString &result) const
Get the positive prefix.
bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
void setSignificantDigitsUsed(UBool useSignificantDigits)
Sets whether significant digits are in use, or integer and fraction digit counts are in use.
virtual double getRoundingIncrement() const
Get the rounding increment.
void setParseNoExponent(UBool value)
Specifies whether to stop parsing when an exponent separator is encountered.
virtual UnicodeString & toLocalizedPattern(UnicodeString &result) const
Synthesizes a localized pattern string that represents the current state of this Format object.
int32_t getMinimumSignificantDigits() const
Returns the minimum number of significant digits that will be displayed.
UnicodeString & getPositiveSuffix(UnicodeString &result) const
Get the positive suffix.
int32_t getMultiplier() const
Get the multiplier for use in percent, permill, etc.
UClassID getDynamicClassID() const override
Returns a unique class ID POLYMORPHICALLY.
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format an int64 number using base-10 representation.
virtual void setPadPosition(EPadPosition padPos)
Set the position at which padding will take place.
virtual void setNegativePrefix(const UnicodeString &newValue)
Set the negative prefix.
virtual void setRoundingIncrement(double newValue)
Set the rounding increment.
virtual void setCurrencyPluralInfo(const CurrencyPluralInfo &info)
Sets the currency plural format information, which is generally not changed by the programmer or user...
virtual void setDecimalFormatSymbols(const DecimalFormatSymbols &symbols)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
void setMinimumGroupingDigits(int32_t newValue)
Sets the minimum grouping digits.
UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos) const override
Format a double or long number using base-10 representation.
static UClassID getStaticClassID()
Return the class ID for this class.
DecimalFormat(const UnicodeString &pattern, DecimalFormatSymbols *symbolsToAdopt, UErrorCode &status)
Create a DecimalFormat from the given pattern and symbols.
virtual int8_t getMinimumExponentDigits() const
Return the minimum exponent digits that will be shown.
virtual void setCurrency(const char16_t *theCurrency)
Sets the currency used to display currency amounts.
int32_t getMinimumGroupingDigits() const
Returns the minimum number of grouping digits.
void setMinimumFractionDigits(int32_t newValue) override
Sets the minimum number of digits allowed in the fraction portion of a number.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format a long number using base-10 representation.
void setMaximumSignificantDigits(int32_t max)
Sets the maximum number of significant digits that will be displayed.
virtual void setFormatWidth(int32_t width)
Set the width to which the output of format() is padded.
void formatToDecimalQuantity(const Formattable &number, number::impl::DecimalQuantity &output, UErrorCode &status) const
Get a DecimalQuantity corresponding to a formattable as it would be formatted by this DecimalFormat.
void setSignAlwaysShown(UBool value)
Set whether to show the plus sign on positive (non-negative) numbers; for example,...
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos) const override
Format an int64 number using base-10 representation.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format a long number using base-10 representation.
int32_t getMultiplierScale() const
Gets the power of ten by which number should be multiplied before formatting, which can be combined w...
UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format a double or long number using base-10 representation.
virtual void setExponentSignAlwaysShown(UBool expSignAlways)
Set whether the exponent sign is always shown.
UCurrencyUsage getCurrencyUsage() const
Returns the Currency Usage object used to display currency.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos) const override
Format a long number using base-10 representation.
virtual const DecimalFormatSymbols * getDecimalFormatSymbols() const
Returns the decimal format symbols, which is generally not changed by the programmer or user.
virtual UBool isScientificNotation() const
Return whether or not scientific notation is used.
void setGroupingUsed(UBool newValue) override
Set whether or not grouping will be used in this format.
DecimalFormat(const UnicodeString &pattern, UErrorCode &status)
Create a DecimalFormat from the given pattern and the symbols for the default locale.
UBool isParseCaseSensitive() const
Returns whether parsing is sensitive to case (lowercase/uppercase).
void setMaximumFractionDigits(int32_t newValue) override
Sets the maximum number of digits allowed in the fraction portion of a number.
virtual void setNegativeSuffix(const UnicodeString &newValue)
Set the negative suffix.
virtual int32_t getFormatWidth() const
Get the width to which the output of format() is padded.
virtual void setScientificNotation(UBool useScientific)
Set whether or not scientific notation is used.
DecimalFormat(const DecimalFormat &source)
Copy constructor.
void formatToDecimalQuantity(double number, number::impl::DecimalQuantity &output, UErrorCode &status) const
Format a number and save it into the given DecimalQuantity.
DecimalFormat * clone() const override
Clone this Format object polymorphically.
UBool isSignAlwaysShown() const
Whether to show the plus sign on positive (non-negative) numbers; for example, "+12".
void setFormatFailIfMoreThanMaxDigits(UBool value)
Sets whether truncation of high-order integer digits should result in an error.
virtual void setPositiveSuffix(const UnicodeString &newValue)
Set the positive suffix.
virtual void setGroupingSize(int32_t newValue)
Set the grouping size.
CurrencyAmount * parseCurrency(const UnicodeString &text, ParsePosition &pos) const override
Parses text from the given string as a currency amount.
UBool areSignificantDigitsUsed() const
Returns true if significant digits are in use, or false if integer and fraction digit counts are in u...
void setMultiplierScale(int32_t newValue)
Sets a power of ten by which number should be multiplied before formatting, which can be combined wit...
DecimalFormat(const UnicodeString &pattern, DecimalFormatSymbols *symbolsToAdopt, UParseError &parseError, UErrorCode &status)
Create a DecimalFormat from the given pattern and symbols.
virtual DecimalFormat & setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode &status)
Set an integer attribute on this DecimalFormat.
UnicodeString & format(StringPiece number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format a decimal number.
virtual void setMinimumExponentDigits(int8_t minExpDig)
Set the minimum exponent digits that will be shown.
virtual void applyPattern(const UnicodeString &pattern, UErrorCode &status)
Sets the pattern.
virtual ERoundingMode getRoundingMode() const override
Get the rounding mode.
const number::LocalizedNumberFormatter * toNumberFormatter(UErrorCode &status) const
Converts this DecimalFormat to a (Localized)NumberFormatter.
void setLenient(UBool enable) override
Sets whether lenient parsing should be enabled (it is off by default).
void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode *ec)
Sets the Currency Usage object used to display currency.
UBool isDecimalPatternMatchRequired() const
Allows you to get the parse behavior of the pattern decimal mark.
void setCurrency(const char16_t *theCurrency, UErrorCode &ec) override
Sets the currency used to display currency amounts.
virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode &status) const
Get an integer May return U_UNSUPPORTED_ERROR if this instance does not support the specified attribu...
void setMinimumIntegerDigits(int32_t newValue) override
Sets the minimum number of digits allowed in the integer portion of a number.
virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols *symbolsToAdopt)
Sets the decimal format symbols, which is generally not changed by the programmer or user.
virtual UnicodeString getPadCharacterString() const
Get the pad character used to pad to the format width.
virtual void setMultiplier(int32_t newValue)
Set the multiplier for use in percent, permill, etc.
virtual void applyLocalizedPattern(const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
Apply the given pattern to this Format object.
EPadPosition
Pad position.
Definition decimfmt.h:672
int32_t getSecondaryGroupingSize() const
Return the secondary grouping size.
UnicodeString & getNegativeSuffix(UnicodeString &result) const
Get the negative suffix.
UBool isParseNoExponent() const
Returns whether to ignore exponents when parsing.
void setParseIntegerOnly(UBool value) override
Sets whether or not numbers should be parsed as integers only.
DecimalFormat & operator=(const DecimalFormat &rhs)
Assignment operator.
virtual const CurrencyPluralInfo * getCurrencyPluralInfo() const
Returns the currency plural format information, which is generally not changed by the programmer or u...
virtual UnicodeString & toPattern(UnicodeString &result) const
Synthesizes a pattern string that represents the current state of this Format object.
virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo *toAdopt)
Sets the currency plural format information, which is generally not changed by the programmer or user...
virtual UBool isExponentSignAlwaysShown() const
Return whether the exponent sign is always shown.
virtual void setRoundingMode(ERoundingMode roundingMode) override
Set the rounding mode.
UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format a decimal number.
virtual void setPadCharacter(const UnicodeString &padChar)
Set the character used to pad to the format width.
DecimalFormat(const UnicodeString &pattern, const DecimalFormatSymbols &symbols, UErrorCode &status)
Create a DecimalFormat from the given pattern and symbols.
void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const override
Parse the given string using this object's choices.
DecimalFormat(const UnicodeString &pattern, DecimalFormatSymbols *symbolsToAdopt, UNumberFormatStyle style, UErrorCode &status)
This API is for ICU use only.
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition fpositer.h:58
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:110
Base class for all formats.
Definition format.h:98
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:63
"Smart pointer" base class; do not use directly: use LocalPointer etc.
ERoundingMode
Rounding mode.
Definition numfmt.h:186
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition parsepos.h:52
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:61
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:296
A NumberFormatter that has a locale associated with it; this means .format() methods are available.
C++ API: Currency Amount Object.
C++ API: Symbols for formatting numbers.
C++: internal template EnumSet<>
C++ API: FieldPosition Iterator.
C++ API: Locale ID object.
C++ API: Compatibility APIs for number formatting.
C++ API: StringPiece: Read-only byte string wrapper class.
A UParseError struct is used to returned detailed information about parsing errors.
Definition parseerr.h:58
UCurrencyUsage
Currency Usage used for Decimal Format.
Definition ucurr.h:41
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:247
UNumberFormatAttribute
The possible UNumberFormat numeric attributes.
Definition unum.h:970
UNumberFormatStyle
The possible number format styles.
Definition unum.h:150
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:96
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