ICU 62.1 62.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"
36#if !UCONFIG_NO_FORMATTING
37
38#include "unicode/dcfmtsym.h"
39#include "unicode/numfmt.h"
40#include "unicode/locid.h"
41#include "unicode/fpositer.h"
42#include "unicode/stringpiece.h"
43#include "unicode/curramt.h"
44#include "unicode/enumset.h"
45
47
48class CurrencyPluralInfo;
49class CompactDecimalFormat;
50
51namespace number {
52class LocalizedNumberFormatter;
53class FormattedNumber;
54namespace impl {
55class DecimalQuantity;
56struct DecimalFormatFields;
57}
58}
59
60namespace numparse {
61namespace impl {
62class NumberParserImpl;
63}
64}
65
66// explicit template instantiation. see digitlst.h
67// (When building DLLs for Windows this is required.)
68#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
69template class U_I18N_API EnumSet<UNumberFormatAttribute,
72#endif
73
673 public:
679 kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
680 };
681
699
718
741
742#ifndef U_HIDE_INTERNAL_API
743
758
759#if UCONFIG_HAVE_PARSEALLINPUT
760
765
766#endif
767
768#endif /* U_HIDE_INTERNAL_API */
769
770 private:
771
777
778 public:
779
791
802
803
811
820
829
854
876
884
892
898
906 Format* clone(void) const U_OVERRIDE;
907
917
918
919 using NumberFormat::format;
920
932 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
933
934#ifndef U_HIDE_INTERNAL_API
947 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
949#endif /* U_HIDE_INTERNAL_API */
950
966
978 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
979
980#ifndef U_HIDE_INTERNAL_API
992 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
994#endif /* U_HIDE_INTERNAL_API */
995
1011
1023 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const U_OVERRIDE;
1024
1025#ifndef U_HIDE_INTERNAL_API
1037 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
1039#endif /* U_HIDE_INTERNAL_API */
1040
1056
1075
1076#ifndef U_HIDE_INTERNAL_API
1077
1093 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1095
1111 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
1113
1114#endif // U_HIDE_INTERNAL_API
1115
1116 using NumberFormat::parse;
1117
1139
1160
1169
1177
1184 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
1185
1186
1193 virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const;
1194
1202
1210
1211
1221
1230
1240
1249
1259
1268
1278
1287
1288#ifndef U_HIDE_INTERNAL_API
1294#endif /* U_HIDE_INTERNAL_API */
1295
1301 virtual void setSignAlwaysShown(UBool value);
1302
1315 int32_t getMultiplier(void) const;
1316
1330 virtual void setMultiplier(int32_t newValue);
1331
1332#ifndef U_HIDE_DRAFT_API
1345 int32_t getMultiplierScale(void) const;
1346#endif /* U_HIDE_DRAFT_API */
1347
1367 virtual void setMultiplierScale(int32_t newValue);
1368
1378 virtual double getRoundingIncrement(void) const;
1379
1391 virtual void setRoundingIncrement(double newValue);
1392
1402
1411 virtual void setRoundingMode(ERoundingMode roundingMode) U_OVERRIDE;
1412
1424 virtual int32_t getFormatWidth(void) const;
1425
1440 virtual void setFormatWidth(int32_t width);
1441
1455
1471
1487 virtual EPadPosition getPadPosition(void) const;
1488
1506
1517 virtual UBool isScientificNotation(void) const;
1518
1535
1546 virtual int8_t getMinimumExponentDigits(void) const;
1547
1561
1575
1590
1602 int32_t getGroupingSize(void) const;
1603
1615 virtual void setGroupingSize(int32_t newValue);
1616
1635 int32_t getSecondaryGroupingSize(void) const;
1636
1648 virtual void setSecondaryGroupingSize(int32_t newValue);
1649
1650#ifndef U_HIDE_INTERNAL_API
1651
1674
1675#endif /* U_HIDE_INTERNAL_API */
1676
1677 /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual. */
1686 virtual void setMinimumGroupingDigits(int32_t newValue);
1687
1688
1698
1708
1716
1727
1734 virtual UBool isParseNoExponent() const;
1735
1744 virtual void setParseNoExponent(UBool value);
1745
1753
1763 virtual void setParseCaseSensitive(UBool value);
1764
1773
1781
1782
1794
1806
1837
1846 virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
1847
1880
1891
1892
1903
1914
1925
1936
1945
1954
1967
1980
1988
1997
2011
2017 virtual void setCurrency(const char16_t* theCurrency);
2018
2027
2033
2034#ifndef U_HIDE_INTERNAL_API
2035
2041 void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
2042 UErrorCode& status) const;
2043
2050 void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
2051 UErrorCode& status) const;
2052
2053#endif
2054
2055#ifndef U_HIDE_DRAFT_API
2080#endif /* U_HIDE_DRAFT_API */
2081
2094
2107
2108 private:
2109
2111 void touch(UErrorCode& status);
2112
2114 void touchNoError();
2115
2127 void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
2129
2130 const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
2131
2132 const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
2133
2134 static void fieldPositionHelper(const number::FormattedNumber& formatted, FieldPosition& fieldPosition,
2135 int32_t offset, UErrorCode& status);
2136
2137 static void fieldPositionIteratorHelper(const number::FormattedNumber& formatted,
2138 FieldPositionIterator* fpi, int32_t offset, UErrorCode& status);
2139
2140 void setupFastFormat();
2141
2142 bool fastFormatDouble(double input, UnicodeString& output) const;
2143
2144 bool fastFormatInt64(int64_t input, UnicodeString& output) const;
2145
2146 void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
2147
2148 //=====================================================================================//
2149 // INSTANCE FIELDS //
2150 //=====================================================================================//
2151
2152 // Only one instance field: keep all fields inside of an implementation class defined in number_mapper.h
2153 number::impl::DecimalFormatFields* fields;
2154
2155 // Allow child class CompactDecimalFormat to access fProperties:
2156 friend class CompactDecimalFormat;
2157
2158};
2159
2161
2162#endif /* #if !UCONFIG_NO_FORMATTING */
2163
2164#endif // _DECIMFMT
2165//eof
A currency together with a numeric amount, such as 200 USD.
Definition curramt.h:37
This class represents the information needed by DecimalFormat to format currency plural,...
Definition currpinf.h:46
This class represents the set of symbols needed by DecimalFormat to format numbers.
Definition dcfmtsym.h:84
void setGroupingUsed(UBool newValue)
Set whether or not grouping will be used in this format.
UnicodeString & getNegativePrefix(UnicodeString &result) const
Get the negative prefix.
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.
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.
virtual void setFormatFailIfMoreThanMaxDigits(UBool value)
{@icu} Sets whether truncation of high-order integer digits should result in an error.
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.
virtual ERoundingMode getRoundingMode(void) const
Get the rounding mode.
virtual void setMultiplierScale(int32_t newValue)
Sets a power of ten by which number should be multiplied before formatting, which can be combined wit...
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.
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.
virtual void setSignAlwaysShown(UBool value)
Set whether to show the plus sign on positive (non-negative) numbers; for example,...
UnicodeString & getPositiveSuffix(UnicodeString &result) const
Get the positive suffix.
virtual void setRoundingMode(ERoundingMode roundingMode)
Set the rounding mode.
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.
virtual void setRoundingIncrement(double newValue)
Set the rounding increment.
void setLenient(UBool enable)
Sets whether lenient parsing should be enabled (it is off by default).
UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a decimal number.
virtual void setCurrencyPluralInfo(const CurrencyPluralInfo &info)
Sets the currency plural format information, which is generally not changed by the programmer or user...
virtual void setParseCaseSensitive(UBool value)
{@icu} Whether to pay attention to case when parsing; default is to ignore case (perform case-folding...
void setMaximumFractionDigits(int32_t newValue)
Sets the maximum number of digits allowed in the fraction portion of a number.
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.
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.
int32_t getMinimumGroupingDigits() const
Returns the minimum number of grouping digits.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos) const
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.
virtual int8_t getMinimumExponentDigits(void) const
Return the minimum exponent digits that will be shown.
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format an int64 number using base-10 representation.
virtual UBool isParseNoExponent() const
{@icu} Returns whether to ignore exponents when parsing.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a 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 Context object used to display currency.
virtual EPadPosition getPadPosition(void) const
Get the position at which padding will take place.
DecimalFormat(const UnicodeString &pattern, UErrorCode &status)
Create a DecimalFormat from the given pattern and the symbols for the default locale.
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.
UClassID getDynamicClassID(void) const
Returns a unique class ID POLYMORPHICALLY.
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.
void setMinimumFractionDigits(int32_t newValue)
Sets the minimum number of digits allowed in the fraction portion of a number.
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos) const
Format an int64 number using base-10 representation.
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".
virtual void setPositiveSuffix(const UnicodeString &newValue)
Set the positive suffix.
virtual void setGroupingSize(int32_t newValue)
Set the grouping size.
~DecimalFormat()
Destructor.
const number::LocalizedNumberFormatter & toNumberFormatter() const
Converts this DecimalFormat to a NumberFormatter.
virtual UBool isFormatFailIfMoreThanMaxDigits() const
{@icu} Returns whether truncation of high-order integer digits should result in an error.
void setMaximumIntegerDigits(int32_t newValue)
Sets the maximum number of digits allowed in the integer portion of a number.
UBool areSignificantDigitsUsed() const
Returns true if significant digits are in use, or false if integer and fraction digit counts are in u...
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 setParseIntegerOnly(UBool value)
Sets whether or not numbers should be parsed as integers only.
void parse(const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const
Parse the given string using this object's choices.
virtual void setParseNoExponent(UBool value)
{@icu} Specifies whether to stop parsing when an exponent separator is encountered.
UnicodeString & format(int64_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an int64 number using base-10 representation.
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.
void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode *ec)
Sets the Currency Context 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...
void setCurrency(const char16_t *theCurrency, UErrorCode &ec)
Sets the currency used to display currency amounts.
UnicodeString & format(double number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a double or long number using base-10 representation.
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:678
UnicodeString & format(StringPiece number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a decimal number.
virtual double getRoundingIncrement(void) const
Get the rounding increment.
virtual UBool isParseCaseSensitive() const
{@icu} Returns whether parsing is sensitive to case (lowercase/uppercase).
UnicodeString & getNegativeSuffix(UnicodeString &result) const
Get the negative suffix.
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.
CurrencyAmount * parseCurrency(const UnicodeString &text, ParsePosition &pos) const
Parses text from the given string as a currency amount.
virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo *toAdopt)
Sets the currency plural format information, which is generally not changed by the programmer or user...
virtual void setMinimumGroupingDigits(int32_t newValue)
Sets the minimum grouping digits.
virtual void setPadCharacter(const UnicodeString &padChar)
Set the character used to pad to the format width.
void setMinimumIntegerDigits(int32_t newValue)
Sets the minimum number of digits allowed in the integer portion of a number.
DecimalFormat(const UnicodeString &pattern, const DecimalFormatSymbols &symbols, UErrorCode &status)
Create a DecimalFormat from the given pattern and symbols.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a long number using base-10 representation.
UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format a double or long number using base-10 representation.
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...
UnicodeString & format(const number::impl::DecimalQuantity &number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Format a decimal number.
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition fpositer.h:55
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition fieldpos.h:108
Base class for all formats.
Definition format.h:96
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:62
"Smart pointer" base class; do not use directly: use LocalPointer etc.
ERoundingMode
Rounding mode.
Definition numfmt.h:185
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition parsepos.h:49
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:54
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:287
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:236
UNumberFormatAttribute
The possible UNumberFormat numeric attributes.
Definition unum.h:960
@ UNUM_LIMIT_BOOLEAN_ATTRIBUTE
Limit of boolean attributes.
Definition unum.h:1077
@ UNUM_MAX_NONBOOLEAN_ATTRIBUTE
One below the first bitfield-boolean item.
Definition unum.h:1048
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:93
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition utypes.h:396
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:360
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:138
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:137