1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 2012-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
9 * File COMPACTDECIMALFORMAT.H
10 ********************************************************************************
13 #ifndef __COMPACT_DECIMAL_FORMAT_H__
14 #define __COMPACT_DECIMAL_FORMAT_H__
16 #include "unicode/utypes.h"
19 * \brief C++ API: Formats decimal numbers in compact form.
22 #if !UCONFIG_NO_FORMATTING
24 #include "unicode/decimfmt.h"
28 #if U_SHOW_CPLUSPLUS_API
34 * The CompactDecimalFormat produces abbreviated numbers, suitable for display in
35 * environments will limited real estate. For example, 'Hits: 1.2B' instead of
36 * 'Hits: 1,200,000,000'. The format will be appropriate for the given language,
37 * such as "1,2 Mrd." for German.
39 * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345),
40 * the result will be short for supported languages. However, the result may
41 * sometimes exceed 7 characters, such as when there are combining marks or thin
42 * characters. In such cases, the visual width in fonts should still be short.
44 * By default, there are 3 significant digits. After creation, if more than
45 * three significant digits are set (with setMaximumSignificantDigits), or if a
46 * fixed number of digits are set (with setMaximumIntegerDigits or
47 * setMaximumFractionDigits), then result may be wider.
49 * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR.
50 * Resetting the pattern prefixes or suffixes is not supported; the method calls
55 class U_I18N_API CompactDecimalFormat
: public DecimalFormat
{
59 * Returns a compact decimal instance for specified locale.
60 * @param inLocale the given locale.
61 * @param style whether to use short or long style.
62 * @param status error code returned here.
65 static CompactDecimalFormat
* U_EXPORT2
createInstance(
66 const Locale
& inLocale
, UNumberCompactStyle style
, UErrorCode
& status
);
71 * @param source the DecimalFormat object to be copied from.
74 CompactDecimalFormat(const CompactDecimalFormat
& source
);
80 virtual ~CompactDecimalFormat();
83 * Assignment operator.
85 * @param rhs the DecimalFormat object to be copied.
88 CompactDecimalFormat
& operator=(const CompactDecimalFormat
& rhs
);
91 * Clone this Format object polymorphically. The caller owns the
92 * result and should delete it when done.
94 * @return a polymorphic copy of this CompactDecimalFormat.
97 virtual Format
* clone() const;
100 * Return TRUE if the given Format objects are semantically equal.
101 * Objects of different subclasses are considered unequal.
103 * @param other the object to be compared with.
104 * @return TRUE if the given Format objects are semantically equal.
107 virtual UBool
operator==(const Format
& other
) const;
110 using DecimalFormat::format
;
113 * Format a double or long number using base-10 representation.
115 * @param number The value to be formatted.
116 * @param appendTo Output parameter to receive result.
117 * Result is appended to existing contents.
118 * @param pos On input: an alignment field, if desired.
119 * On output: the offsets of the alignment field.
120 * @return Reference to 'appendTo' parameter.
123 virtual UnicodeString
& format(double number
,
124 UnicodeString
& appendTo
,
125 FieldPosition
& pos
) const;
128 * Format a double or long number using base-10 representation.
130 * @param number The value to be formatted.
131 * @param appendTo Output parameter to receive result.
132 * Result is appended to existing contents.
133 * @param pos On input: an alignment field, if desired.
134 * On output: the offsets of the alignment field.
136 * @return Reference to 'appendTo' parameter.
139 virtual UnicodeString
& format(double number
,
140 UnicodeString
& appendTo
,
142 UErrorCode
&status
) const;
145 * Format a double or long number using base-10 representation.
146 * Currently sets status to U_UNSUPPORTED_ERROR.
148 * @param number The value to be formatted.
149 * @param appendTo Output parameter to receive result.
150 * Result is appended to existing contents.
151 * @param posIter On return, can be used to iterate over positions
152 * of fields generated by this format call.
154 * @param status Output param filled with success/failure status.
155 * @return Reference to 'appendTo' parameter.
158 virtual UnicodeString
& format(double number
,
159 UnicodeString
& appendTo
,
160 FieldPositionIterator
* posIter
,
161 UErrorCode
& status
) const;
164 * Format a long number using base-10 representation.
166 * @param number The value to be formatted.
167 * @param appendTo Output parameter to receive result.
168 * Result is appended to existing contents.
169 * @param pos On input: an alignment field, if desired.
170 * On output: the offsets of the alignment field.
171 * @return Reference to 'appendTo' parameter.
174 virtual UnicodeString
& format(int32_t number
,
175 UnicodeString
& appendTo
,
176 FieldPosition
& pos
) const;
179 * Format a long number using base-10 representation.
181 * @param number The value to be formatted.
182 * @param appendTo Output parameter to receive result.
183 * Result is appended to existing contents.
184 * @param pos On input: an alignment field, if desired.
185 * On output: the offsets of the alignment field.
186 * @return Reference to 'appendTo' parameter.
189 virtual UnicodeString
& format(int32_t number
,
190 UnicodeString
& appendTo
,
192 UErrorCode
&status
) const;
195 * Format a long number using base-10 representation.
196 * Currently sets status to U_UNSUPPORTED_ERROR
198 * @param number The value to be formatted.
199 * @param appendTo Output parameter to receive result.
200 * Result is appended to existing contents.
201 * @param posIter On return, can be used to iterate over positions
202 * of fields generated by this format call.
204 * @param status Output param filled with success/failure status.
205 * @return Reference to 'appendTo' parameter.
208 virtual UnicodeString
& format(int32_t number
,
209 UnicodeString
& appendTo
,
210 FieldPositionIterator
* posIter
,
211 UErrorCode
& status
) const;
214 * Format an int64 number using base-10 representation.
216 * @param number The value to be formatted.
217 * @param appendTo Output parameter to receive result.
218 * Result is appended to existing contents.
219 * @param pos On input: an alignment field, if desired.
220 * On output: the offsets of the alignment field.
221 * @return Reference to 'appendTo' parameter.
224 virtual UnicodeString
& format(int64_t number
,
225 UnicodeString
& appendTo
,
226 FieldPosition
& pos
) const;
229 * Format an int64 number using base-10 representation.
231 * @param number The value to be formatted.
232 * @param appendTo Output parameter to receive result.
233 * Result is appended to existing contents.
234 * @param pos On input: an alignment field, if desired.
235 * On output: the offsets of the alignment field.
236 * @return Reference to 'appendTo' parameter.
239 virtual UnicodeString
& format(int64_t number
,
240 UnicodeString
& appendTo
,
242 UErrorCode
&status
) const;
245 * Format an int64 number using base-10 representation.
246 * Currently sets status to U_UNSUPPORTED_ERROR
248 * @param number The value to be formatted.
249 * @param appendTo Output parameter to receive result.
250 * Result is appended to existing contents.
251 * @param posIter On return, can be used to iterate over positions
252 * of fields generated by this format call.
254 * @param status Output param filled with success/failure status.
255 * @return Reference to 'appendTo' parameter.
258 virtual UnicodeString
& format(int64_t number
,
259 UnicodeString
& appendTo
,
260 FieldPositionIterator
* posIter
,
261 UErrorCode
& status
) const;
264 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
265 * The syntax of the unformatted number is a "numeric string"
266 * as defined in the Decimal Arithmetic Specification, available at
267 * http://speleotrove.com/decimal
269 * @param number The unformatted number, as a string.
270 * @param appendTo Output parameter to receive result.
271 * Result is appended to existing contents.
272 * @param posIter On return, can be used to iterate over positions
273 * of fields generated by this format call.
275 * @param status Output param filled with success/failure status.
276 * @return Reference to 'appendTo' parameter.
279 virtual UnicodeString
& format(StringPiece number
,
280 UnicodeString
& appendTo
,
281 FieldPositionIterator
* posIter
,
282 UErrorCode
& status
) const;
285 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
286 * The number is a DigitList wrapper onto a floating point decimal number.
287 * The default implementation in NumberFormat converts the decimal number
288 * to a double and formats that.
290 * @param number The number, a DigitList format Decimal Floating Point.
291 * @param appendTo Output parameter to receive result.
292 * Result is appended to existing contents.
293 * @param posIter On return, can be used to iterate over positions
294 * of fields generated by this format call.
295 * @param status Output param filled with success/failure status.
296 * @return Reference to 'appendTo' parameter.
299 virtual UnicodeString
& format(const DigitList
&number
,
300 UnicodeString
& appendTo
,
301 FieldPositionIterator
* posIter
,
302 UErrorCode
& status
) const;
305 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR.
306 * The number is a DigitList wrapper onto a floating point decimal number.
307 * The default implementation in NumberFormat converts the decimal number
308 * to a double and formats that.
310 * @param number The number, a DigitList format Decimal Floating Point.
311 * @param appendTo Output parameter to receive result.
312 * Result is appended to existing contents.
313 * @param pos On input: an alignment field, if desired.
314 * On output: the offsets of the alignment field.
315 * @param status Output param filled with success/failure status.
316 * @return Reference to 'appendTo' parameter.
319 virtual UnicodeString
& format(const DigitList
&number
,
320 UnicodeString
& appendTo
,
322 UErrorCode
& status
) const;
325 * CompactDecimalFormat does not support parsing. This implementation
327 * @param text Unused.
328 * @param result Does not change.
329 * @param parsePosition Does not change.
333 virtual void parse(const UnicodeString
& text
,
335 ParsePosition
& parsePosition
) const;
338 * CompactDecimalFormat does not support parsing. This implementation
339 * sets status to U_UNSUPPORTED_ERROR
341 * @param text Unused.
342 * @param result Does not change.
343 * @param status Always set to U_UNSUPPORTED_ERROR.
346 virtual void parse(const UnicodeString
& text
,
348 UErrorCode
& status
) const;
351 * Parses text from the given string as a currency amount. Unlike
352 * the parse() method, this method will attempt to parse a generic
353 * currency name, searching for a match of this object's locale's
354 * currency display names, or for a 3-letter ISO currency code.
355 * This method will fail if this format is not a currency format,
356 * that is, if it does not contain the currency pattern symbol
357 * (U+00A4) in its prefix or suffix. This implementation always returns
360 * @param text the string to parse
361 * @param pos input-output position; on input, the position within text
362 * to match; must have 0 <= pos.getIndex() < text.length();
363 * on output, the position after the last matched character.
364 * If the parse fails, the position in unchanged upon output.
365 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
366 * object (owned by the caller) containing information about
367 * the parsed currency; if parse fails, this is NULL.
370 virtual CurrencyAmount
* parseCurrency(const UnicodeString
& text
,
371 ParsePosition
& pos
) const;
374 * Return the class ID for this class. This is useful only for
375 * comparing to a return value from getDynamicClassID(). For example:
377 * . Base* polymorphic_pointer = createPolymorphicObject();
378 * . if (polymorphic_pointer->getDynamicClassID() ==
379 * . Derived::getStaticClassID()) ...
381 * @return The class ID for all objects of this class.
384 static UClassID U_EXPORT2
getStaticClassID();
387 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
388 * This method is to implement a simple version of RTTI, since not all
389 * C++ compilers support genuine RTTI. Polymorphic operator==() and
390 * clone() methods call this method.
392 * @return The class ID for this object. All objects of a
393 * given class have the same class ID. Objects of
394 * other classes have different class IDs.
397 virtual UClassID
getDynamicClassID() const;
401 const UHashtable
* _unitsByVariant
;
402 const double* _divisors
;
403 PluralRules
* _pluralRules
;
405 // Default constructor not implemented.
406 CompactDecimalFormat(const DecimalFormat
&, const UHashtable
* unitsByVariant
, const double* divisors
, PluralRules
* pluralRules
);
408 UBool
eqHelper(const CompactDecimalFormat
& that
) const;
412 #endif // U_SHOW_CPLUSPLUS_API
414 #endif /* #if !UCONFIG_NO_FORMATTING */
416 #endif // __COMPACT_DECIMAL_FORMAT_H__