2 ********************************************************************************
3 * Copyright (C) 2012-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
7 * File COMPACTDECIMALFORMAT.H
8 ********************************************************************************
11 #ifndef __COMPACT_DECIMAL_FORMAT_H__
12 #define __COMPACT_DECIMAL_FORMAT_H__
14 #include "unicode/utypes.h"
17 * \brief C++ API: Formats decimal numbers in compact form.
20 #if !UCONFIG_NO_FORMATTING
21 #ifndef U_HIDE_DRAFT_API
23 #include "unicode/decimfmt.h"
32 * The CompactDecimalFormat produces abbreviated numbers, suitable for display in
33 * environments will limited real estate. For example, 'Hits: 1.2B' instead of
34 * 'Hits: 1,200,000,000'. The format will be appropriate for the given language,
35 * such as "1,2 Mrd." for German.
37 * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345),
38 * the result will be short for supported languages. However, the result may
39 * sometimes exceed 7 characters, such as when there are combining marks or thin
40 * characters. In such cases, the visual width in fonts should still be short.
42 * By default, there are 3 significant digits. After creation, if more than
43 * three significant digits are set (with setMaximumSignificantDigits), or if a
44 * fixed number of digits are set (with setMaximumIntegerDigits or
45 * setMaximumFractionDigits), then result may be wider.
47 * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR.
48 * Resetting the pattern prefixes or suffixes is not supported; the method calls
53 class U_I18N_API CompactDecimalFormat
: public DecimalFormat
{
57 * Returns a compact decimal instance for specified locale.
58 * @param inLocale the given locale.
59 * @param style whether to use short or long style.
60 * @param status error code returned here.
63 static CompactDecimalFormat
* U_EXPORT2
createInstance(
64 const Locale
& inLocale
, UNumberCompactStyle style
, UErrorCode
& status
);
69 * @param source the DecimalFormat object to be copied from.
72 CompactDecimalFormat(const CompactDecimalFormat
& source
);
78 virtual ~CompactDecimalFormat();
81 * Assignment operator.
83 * @param rhs the DecimalFormat object to be copied.
86 CompactDecimalFormat
& operator=(const CompactDecimalFormat
& rhs
);
89 * Clone this Format object polymorphically. The caller owns the
90 * result and should delete it when done.
92 * @return a polymorphic copy of this CompactDecimalFormat.
95 virtual Format
* clone() const;
98 * Return TRUE if the given Format objects are semantically equal.
99 * Objects of different subclasses are considered unequal.
101 * @param other the object to be compared with.
102 * @return TRUE if the given Format objects are semantically equal.
105 virtual UBool
operator==(const Format
& other
) const;
108 using DecimalFormat::format
;
111 * Format a double or long number using base-10 representation.
113 * @param number The value to be formatted.
114 * @param appendTo Output parameter to receive result.
115 * Result is appended to existing contents.
116 * @param pos On input: an alignment field, if desired.
117 * On output: the offsets of the alignment field.
118 * @return Reference to 'appendTo' parameter.
121 virtual UnicodeString
& format(double number
,
122 UnicodeString
& appendTo
,
123 FieldPosition
& pos
) const;
126 * Format a double or long number using base-10 representation.
127 * Currently sets status to U_UNSUPPORTED_ERROR.
129 * @param number The value to be formatted.
130 * @param appendTo Output parameter to receive result.
131 * Result is appended to existing contents.
132 * @param posIter On return, can be used to iterate over positions
133 * of fields generated by this format call.
135 * @param status Output param filled with success/failure status.
136 * @return Reference to 'appendTo' parameter.
139 virtual UnicodeString
& format(double number
,
140 UnicodeString
& appendTo
,
141 FieldPositionIterator
* posIter
,
142 UErrorCode
& status
) const;
145 * Format an int64 number using base-10 representation.
147 * @param number The value to be formatted.
148 * @param appendTo Output parameter to receive result.
149 * Result is appended to existing contents.
150 * @param pos On input: an alignment field, if desired.
151 * On output: the offsets of the alignment field.
152 * @return Reference to 'appendTo' parameter.
155 virtual UnicodeString
& format(int64_t number
,
156 UnicodeString
& appendTo
,
157 FieldPosition
& pos
) const;
160 * Format an int64 number using base-10 representation.
161 * Currently sets status to U_UNSUPPORTED_ERROR
163 * @param number The value to be formatted.
164 * @param appendTo Output parameter to receive result.
165 * Result is appended to existing contents.
166 * @param posIter On return, can be used to iterate over positions
167 * of fields generated by this format call.
169 * @param status Output param filled with success/failure status.
170 * @return Reference to 'appendTo' parameter.
173 virtual UnicodeString
& format(int64_t number
,
174 UnicodeString
& appendTo
,
175 FieldPositionIterator
* posIter
,
176 UErrorCode
& status
) const;
179 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
180 * The syntax of the unformatted number is a "numeric string"
181 * as defined in the Decimal Arithmetic Specification, available at
182 * http://speleotrove.com/decimal
184 * @param number The unformatted number, as a string.
185 * @param appendTo Output parameter to receive result.
186 * Result is appended to existing contents.
187 * @param posIter On return, can be used to iterate over positions
188 * of fields generated by this format call.
190 * @param status Output param filled with success/failure status.
191 * @return Reference to 'appendTo' parameter.
194 virtual UnicodeString
& format(const StringPiece
&number
,
195 UnicodeString
& appendTo
,
196 FieldPositionIterator
* posIter
,
197 UErrorCode
& status
) const;
200 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
201 * The number is a DigitList wrapper onto a floating point decimal number.
202 * The default implementation in NumberFormat converts the decimal number
203 * to a double and formats that.
205 * @param number The number, a DigitList format Decimal Floating Point.
206 * @param appendTo Output parameter to receive result.
207 * Result is appended to existing contents.
208 * @param posIter On return, can be used to iterate over positions
209 * of fields generated by this format call.
210 * @param status Output param filled with success/failure status.
211 * @return Reference to 'appendTo' parameter.
214 virtual UnicodeString
& format(const DigitList
&number
,
215 UnicodeString
& appendTo
,
216 FieldPositionIterator
* posIter
,
217 UErrorCode
& status
) const;
220 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR.
221 * The number is a DigitList wrapper onto a floating point decimal number.
222 * The default implementation in NumberFormat converts the decimal number
223 * to a double and formats that.
225 * @param number The number, a DigitList format Decimal Floating Point.
226 * @param appendTo Output parameter to receive result.
227 * Result is appended to existing contents.
228 * @param pos On input: an alignment field, if desired.
229 * On output: the offsets of the alignment field.
230 * @param status Output param filled with success/failure status.
231 * @return Reference to 'appendTo' parameter.
234 virtual UnicodeString
& format(const DigitList
&number
,
235 UnicodeString
& appendTo
,
237 UErrorCode
& status
) const;
240 * CompactDecimalFormat does not support parsing. This implementation
242 * @param text Unused.
243 * @param result Does not change.
244 * @param parsePosition Does not change.
248 virtual void parse(const UnicodeString
& text
,
250 ParsePosition
& parsePosition
) const;
253 * CompactDecimalFormat does not support parsing. This implementation
254 * sets status to U_UNSUPPORTED_ERROR
256 * @param text Unused.
257 * @param result Does not change.
258 * @param status Always set to U_UNSUPPORTED_ERROR.
261 virtual void parse(const UnicodeString
& text
,
263 UErrorCode
& status
) const;
265 /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual */
267 * Parses text from the given string as a currency amount. Unlike
268 * the parse() method, this method will attempt to parse a generic
269 * currency name, searching for a match of this object's locale's
270 * currency display names, or for a 3-letter ISO currency code.
271 * This method will fail if this format is not a currency format,
272 * that is, if it does not contain the currency pattern symbol
273 * (U+00A4) in its prefix or suffix. This implementation always returns
276 * @param text the string to parse
277 * @param pos input-output position; on input, the position within text
278 * to match; must have 0 <= pos.getIndex() < text.length();
279 * on output, the position after the last matched character.
280 * If the parse fails, the position in unchanged upon output.
281 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
282 * object (owned by the caller) containing information about
283 * the parsed currency; if parse fails, this is NULL.
286 virtual CurrencyAmount
* parseCurrency(const UnicodeString
& text
,
287 ParsePosition
& pos
) const;
290 * Return the class ID for this class. This is useful only for
291 * comparing to a return value from getDynamicClassID(). For example:
293 * . Base* polymorphic_pointer = createPolymorphicObject();
294 * . if (polymorphic_pointer->getDynamicClassID() ==
295 * . Derived::getStaticClassID()) ...
297 * @return The class ID for all objects of this class.
300 static UClassID U_EXPORT2
getStaticClassID();
303 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
304 * This method is to implement a simple version of RTTI, since not all
305 * C++ compilers support genuine RTTI. Polymorphic operator==() and
306 * clone() methods call this method.
308 * @return The class ID for this object. All objects of a
309 * given class have the same class ID. Objects of
310 * other classes have different class IDs.
313 virtual UClassID
getDynamicClassID() const;
317 const UHashtable
* _unitsByVariant
;
318 const double* _divisors
;
319 PluralRules
* _pluralRules
;
321 // Default constructor not implemented.
322 CompactDecimalFormat(const DecimalFormat
&, const UHashtable
* unitsByVariant
, const double* divisors
, PluralRules
* pluralRules
);
324 UBool
eqHelper(const CompactDecimalFormat
& that
) const;
329 #endif /* U_HIDE_DRAFT_API */
330 #endif /* #if !UCONFIG_NO_FORMATTING */
332 #endif // __COMPACT_DECIMAL_FORMAT_H__