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: Compatibility APIs for compact decimal number formatting.
22 #if !UCONFIG_NO_FORMATTING
24 #include "unicode/decimfmt.h"
28 #if U_SHOW_CPLUSPLUS_API
34 * <p><strong>IMPORTANT:</strong> New users are strongly encouraged to see if
35 * numberformatter.h fits their use case. Although not deprecated, this header
36 * is provided for backwards compatibility only.
39 * The CompactDecimalFormat produces abbreviated numbers, suitable for display in
40 * environments will limited real estate. For example, 'Hits: 1.2B' instead of
41 * 'Hits: 1,200,000,000'. The format will be appropriate for the given language,
42 * such as "1,2 Mrd." for German.
44 * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345),
45 * the result will be short for supported languages. However, the result may
46 * sometimes exceed 7 characters, such as when there are combining marks or thin
47 * characters. In such cases, the visual width in fonts should still be short.
49 * By default, there are 3 significant digits. After creation, if more than
50 * three significant digits are set (with setMaximumSignificantDigits), or if a
51 * fixed number of digits are set (with setMaximumIntegerDigits or
52 * setMaximumFractionDigits), then result may be wider.
54 * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR.
55 * Resetting the pattern prefixes or suffixes is not supported; the method calls
60 class U_I18N_API CompactDecimalFormat
: public DecimalFormat
{
64 * Returns a compact decimal instance for specified locale.
66 * <strong>NOTE:</strong> New users are strongly encouraged to use
67 * {@link NumberFormatter} instead of NumberFormat.
68 * @param inLocale the given locale.
69 * @param style whether to use short or long style.
70 * @param status error code returned here.
73 static CompactDecimalFormat
* U_EXPORT2
createInstance(
74 const Locale
& inLocale
, UNumberCompactStyle style
, UErrorCode
& status
);
79 * @param source the DecimalFormat object to be copied from.
82 CompactDecimalFormat(const CompactDecimalFormat
& source
);
88 virtual ~CompactDecimalFormat();
91 * Assignment operator.
93 * @param rhs the DecimalFormat object to be copied.
96 CompactDecimalFormat
& operator=(const CompactDecimalFormat
& rhs
);
99 * Clone this Format object polymorphically. The caller owns the
100 * result and should delete it when done.
102 * @return a polymorphic copy of this CompactDecimalFormat.
105 virtual Format
* clone() const;
108 * Return TRUE if the given Format objects are semantically equal.
109 * Objects of different subclasses are considered unequal.
111 * @param other the object to be compared with.
112 * @return TRUE if the given Format objects are semantically equal.
115 virtual UBool
operator==(const Format
& other
) const;
118 using DecimalFormat::format
;
121 * Format a double or long number using base-10 representation.
123 * @param number The value to be formatted.
124 * @param appendTo Output parameter to receive result.
125 * Result is appended to existing contents.
126 * @param pos On input: an alignment field, if desired.
127 * On output: the offsets of the alignment field.
128 * @return Reference to 'appendTo' parameter.
131 virtual UnicodeString
& format(double number
,
132 UnicodeString
& appendTo
,
133 FieldPosition
& pos
) const;
136 * Format a double or long number using base-10 representation.
138 * @param number The value to be formatted.
139 * @param appendTo Output parameter to receive result.
140 * Result is appended to existing contents.
141 * @param pos On input: an alignment field, if desired.
142 * On output: the offsets of the alignment field.
144 * @return Reference to 'appendTo' parameter.
147 virtual UnicodeString
& format(double number
,
148 UnicodeString
& appendTo
,
150 UErrorCode
&status
) const;
153 * Format a double or long number using base-10 representation.
154 * Currently sets status to U_UNSUPPORTED_ERROR.
156 * @param number The value to be formatted.
157 * @param appendTo Output parameter to receive result.
158 * Result is appended to existing contents.
159 * @param posIter On return, can be used to iterate over positions
160 * of fields generated by this format call.
162 * @param status Output param filled with success/failure status.
163 * @return Reference to 'appendTo' parameter.
166 virtual UnicodeString
& format(double number
,
167 UnicodeString
& appendTo
,
168 FieldPositionIterator
* posIter
,
169 UErrorCode
& status
) const;
172 * Format a long number using base-10 representation.
174 * @param number The value to be formatted.
175 * @param appendTo Output parameter to receive result.
176 * Result is appended to existing contents.
177 * @param pos On input: an alignment field, if desired.
178 * On output: the offsets of the alignment field.
179 * @return Reference to 'appendTo' parameter.
182 virtual UnicodeString
& format(int32_t number
,
183 UnicodeString
& appendTo
,
184 FieldPosition
& pos
) const;
187 * Format a long number using base-10 representation.
189 * @param number The value to be formatted.
190 * @param appendTo Output parameter to receive result.
191 * Result is appended to existing contents.
192 * @param pos On input: an alignment field, if desired.
193 * On output: the offsets of the alignment field.
194 * @return Reference to 'appendTo' parameter.
197 virtual UnicodeString
& format(int32_t number
,
198 UnicodeString
& appendTo
,
200 UErrorCode
&status
) const;
203 * Format a long number using base-10 representation.
204 * Currently sets status to U_UNSUPPORTED_ERROR
206 * @param number The value to be formatted.
207 * @param appendTo Output parameter to receive result.
208 * Result is appended to existing contents.
209 * @param posIter On return, can be used to iterate over positions
210 * of fields generated by this format call.
212 * @param status Output param filled with success/failure status.
213 * @return Reference to 'appendTo' parameter.
216 virtual UnicodeString
& format(int32_t number
,
217 UnicodeString
& appendTo
,
218 FieldPositionIterator
* posIter
,
219 UErrorCode
& status
) const;
222 * Format an int64 number using base-10 representation.
224 * @param number The value to be formatted.
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 * @return Reference to 'appendTo' parameter.
232 virtual UnicodeString
& format(int64_t number
,
233 UnicodeString
& appendTo
,
234 FieldPosition
& pos
) const;
237 * Format an int64 number using base-10 representation.
239 * @param number The value to be formatted.
240 * @param appendTo Output parameter to receive result.
241 * Result is appended to existing contents.
242 * @param pos On input: an alignment field, if desired.
243 * On output: the offsets of the alignment field.
244 * @return Reference to 'appendTo' parameter.
247 virtual UnicodeString
& format(int64_t number
,
248 UnicodeString
& appendTo
,
250 UErrorCode
&status
) const;
253 * Format an int64 number using base-10 representation.
254 * Currently sets status to U_UNSUPPORTED_ERROR
256 * @param number The value to be formatted.
257 * @param appendTo Output parameter to receive result.
258 * Result is appended to existing contents.
259 * @param posIter On return, can be used to iterate over positions
260 * of fields generated by this format call.
262 * @param status Output param filled with success/failure status.
263 * @return Reference to 'appendTo' parameter.
266 virtual UnicodeString
& format(int64_t number
,
267 UnicodeString
& appendTo
,
268 FieldPositionIterator
* posIter
,
269 UErrorCode
& status
) const;
272 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
273 * The syntax of the unformatted number is a "numeric string"
274 * as defined in the Decimal Arithmetic Specification, available at
275 * http://speleotrove.com/decimal
277 * @param number The unformatted number, as a string.
278 * @param appendTo Output parameter to receive result.
279 * Result is appended to existing contents.
280 * @param posIter On return, can be used to iterate over positions
281 * of fields generated by this format call.
283 * @param status Output param filled with success/failure status.
284 * @return Reference to 'appendTo' parameter.
287 virtual UnicodeString
& format(StringPiece number
,
288 UnicodeString
& appendTo
,
289 FieldPositionIterator
* posIter
,
290 UErrorCode
& status
) const;
293 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR
294 * The number is a DigitList wrapper onto a floating point decimal number.
295 * The default implementation in NumberFormat converts the decimal number
296 * to a double and formats that.
298 * @param number The number, a DigitList format Decimal Floating Point.
299 * @param appendTo Output parameter to receive result.
300 * Result is appended to existing contents.
301 * @param posIter On return, can be used to iterate over positions
302 * of fields generated by this format call.
303 * @param status Output param filled with success/failure status.
304 * @return Reference to 'appendTo' parameter.
307 virtual UnicodeString
& format(const DigitList
&number
,
308 UnicodeString
& appendTo
,
309 FieldPositionIterator
* posIter
,
310 UErrorCode
& status
) const;
313 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR.
314 * The number is a DigitList wrapper onto a floating point decimal number.
315 * The default implementation in NumberFormat converts the decimal number
316 * to a double and formats that.
318 * @param number The number, a DigitList format Decimal Floating Point.
319 * @param appendTo Output parameter to receive result.
320 * Result is appended to existing contents.
321 * @param pos On input: an alignment field, if desired.
322 * On output: the offsets of the alignment field.
323 * @param status Output param filled with success/failure status.
324 * @return Reference to 'appendTo' parameter.
327 virtual UnicodeString
& format(const DigitList
&number
,
328 UnicodeString
& appendTo
,
330 UErrorCode
& status
) const;
333 * CompactDecimalFormat does not support parsing. This implementation
335 * @param text Unused.
336 * @param result Does not change.
337 * @param parsePosition Does not change.
341 virtual void parse(const UnicodeString
& text
,
343 ParsePosition
& parsePosition
) const;
346 * CompactDecimalFormat does not support parsing. This implementation
347 * sets status to U_UNSUPPORTED_ERROR
349 * @param text Unused.
350 * @param result Does not change.
351 * @param status Always set to U_UNSUPPORTED_ERROR.
354 virtual void parse(const UnicodeString
& text
,
356 UErrorCode
& status
) const;
359 * Parses text from the given string as a currency amount. Unlike
360 * the parse() method, this method will attempt to parse a generic
361 * currency name, searching for a match of this object's locale's
362 * currency display names, or for a 3-letter ISO currency code.
363 * This method will fail if this format is not a currency format,
364 * that is, if it does not contain the currency pattern symbol
365 * (U+00A4) in its prefix or suffix. This implementation always returns
368 * @param text the string to parse
369 * @param pos input-output position; on input, the position within text
370 * to match; must have 0 <= pos.getIndex() < text.length();
371 * on output, the position after the last matched character.
372 * If the parse fails, the position in unchanged upon output.
373 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
374 * object (owned by the caller) containing information about
375 * the parsed currency; if parse fails, this is NULL.
378 virtual CurrencyAmount
* parseCurrency(const UnicodeString
& text
,
379 ParsePosition
& pos
) const;
382 * Return the class ID for this class. This is useful only for
383 * comparing to a return value from getDynamicClassID(). For example:
385 * . Base* polymorphic_pointer = createPolymorphicObject();
386 * . if (polymorphic_pointer->getDynamicClassID() ==
387 * . Derived::getStaticClassID()) ...
389 * @return The class ID for all objects of this class.
392 static UClassID U_EXPORT2
getStaticClassID();
395 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
396 * This method is to implement a simple version of RTTI, since not all
397 * C++ compilers support genuine RTTI. Polymorphic operator==() and
398 * clone() methods call this method.
400 * @return The class ID for this object. All objects of a
401 * given class have the same class ID. Objects of
402 * other classes have different class IDs.
405 virtual UClassID
getDynamicClassID() const;
409 const UHashtable
* _unitsByVariant
;
410 const double* _divisors
;
411 PluralRules
* _pluralRules
;
413 // Default constructor not implemented.
414 CompactDecimalFormat(const DecimalFormat
&, const UHashtable
* unitsByVariant
, const double* divisors
, PluralRules
* pluralRules
);
416 UBool
eqHelper(const CompactDecimalFormat
& that
) const;
420 #endif // U_SHOW_CPLUSPLUS_API
422 #endif /* #if !UCONFIG_NO_FORMATTING */
424 #endif // __COMPACT_DECIMAL_FORMAT_H__