ICU 65.1 65.1
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;
56class FormattedNumber;
57namespace impl {
58class DecimalQuantity;
59struct DecimalFormatFields;
60}
61}
62
63namespace numparse {
64namespace impl {
65class NumberParserImpl;
66}
67}
68
669 public:
675 kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
676 };
677
695
714
737
738#ifndef U_HIDE_INTERNAL_API
739
754
755#if UCONFIG_HAVE_PARSEALLINPUT
756
761
762#endif
763
764#endif /* U_HIDE_INTERNAL_API */
765
766 private:
767
773
774 public:
775
787
798
799
807
816
825
850
872
880
888
894
903
913
914
915 using NumberFormat::format;
916
928 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
929
930#ifndef U_HIDE_INTERNAL_API
943 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
945#endif /* U_HIDE_INTERNAL_API */
946
962
975
976#ifndef U_HIDE_INTERNAL_API
991#endif /* U_HIDE_INTERNAL_API */
992
1008
1021
1022#ifndef U_HIDE_INTERNAL_API
1037#endif /* U_HIDE_INTERNAL_API */
1038
1054
1073
1074#ifndef U_HIDE_INTERNAL_API
1075
1091 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1093
1109 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1111
1112#endif // U_HIDE_INTERNAL_API
1113
1114 using NumberFormat::parse;
1115
1137
1158
1167
1175
1182 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1183
1184
1191 virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const;
1192
1200
1208
1209
1219
1228
1238
1247
1257
1266
1276
1285
1286#ifndef U_HIDE_DRAFT_API
1296
1306#endif /* U_HIDE_DRAFT_API */
1307
1321
1336
1350
1371
1381 virtual double getRoundingIncrement(void) const;
1382
1394 virtual void setRoundingIncrement(double newValue);
1395
1405
1414 virtual void setRoundingMode(ERoundingMode roundingMode) U_OVERRIDE;
1415
1427 virtual int32_t getFormatWidth(void) const;
1428
1444
1458
1474
1490 virtual EPadPosition getPadPosition(void) const;
1491
1509
1520 virtual UBool isScientificNotation(void) const;
1521
1538
1550
1564
1578
1593
1606
1619
1639
1652
1653#ifndef U_HIDE_DRAFT_API
1678
1690#endif /* U_HIDE_DRAFT_API */
1691
1692
1702
1712
1720
1731
1732#ifndef U_HIDE_DRAFT_API
1741
1751
1760
1773
1783
1792#endif /* U_HIDE_DRAFT_API */
1793
1794
1806
1818
1849
1858 virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1859
1892
1903
1904
1915
1926
1937
1948
1957
1966
1979
1992
2000
2009
2023
2024#ifndef U_FORCE_HIDE_DEPRECATED_API
2030 virtual void setCurrency(const char16_t* theCurrency);
2031#endif // U_FORCE_HIDE_DEPRECATED_API
2032
2042
2048
2049#ifndef U_HIDE_INTERNAL_API
2050
2056 void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
2057 UErrorCode& status) const;
2058
2065 void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
2066 UErrorCode& status) const;
2067
2068#endif /* U_HIDE_INTERNAL_API */
2069
2070#ifndef U_HIDE_DRAFT_API
2117#endif /* U_HIDE_DRAFT_API */
2118
2131
2144
2145 private:
2146
2148 void touch(UErrorCode& status);
2149
2151 void touchNoError();
2152
2164 void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2166
2167 const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2168
2169 const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2170
2171 static void fieldPositionHelper(const number::FormattedNumber& formatted, FieldPosition& fieldPosition,
2172 int32_t offset, UErrorCode& status);
2173
2174 static void fieldPositionIteratorHelper(const number::FormattedNumber& formatted,
2176
2177 void setupFastFormat();
2178
2179 bool fastFormatDouble(double input, UnicodeString& output) const;
2180
2181 bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2182
2183 void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2184
2185 //=====================================================================================//
2186 // INSTANCE FIELDS //
2187 //=====================================================================================//
2188
2189
2190 // One instance field for the implementation, keep all fields inside of an implementation
2191 // class defined in number_mapper.h
2192 number::impl::DecimalFormatFields* fields = nullptr;
2193
2194 // Allow child class CompactDecimalFormat to access fProperties:
2195 friend class CompactDecimalFormat;
2196
2197 // Allow MeasureFormat to use fieldPositionHelper:
2198 friend class MeasureFormat;
2199
2200};
2201
2203
2204#endif /* #if !UCONFIG_NO_FORMATTING */
2205
2206#endif /* U_SHOW_CPLUSPLUS_API */
2207
2208#endif // _DECIMFMT
2209//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:668
UnicodeString & getNegativePrefix(UnicodeString &result) const
Get the negative prefix.
void setLenient(UBool enable) U_OVERRIDE
Sets whether lenient parsing should be enabled (it is off by default).
virtual const CurrencyPluralInfo * getCurrencyPluralInfo(void) const
Returns the currency plural format information, which is generally not changed by the programmer or u...
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.
int32_t getMultiplier(void) const
Get the multiplier for use in percent, permill, etc.
virtual void setSecondaryGroupingSize(int32_t newValue)
Set the secondary grouping size.
virtual UBool isExponentSignAlwaysShown(void) const
Return whether the exponent sign is always shown.
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.
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.
UBool isDecimalPatternMatchRequired(void) const
Allows you to get the parse behavior of the pattern decimal mark.
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(int32_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const U_OVERRIDE
Format a long number using base-10 representation.
void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const U_OVERRIDE
Parse the given string using this object's choices.
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos) const U_OVERRIDE
Format an int64 number using base-10 representation.
UBool isFormatFailIfMoreThanMaxDigits() const
Returns whether truncation of high-order integer digits should result in an error.
UnicodeString & getPositivePrefix(UnicodeString &result) const
Get the positive prefix.
void setSignificantDigitsUsed(UBool useSignificantDigits)
Sets whether significant digits are in use, or integer and fraction digit counts are in use.
virtual UBool isScientificNotation(void) const
Return whether or not scientific notation is used.
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.
void setMaximumIntegerDigits(int32_t newValue) U_OVERRIDE
Sets the maximum number of digits allowed in the integer portion of a number.
int32_t getMinimumSignificantDigits() const
Returns the minimum number of significant digits that will be displayed.
void setMinimumIntegerDigits(int32_t newValue) U_OVERRIDE
Sets the minimum number of digits allowed in the integer portion of a number.
UnicodeString & getPositiveSuffix(UnicodeString &result) const
Get the positive suffix.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos) const U_OVERRIDE
Format a long number using base-10 representation.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const U_OVERRIDE
Format a long number using base-10 representation.
static UClassID getStaticClassID(void)
Return the class ID for this class.
virtual void setPadPosition(EPadPosition padPos)
Set the position at which padding will take place.
virtual void setNegativePrefix(const UnicodeString &newValue)
Set the negative prefix.
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const U_OVERRIDE
Format an int64 number using base-10 representation.
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...
UnicodeString & format(double number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const U_OVERRIDE
Format a double or long number using base-10 representation.
virtual const DecimalFormatSymbols * getDecimalFormatSymbols(void) const
Returns the decimal format symbols, 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.
DecimalFormat(const UnicodeString &pattern, DecimalFormatSymbols *symbolsToAdopt, UErrorCode &status)
Create a DecimalFormat from the given pattern and symbols.
virtual void setCurrency(const char16_t *theCurrency)
Sets the currency used to display currency amounts.
void setMaximumFractionDigits(int32_t newValue) U_OVERRIDE
Sets the maximum number of digits allowed in the fraction portion of a number.
~DecimalFormat() U_OVERRIDE
Destructor.
int32_t getMinimumGroupingDigits() const
Returns the minimum number of grouping digits.
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.
UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const U_OVERRIDE
Format a double or long number using base-10 representation.
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,...
virtual void setRoundingMode(ERoundingMode roundingMode) U_OVERRIDE
Set the rounding mode.
virtual int8_t getMinimumExponentDigits(void) const
Return the minimum exponent digits that will be shown.
UClassID getDynamicClassID(void) const U_OVERRIDE
Returns a unique class ID POLYMORPHICALLY.
virtual void setExponentSignAlwaysShown(UBool expSignAlways)
Set whether the exponent sign is always shown.
CurrencyAmount * parseCurrency(const UnicodeString &text, ParsePosition &pos) const U_OVERRIDE
Parses text from the given string as a currency amount.
UCurrencyUsage getCurrencyUsage() const
Returns the Currency Usage object used to display currency.
virtual EPadPosition getPadPosition(void) const
Get the position at which padding will take place.
void setCurrency(const char16_t *theCurrency, UErrorCode &ec) U_OVERRIDE
Sets the currency used to display currency amounts.
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).
virtual void setNegativeSuffix(const UnicodeString &newValue)
Set the negative suffix.
virtual int32_t getFormatWidth(void) const
Get the width to which the output of format() is padded.
void setMinimumFractionDigits(int32_t newValue) U_OVERRIDE
Sets the minimum number of digits allowed in the fraction portion of a number.
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.
int32_t getGroupingSize(void) const
Return the grouping size.
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.
void setParseIntegerOnly(UBool value) U_OVERRIDE
Sets whether or not numbers should be parsed as integers only.
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.
void setGroupingUsed(UBool newValue) U_OVERRIDE
Set whether or not grouping will be used in this format.
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.
UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const U_OVERRIDE
Format a decimal number.
UnicodeString & format(StringPiece number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const U_OVERRIDE
Format a decimal number.
const number::LocalizedNumberFormatter * toNumberFormatter(UErrorCode &status) const
Converts this DecimalFormat to a (Localized)NumberFormatter.
void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode *ec)
Sets the Currency Usage object used to display currency.
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...
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const U_OVERRIDE
Format an int64 number using base-10 representation.
UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const U_OVERRIDE
Format a decimal 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.
int32_t getSecondaryGroupingSize(void) const
Return the secondary grouping size.
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:674
virtual ERoundingMode getRoundingMode(void) const U_OVERRIDE
Get the rounding mode.
virtual double getRoundingIncrement(void) const
Get the rounding increment.
UnicodeString & getNegativeSuffix(UnicodeString &result) const
Get the negative suffix.
UBool isParseNoExponent() const
Returns whether to ignore exponents when parsing.
DecimalFormat & operator=(const DecimalFormat &rhs)
Assignment operator.
virtual UnicodeString & toPattern(UnicodeString &result) const
Synthesizes a pattern string that represents the current state of this Format object.
UBool isDecimalSeparatorAlwaysShown(void) const
Allows you to get the behavior of the decimal separator with integers.
virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo *toAdopt)
Sets the currency plural format information, which is generally not changed by the programmer or user...
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.
DecimalFormat(const UnicodeString &pattern, DecimalFormatSymbols *symbolsToAdopt, UNumberFormatStyle style, UErrorCode &status)
This API is for ICU use only.
int32_t getMultiplierScale(void) const
Gets the power of ten by which number should be multiplied before formatting, which can be combined w...
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:64
"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:60
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:295
The result of a number formatting operation.
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.
Definition umachine.h:261
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition umachine.h:129
UNumberFormatAttribute
The possible UNumberFormat numeric attributes.
Definition unum.h:969
UNumberFormatStyle
The possible number format styles.
Definition unum.h:146
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:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:301