2 ********************************************************************************
3 * Copyright (C) 2012-2014, 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
22 #include "unicode/decimfmt.h"
31 * The CompactDecimalFormat produces abbreviated numbers, suitable for display in
32 * environments will limited real estate. For example, 'Hits: 1.2B' instead of
33 * 'Hits: 1,200,000,000'. The format will be appropriate for the given language,
34 * such as "1,2 Mrd." for German.
36 * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345),
37 * the result will be short for supported languages. However, the result may
38 * sometimes exceed 7 characters, such as when there are combining marks or thin
39 * characters. In such cases, the visual width in fonts should still be short.
41 * By default, there are 3 significant digits. After creation, if more than
42 * three significant digits are set (with setMaximumSignificantDigits), or if a
43 * fixed number of digits are set (with setMaximumIntegerDigits or
44 * setMaximumFractionDigits), then result may be wider.
46 * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR.
47 * Resetting the pattern prefixes or suffixes is not supported; the method calls
52 class U_I18N_API CompactDecimalFormat
: public DecimalFormat
{
56 * Returns a compact decimal instance for specified locale.
57 * @param inLocale the given locale.
58 * @param style whether to use short or long style.
59 * @param status error code returned here.
62 static CompactDecimalFormat
* U_EXPORT2
createInstance(
63 const Locale
& inLocale
, UNumberCompactStyle style
, UErrorCode
& status
);
68 * @param source the DecimalFormat object to be copied from.
71 CompactDecimalFormat(const CompactDecimalFormat
& source
);
77 virtual ~CompactDecimalFormat();
80 * Assignment operator.
82 * @param rhs the DecimalFormat object to be copied.
85 CompactDecimalFormat
& operator=(const CompactDecimalFormat
& rhs
);
88 * Clone this Format object polymorphically. The caller owns the
89 * result and should delete it when done.
91 * @return a polymorphic copy of this CompactDecimalFormat.
94 virtual Format
* clone() const;
97 * Return TRUE if the given Format objects are semantically equal.
98 * Objects of different subclasses are considered unequal.
100 * @param other the object to be compared with.
101 * @return TRUE if the given Format objects are semantically equal.
104 virtual UBool
operator==(const Format
& other
) const;
107 using DecimalFormat::format
;
110 * Format a double or long number using base-10 representation.
112 * @param number The value to be formatted.
113 * @param appendTo Output parameter to receive result.
114 * Result is appended to existing contents.
115 * @param pos On input: an alignment field, if desired.
116 * On output: the offsets of the alignment field.
117 * @return Reference to 'appendTo' parameter.
120 virtual UnicodeString
& format(double number
,
121 UnicodeString
& appendTo
,
122 FieldPosition
& pos
) const;
125 * Format a double or long number using base-10 representation.
126 * Currently sets status to U_UNSUPPORTED_ERROR.
128 * @param number The value to be formatted.
129 * @param appendTo Output parameter to receive result.
130 * Result is appended to existing contents.
131 * @param posIter On return, can be used to iterate over positions
132 * of fields generated by this format call.
134 * @param status Output param filled with success/failure status.
135 * @return Reference to 'appendTo' parameter.
138 virtual UnicodeString
& format(double number
,
139 UnicodeString
& appendTo
,
140 FieldPositionIterator
* posIter
,
141 UErrorCode
& status
) const;
144 * Format an int64 number using base-10 representation.
146 * @param number The value to be formatted.
147 * @param appendTo Output parameter to receive result.
148 * Result is appended to existing contents.
149 * @param pos On input: an alignment field, if desired.
150 * On output: the offsets of the alignment field.
151 * @return Reference to 'appendTo' parameter.
154 virtual UnicodeString
& format(int64_t number
,
155 UnicodeString
& appendTo
,
156 FieldPosition
& pos
) const;
159 * Format an int64 number using base-10 representation.
160 * Currently sets status to U_UNSUPPORTED_ERROR
162 * @param number The value to be formatted.
163 * @param appendTo Output parameter to receive result.
164 * Result is appended to existing contents.
165 * @param posIter On return, can be used to iterate over positions
166 * of fields generated by this format call.
168 * @param status Output param filled with success/failure status.
169 * @return Reference to 'appendTo' parameter.
172 virtual UnicodeString
& format(int64_t number
,
173 UnicodeString
& appendTo
,
174 FieldPositionIterator
* posIter
,
175 UErrorCode
& status
) const;
178 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
179 * The syntax of the unformatted number is a "numeric string"
180 * as defined in the Decimal Arithmetic Specification, available at
181 * http://speleotrove.com/decimal
183 * @param number The unformatted number, as a string.
184 * @param appendTo Output parameter to receive result.
185 * Result is appended to existing contents.
186 * @param posIter On return, can be used to iterate over positions
187 * of fields generated by this format call.
189 * @param status Output param filled with success/failure status.
190 * @return Reference to 'appendTo' parameter.
193 virtual UnicodeString
& format(const StringPiece
&number
,
194 UnicodeString
& appendTo
,
195 FieldPositionIterator
* posIter
,
196 UErrorCode
& status
) const;
199 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
200 * The number is a DigitList wrapper onto a floating point decimal number.
201 * The default implementation in NumberFormat converts the decimal number
202 * to a double and formats that.
204 * @param number The number, a DigitList format Decimal Floating Point.
205 * @param appendTo Output parameter to receive result.
206 * Result is appended to existing contents.
207 * @param posIter On return, can be used to iterate over positions
208 * of fields generated by this format call.
209 * @param status Output param filled with success/failure status.
210 * @return Reference to 'appendTo' parameter.
213 virtual UnicodeString
& format(const DigitList
&number
,
214 UnicodeString
& appendTo
,
215 FieldPositionIterator
* posIter
,
216 UErrorCode
& status
) const;
219 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR.
220 * The number is a DigitList wrapper onto a floating point decimal number.
221 * The default implementation in NumberFormat converts the decimal number
222 * to a double and formats that.
224 * @param number The number, a DigitList format Decimal Floating Point.
225 * @param appendTo Output parameter to receive result.
226 * Result is appended to existing contents.
227 * @param pos On input: an alignment field, if desired.
228 * On output: the offsets of the alignment field.
229 * @param status Output param filled with success/failure status.
230 * @return Reference to 'appendTo' parameter.
233 virtual UnicodeString
& format(const DigitList
&number
,
234 UnicodeString
& appendTo
,
236 UErrorCode
& status
) const;
239 * CompactDecimalFormat does not support parsing. This implementation
241 * @param text Unused.
242 * @param result Does not change.
243 * @param parsePosition Does not change.
247 virtual void parse(const UnicodeString
& text
,
249 ParsePosition
& parsePosition
) const;
252 * CompactDecimalFormat does not support parsing. This implementation
253 * sets status to U_UNSUPPORTED_ERROR
255 * @param text Unused.
256 * @param result Does not change.
257 * @param status Always set to U_UNSUPPORTED_ERROR.
260 virtual void parse(const UnicodeString
& text
,
262 UErrorCode
& status
) const;
265 * Parses text from the given string as a currency amount. Unlike
266 * the parse() method, this method will attempt to parse a generic
267 * currency name, searching for a match of this object's locale's
268 * currency display names, or for a 3-letter ISO currency code.
269 * This method will fail if this format is not a currency format,
270 * that is, if it does not contain the currency pattern symbol
271 * (U+00A4) in its prefix or suffix. This implementation always returns
274 * @param text the string to parse
275 * @param pos input-output position; on input, the position within text
276 * to match; must have 0 <= pos.getIndex() < text.length();
277 * on output, the position after the last matched character.
278 * If the parse fails, the position in unchanged upon output.
279 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
280 * object (owned by the caller) containing information about
281 * the parsed currency; if parse fails, this is NULL.
284 virtual CurrencyAmount
* parseCurrency(const UnicodeString
& text
,
285 ParsePosition
& pos
) const;
288 * Return the class ID for this class. This is useful only for
289 * comparing to a return value from getDynamicClassID(). For example:
291 * . Base* polymorphic_pointer = createPolymorphicObject();
292 * . if (polymorphic_pointer->getDynamicClassID() ==
293 * . Derived::getStaticClassID()) ...
295 * @return The class ID for all objects of this class.
298 static UClassID U_EXPORT2
getStaticClassID();
301 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
302 * This method is to implement a simple version of RTTI, since not all
303 * C++ compilers support genuine RTTI. Polymorphic operator==() and
304 * clone() methods call this method.
306 * @return The class ID for this object. All objects of a
307 * given class have the same class ID. Objects of
308 * other classes have different class IDs.
311 virtual UClassID
getDynamicClassID() const;
315 const UHashtable
* _unitsByVariant
;
316 const double* _divisors
;
317 PluralRules
* _pluralRules
;
319 // Default constructor not implemented.
320 CompactDecimalFormat(const DecimalFormat
&, const UHashtable
* unitsByVariant
, const double* divisors
, PluralRules
* pluralRules
);
322 UBool
eqHelper(const CompactDecimalFormat
& that
) const;
327 #endif /* #if !UCONFIG_NO_FORMATTING */
329 #endif // __COMPACT_DECIMAL_FORMAT_H__