1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * created on: 2015jan06
11 * created by: Travis Keep
14 #ifndef __DIGITFORMATTER_H__
15 #define __DIGITFORMATTER_H__
17 #include "unicode/uobject.h"
19 #if !UCONFIG_NO_FORMATTING
21 #include "unicode/utypes.h"
22 #include "unicode/unistr.h"
23 #include "digitaffix.h"
27 class DecimalFormatSymbols;
32 class FieldPositionHandler;
33 class IntDigitCountRange;
35 class VisibleDigitsWithExponent;
38 * Various options for formatting in fixed point.
40 class U_I18N_API DigitFormatterOptions : public UMemory {
42 DigitFormatterOptions() : fAlwaysShowDecimal(FALSE) { }
45 * Returns TRUE if this object equals rhs.
47 UBool equals(const DigitFormatterOptions &rhs) const {
49 fAlwaysShowDecimal == rhs.fAlwaysShowDecimal);
53 * Returns TRUE if these options allow for fast formatting of
56 UBool isFastFormattable() const {
57 return (fAlwaysShowDecimal == FALSE);
61 * If TRUE, show the decimal separator even when there are no fraction
62 * digits. default is FALSE.
64 UBool fAlwaysShowDecimal;
68 * Various options for formatting an integer.
70 class U_I18N_API DigitFormatterIntOptions : public UMemory {
72 DigitFormatterIntOptions() : fAlwaysShowSign(FALSE) { }
75 * Returns TRUE if this object equals rhs.
77 UBool equals(const DigitFormatterIntOptions &rhs) const {
78 return (fAlwaysShowSign == rhs.fAlwaysShowSign);
82 * If TRUE, always prefix the integer with its sign even if the number is
83 * positive. Default is FALSE.
85 UBool fAlwaysShowSign;
89 * Options for formatting in scientific notation.
91 class U_I18N_API SciFormatterOptions : public UMemory {
95 * Returns TRUE if this object equals rhs.
97 UBool equals(const SciFormatterOptions &rhs) const {
98 return (fMantissa.equals(rhs.fMantissa) &&
99 fExponent.equals(rhs.fExponent));
103 * Options for formatting the mantissa.
105 DigitFormatterOptions fMantissa;
108 * Options for formatting the exponent.
110 DigitFormatterIntOptions fExponent;
115 * Does fixed point formatting.
117 * This class only does fixed point formatting. It does no rounding before
120 class U_I18N_API DigitFormatter : public UMemory {
124 * Decimal separator is period (.), Plus sign is plus (+),
125 * minus sign is minus (-), grouping separator is comma (,), digits are 0-9.
130 * Let symbols determine the digits, decimal separator,
131 * plus and mius sign, grouping separator, and possibly other settings.
133 DigitFormatter(const DecimalFormatSymbols &symbols);
136 * Change what this instance uses for digits, decimal separator,
137 * plus and mius sign, grouping separator, and possibly other settings
138 * according to symbols.
140 void setDecimalFormatSymbols(const DecimalFormatSymbols &symbols);
143 * Change what this instance uses for digits, decimal separator,
144 * plus and mius sign, grouping separator, and possibly other settings
145 * according to symbols in the context of monetary amounts.
147 void setDecimalFormatSymbolsForMonetary(const DecimalFormatSymbols &symbols);
150 * Fixed point formatting.
152 * @param positiveDigits the value to format
153 * Negative sign can be present, but it won't show.
154 * @param grouping controls how digit grouping is done
155 * @param options formatting options
156 * @param handler records field positions
157 * @param appendTo formatted value appended here.
160 UnicodeString &format(
161 const VisibleDigits &positiveDigits,
162 const DigitGrouping &grouping,
163 const DigitFormatterOptions &options,
164 FieldPositionHandler &handler,
165 UnicodeString &appendTo) const;
168 * formats in scientifc notation.
169 * @param positiveDigits the value to format.
170 * Negative sign can be present, but it won't show.
171 * @param options formatting options
172 * @param handler records field positions.
173 * @param appendTo formatted value appended here.
175 UnicodeString &format(
176 const VisibleDigitsWithExponent &positiveDigits,
177 const SciFormatterOptions &options,
178 FieldPositionHandler &handler,
179 UnicodeString &appendTo) const;
182 * Fixed point formatting of integers.
183 * Always performed with no grouping and no decimal point.
185 * @param positiveValue the value to format must be positive.
186 * @param range specifies minimum and maximum number of digits.
187 * @param handler records field positions
188 * @param appendTo formatted value appended here.
191 UnicodeString &formatPositiveInt32(
192 int32_t positiveValue,
193 const IntDigitCountRange &range,
194 FieldPositionHandler &handler,
195 UnicodeString &appendTo) const;
198 * Counts how many code points are needed for fixed formatting.
199 * If digits is negative, the negative sign is not included in the count.
202 const VisibleDigits &digits,
203 const DigitGrouping &grouping,
204 const DigitFormatterOptions &options) const;
207 * Counts how many code points are needed for scientific formatting.
208 * If digits is negative, the negative sign is not included in the count.
211 const VisibleDigitsWithExponent &digits,
212 const SciFormatterOptions &options) const;
215 * Returns TRUE if this object equals rhs.
217 UBool equals(const DigitFormatter &rhs) const;
220 UChar32 fLocalizedDigits[10];
221 UnicodeString fGroupingSeparator;
222 UnicodeString fDecimal;
223 UnicodeString fNegativeSign;
224 UnicodeString fPositiveSign;
225 DigitAffix fInfinity;
227 UBool fIsStandardDigits;
228 UnicodeString fExponent;
229 UBool isStandardDigits() const;
231 UnicodeString &formatDigits(
232 const uint8_t *digits,
234 const IntDigitCountRange &range,
236 FieldPositionHandler &handler,
237 UnicodeString &appendTo) const;
239 void setOtherDecimalFormatSymbols(const DecimalFormatSymbols &symbols);
242 const VisibleDigits &exponent,
243 const DigitInterval &mantissaInterval,
244 const SciFormatterOptions &options) const;
246 UnicodeString &formatNaN(
247 FieldPositionHandler &handler,
248 UnicodeString &appendTo) const {
249 return fNan.format(handler, appendTo);
252 int32_t countChar32ForNaN() const {
253 return fNan.toString().countChar32();
256 UnicodeString &formatInfinity(
257 FieldPositionHandler &handler,
258 UnicodeString &appendTo) const {
259 return fInfinity.format(handler, appendTo);
262 int32_t countChar32ForInfinity() const {
263 return fInfinity.toString().countChar32();
266 UnicodeString &formatExponent(
267 const VisibleDigits &digits,
268 const DigitFormatterIntOptions &options,
271 FieldPositionHandler &handler,
272 UnicodeString &appendTo) const;
275 const DigitGrouping &grouping,
276 const DigitInterval &interval,
277 const DigitFormatterOptions &options) const;
279 int32_t countChar32ForExponent(
280 const VisibleDigits &exponent,
281 const DigitFormatterIntOptions &options) const;
287 #endif /* #if !UCONFIG_NO_FORMATTING */
288 #endif // __DIGITFORMATTER_H__