2 *******************************************************************************
3 * Copyright (C) 2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 * created on: 2015jan06
9 * created by: Travis Keep
12 #ifndef __DIGITFORMATTER_H__
13 #define __DIGITFORMATTER_H__
15 #include "unicode/uobject.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/utypes.h"
20 #include "unicode/unistr.h"
21 #include "digitaffix.h"
25 class DecimalFormatSymbols
;
30 class FieldPositionHandler
;
31 class IntDigitCountRange
;
33 class VisibleDigitsWithExponent
;
36 * Various options for formatting in fixed point.
38 class U_I18N_API DigitFormatterOptions
: public UMemory
{
40 DigitFormatterOptions() : fAlwaysShowDecimal(FALSE
) { }
43 * Returns TRUE if this object equals rhs.
45 UBool
equals(const DigitFormatterOptions
&rhs
) const {
47 fAlwaysShowDecimal
== rhs
.fAlwaysShowDecimal
);
51 * Returns TRUE if these options allow for fast formatting of
54 UBool
isFastFormattable() const {
55 return (fAlwaysShowDecimal
== FALSE
);
59 * If TRUE, show the decimal separator even when there are no fraction
60 * digits. default is FALSE.
62 UBool fAlwaysShowDecimal
;
66 * Various options for formatting an integer.
68 class U_I18N_API DigitFormatterIntOptions
: public UMemory
{
70 DigitFormatterIntOptions() : fAlwaysShowSign(FALSE
) { }
73 * Returns TRUE if this object equals rhs.
75 UBool
equals(const DigitFormatterIntOptions
&rhs
) const {
76 return (fAlwaysShowSign
== rhs
.fAlwaysShowSign
);
80 * If TRUE, always prefix the integer with its sign even if the number is
81 * positive. Default is FALSE.
83 UBool fAlwaysShowSign
;
87 * Options for formatting in scientific notation.
89 class U_I18N_API SciFormatterOptions
: public UMemory
{
93 * Returns TRUE if this object equals rhs.
95 UBool
equals(const SciFormatterOptions
&rhs
) const {
96 return (fMantissa
.equals(rhs
.fMantissa
) &&
97 fExponent
.equals(rhs
.fExponent
));
101 * Options for formatting the mantissa.
103 DigitFormatterOptions fMantissa
;
106 * Options for formatting the exponent.
108 DigitFormatterIntOptions fExponent
;
113 * Does fixed point formatting.
115 * This class only does fixed point formatting. It does no rounding before
118 class U_I18N_API DigitFormatter
: public UMemory
{
122 * Decimal separator is period (.), Plus sign is plus (+),
123 * minus sign is minus (-), grouping separator is comma (,), digits are 0-9.
128 * Let symbols determine the digits, decimal separator,
129 * plus and mius sign, grouping separator, and possibly other settings.
131 DigitFormatter(const DecimalFormatSymbols
&symbols
);
134 * Change what this instance uses for digits, decimal separator,
135 * plus and mius sign, grouping separator, and possibly other settings
136 * according to symbols.
138 void setDecimalFormatSymbols(const DecimalFormatSymbols
&symbols
);
141 * Change what this instance uses for digits, decimal separator,
142 * plus and mius sign, grouping separator, and possibly other settings
143 * according to symbols in the context of monetary amounts.
145 void setDecimalFormatSymbolsForMonetary(const DecimalFormatSymbols
&symbols
);
148 * Fixed point formatting.
150 * @param positiveDigits the value to format
151 * Negative sign can be present, but it won't show.
152 * @param grouping controls how digit grouping is done
153 * @param options formatting options
154 * @param handler records field positions
155 * @param appendTo formatted value appended here.
158 UnicodeString
&format(
159 const VisibleDigits
&positiveDigits
,
160 const DigitGrouping
&grouping
,
161 const DigitFormatterOptions
&options
,
162 FieldPositionHandler
&handler
,
163 UnicodeString
&appendTo
) const;
166 * formats in scientifc notation.
167 * @param positiveDigits the value to format.
168 * Negative sign can be present, but it won't show.
169 * @param options formatting options
170 * @param handler records field positions.
171 * @param appendTo formatted value appended here.
173 UnicodeString
&format(
174 const VisibleDigitsWithExponent
&positiveDigits
,
175 const SciFormatterOptions
&options
,
176 FieldPositionHandler
&handler
,
177 UnicodeString
&appendTo
) const;
180 * Fixed point formatting of integers.
181 * Always performed with no grouping and no decimal point.
183 * @param positiveValue the value to format must be positive.
184 * @param range specifies minimum and maximum number of digits.
185 * @param handler records field positions
186 * @param appendTo formatted value appended here.
189 UnicodeString
&formatPositiveInt32(
190 int32_t positiveValue
,
191 const IntDigitCountRange
&range
,
192 FieldPositionHandler
&handler
,
193 UnicodeString
&appendTo
) const;
196 * Counts how many code points are needed for fixed formatting.
197 * If digits is negative, the negative sign is not included in the count.
200 const VisibleDigits
&digits
,
201 const DigitGrouping
&grouping
,
202 const DigitFormatterOptions
&options
) const;
205 * Counts how many code points are needed for scientific formatting.
206 * If digits is negative, the negative sign is not included in the count.
209 const VisibleDigitsWithExponent
&digits
,
210 const SciFormatterOptions
&options
) const;
213 * Returns TRUE if this object equals rhs.
215 UBool
equals(const DigitFormatter
&rhs
) const;
218 UChar32 fLocalizedDigits
[10];
219 UnicodeString fGroupingSeparator
;
220 UnicodeString fDecimal
;
221 UnicodeString fNegativeSign
;
222 UnicodeString fPositiveSign
;
223 DigitAffix fInfinity
;
225 UBool fIsStandardDigits
;
226 UnicodeString fExponent
;
227 UBool
isStandardDigits() const;
229 UnicodeString
&formatDigits(
230 const uint8_t *digits
,
232 const IntDigitCountRange
&range
,
234 FieldPositionHandler
&handler
,
235 UnicodeString
&appendTo
) const;
237 void setOtherDecimalFormatSymbols(const DecimalFormatSymbols
&symbols
);
240 const VisibleDigits
&exponent
,
241 const DigitInterval
&mantissaInterval
,
242 const SciFormatterOptions
&options
) const;
244 UnicodeString
&formatNaN(
245 FieldPositionHandler
&handler
,
246 UnicodeString
&appendTo
) const {
247 return fNan
.format(handler
, appendTo
);
250 int32_t countChar32ForNaN() const {
251 return fNan
.toString().countChar32();
254 UnicodeString
&formatInfinity(
255 FieldPositionHandler
&handler
,
256 UnicodeString
&appendTo
) const {
257 return fInfinity
.format(handler
, appendTo
);
260 int32_t countChar32ForInfinity() const {
261 return fInfinity
.toString().countChar32();
264 UnicodeString
&formatExponent(
265 const VisibleDigits
&digits
,
266 const DigitFormatterIntOptions
&options
,
269 FieldPositionHandler
&handler
,
270 UnicodeString
&appendTo
) const;
273 const DigitGrouping
&grouping
,
274 const DigitInterval
&interval
,
275 const DigitFormatterOptions
&options
) const;
277 int32_t countChar32ForExponent(
278 const VisibleDigits
&exponent
,
279 const DigitFormatterIntOptions
&options
) const;
285 #endif /* #if !UCONFIG_NO_FORMATTING */
286 #endif // __DIGITFORMATTER_H__