2 *******************************************************************************
3 * Copyright (C) 2015, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 #ifndef VALUEFORMATTER_H
9 #define VALUEFORMATTER_H
11 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/uobject.h"
14 #include "unicode/utypes.h"
22 class FieldPositionHandler
;
27 class DigitFormatterOptions
;
28 class ScientificPrecision
;
29 class SciFormatterOptions
;
31 class VisibleDigitsWithExponent
;
35 * A closure around rounding and formatting a value. As these instances are
36 * designed to be short lived (they only exist while formatting a value), they
37 * do not own their own attributes. Rather the caller maintains ownership of
38 * all attributes. A caller first calls a prepareXXX method on an instance
39 * to share its data before using that instance. Using an
40 * instance without first calling a prepareXXX method results in an
41 * assertion error and a program crash.
43 class U_I18N_API ValueFormatter
: public UObject
{
45 ValueFormatter() : fType(kFormatTypeCount
) {
48 virtual ~ValueFormatter();
51 * This function is here only to support the protected round() method
52 * in DecimalFormat. It serves no ther purpose than that.
54 * @param value this value is rounded in place.
55 * @param status any error returned here.
57 DigitList
&round(DigitList
&value
, UErrorCode
&status
) const;
60 * Returns TRUE if the absolute value of value can be fast formatted
61 * using ValueFormatter::formatInt32.
63 UBool
isFastFormattable(int32_t value
) const;
66 * Converts value to a VisibleDigitsWithExponent.
67 * Result may be fixed point or scientific.
69 VisibleDigitsWithExponent
&toVisibleDigitsWithExponent(
71 VisibleDigitsWithExponent
&digits
,
72 UErrorCode
&status
) const;
75 * Converts value to a VisibleDigitsWithExponent.
76 * Result may be fixed point or scientific.
78 VisibleDigitsWithExponent
&toVisibleDigitsWithExponent(
80 VisibleDigitsWithExponent
&digits
,
81 UErrorCode
&status
) const;
84 * formats positiveValue and appends to appendTo. Returns appendTo.
85 * @param positiveValue If negative, no negative sign is formatted.
86 * @param handler stores the field positions
87 * @param appendTo formatted value appended here.
89 UnicodeString
&format(
90 const VisibleDigitsWithExponent
&positiveValue
,
91 FieldPositionHandler
&handler
,
92 UnicodeString
&appendTo
) const;
96 * formats positiveValue and appends to appendTo. Returns appendTo.
97 * value must be positive. Calling formatInt32 to format a value when
98 * isFastFormattable indicates that the value cannot be fast formatted
99 * results in undefined behavior.
101 UnicodeString
&formatInt32(
102 int32_t positiveValue
,
103 FieldPositionHandler
&handler
,
104 UnicodeString
&appendTo
) const;
107 * Returns the number of code points needed to format.
108 * @param positiveValue if negative, the negative sign is not included
112 const VisibleDigitsWithExponent
&positiveValue
) const;
115 * Prepares this instance for fixed decimal formatting.
117 void prepareFixedDecimalFormatting(
118 const DigitFormatter
&formatter
,
119 const DigitGrouping
&grouping
,
120 const FixedPrecision
&precision
,
121 const DigitFormatterOptions
&options
);
124 * Prepares this instance for scientific formatting.
126 void prepareScientificFormatting(
127 const DigitFormatter
&formatter
,
128 const ScientificPrecision
&precision
,
129 const SciFormatterOptions
&options
);
132 ValueFormatter(const ValueFormatter
&);
133 ValueFormatter
&operator=(const ValueFormatter
&);
142 // for fixed decimal and scientific formatting
143 const DigitFormatter
*fDigitFormatter
;
145 // for fixed decimal formatting
146 const FixedPrecision
*fFixedPrecision
;
147 const DigitFormatterOptions
*fFixedOptions
;
148 const DigitGrouping
*fGrouping
;
150 // for scientific formatting
151 const ScientificPrecision
*fScientificPrecision
;
152 const SciFormatterOptions
*fScientificOptions
;
157 #endif /* !UCONFIG_NO_FORMATTING */
159 #endif /* VALUEFORMATTER_H */