2 *******************************************************************************
3 * Copyright (C) 2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 * created on: 2015jan06
9 * created by: Travis Keep
12 #ifndef __PRECISION_H__
13 #define __PRECISION_H__
15 #include "unicode/uobject.h"
17 #if !UCONFIG_NO_FORMATTING
18 #include "unicode/utypes.h"
20 #include "digitinterval.h"
22 #include "significantdigitinterval.h"
27 class VisibleDigitsWithExponent
;
31 * A precision manager for values to be formatted as fixed point.
32 * Handles rounding of number to prepare it for formatting.
34 class U_I18N_API FixedPrecision
: public UMemory
{
38 * The smallest format interval allowed. Default is 1 integer digit and no
44 * The largest format interval allowed. Must contain fMin.
45 * Default is all digits.
50 * Min and max significant digits allowed. The default is no constraints.
52 SignificantDigitInterval fSignificant
;
55 * The rounding increment or zero if there is no rounding increment.
58 DigitList fRoundingIncrement
;
61 * If set, causes round() to set status to U_FORMAT_INEXACT_ERROR if
62 * any rounding is done. Default is FALSE.
67 * If set, causes round() to set status to U_ILLEGAL_ARGUMENT_ERROR if
68 * rounded number has more than maximum integer digits. Default is FALSE.
73 * Controls the rounding mode that initVisibleDigits uses.
74 * Default is DecimalFormat::kRoundHalfEven
76 DecimalFormat::ERoundingMode fRoundingMode
;
81 * Returns TRUE if this object equals rhs.
83 UBool
equals(const FixedPrecision
&rhs
) const {
84 return (fMin
.equals(rhs
.fMin
) &&
85 fMax
.equals(rhs
.fMax
) &&
86 fSignificant
.equals(rhs
.fSignificant
) &&
87 (fRoundingIncrement
== rhs
.fRoundingIncrement
) &&
88 fExactOnly
== rhs
.fExactOnly
&&
89 fFailIfOverMax
== rhs
.fFailIfOverMax
&&
90 fRoundingMode
== rhs
.fRoundingMode
);
94 * Rounds value in place to prepare it for formatting.
95 * @param value The value to be rounded. It is rounded in place.
96 * @param exponent Always pass 0 for fixed decimal formatting. scientific
97 * precision passes the exponent value. Essentially, it divides value by
98 * 10^exponent, rounds and then multiplies by 10^exponent.
99 * @param status error returned here.
100 * @return reference to value.
102 DigitList
&round(DigitList
&value
, int32_t exponent
, UErrorCode
&status
) const;
105 * Returns the interval to use to format the rounded value.
106 * @param roundedValue the already rounded value to format.
107 * @param interval modified in place to be the interval to use to format
109 * @return a reference to interval.
111 DigitInterval
&getInterval(
112 const DigitList
&roundedValue
, DigitInterval
&interval
) const;
115 * Returns TRUE if this instance allows for fast formatting of integers.
117 UBool
isFastFormattable() const;
120 * Initializes a VisibleDigits.
121 * @param value value for VisibleDigits
122 * Caller must not assume that the value of this parameter will remain
124 * @param digits This is the value that is initialized.
125 * @param status any error returned here.
128 VisibleDigits
&initVisibleDigits(
130 VisibleDigits
&digits
,
131 UErrorCode
&status
) const;
134 * Initializes a VisibleDigits.
135 * @param value value for VisibleDigits
136 * @param digits This is the value that is initialized.
137 * @param status any error returned here.
140 VisibleDigits
&initVisibleDigits(
142 VisibleDigits
&digits
,
143 UErrorCode
&status
) const;
146 * Initializes a VisibleDigits.
147 * @param value value for VisibleDigits
148 * @param digits This is the value that is initialized.
149 * @param status any error returned here.
152 VisibleDigits
&initVisibleDigits(
154 VisibleDigits
&digits
,
155 UErrorCode
&status
) const;
158 * Initializes a VisibleDigitsWithExponent.
159 * @param value value for VisibleDigits
160 * Caller must not assume that the value of this parameter will remain
162 * @param digits This is the value that is initialized.
163 * @param status any error returned here.
166 VisibleDigitsWithExponent
&initVisibleDigitsWithExponent(
168 VisibleDigitsWithExponent
&digits
,
169 UErrorCode
&status
) const;
172 * Initializes a VisibleDigitsWithExponent.
173 * @param value value for VisibleDigits
174 * @param digits This is the value that is initialized.
175 * @param status any error returned here.
178 VisibleDigitsWithExponent
&initVisibleDigitsWithExponent(
180 VisibleDigitsWithExponent
&digits
,
181 UErrorCode
&status
) const;
184 * Initializes a VisibleDigitsWithExponent.
185 * @param value value for VisibleDigits
186 * @param digits This is the value that is initialized.
187 * @param status any error returned here.
190 VisibleDigitsWithExponent
&initVisibleDigitsWithExponent(
192 VisibleDigitsWithExponent
&digits
,
193 UErrorCode
&status
) const;
197 * Attempts to initialize 'digits' using simple mod 10 arithmetic.
198 * Returns FALSE if this is not possible such as when rounding
199 * would change the value. Otherwise returns TRUE.
201 * If the method returns FALSE, caller should create a DigitList
202 * and use it to initialize 'digits'. If this method returns TRUE,
203 * caller should accept the value stored in 'digits'. If this
204 * method returns TRUE along with a non zero error, caller must accept
205 * the error and not try again with a DigitList.
207 * Before calling this method, caller must verify that this object
208 * has no rounding increment set.
210 * The value that 'digits' is initialized to is mantissa * 10^exponent.
211 * For example mantissa = 54700 and exponent = -3 means 54.7. The
212 * properties of this object (such as min and max fraction digits),
213 * not the number of trailing zeros in the mantissa, determine whether or
214 * not the result contains any trailing 0's after the decimal point.
216 * @param mantissa the digits. May be positive or negative. May contain
218 * @param exponent must always be zero or negative. An exponent > 0
219 * yields undefined results!
220 * @param digits result stored here.
221 * @param status any error returned here.
227 VisibleDigits
&digits
,
228 UErrorCode
&status
) const;
229 UBool
isRoundingRequired(
230 int32_t upperExponent
, int32_t lowerExponent
) const;
231 DigitInterval
&getIntervalForZero(DigitInterval
&interval
) const;
232 DigitInterval
&getInterval(
233 int32_t upperExponent
, DigitInterval
&interval
) const;
234 static UBool
handleNonNumeric(DigitList
&value
, VisibleDigits
&digits
);
236 friend class ScientificPrecision
;
240 * A precision manager for values to be expressed as scientific notation.
242 class U_I18N_API ScientificPrecision
: public UMemory
{
244 FixedPrecision fMantissa
;
245 int32_t fMinExponentDigits
;
247 ScientificPrecision();
250 * rounds value in place to prepare it for formatting.
251 * @param value The value to be rounded. It is rounded in place.
252 * @param status error returned here.
253 * @return reference to value.
255 DigitList
&round(DigitList
&value
, UErrorCode
&status
) const;
258 * Converts value to a mantissa and exponent.
260 * @param value modified in place to be the mantissa. Depending on
261 * the precision settings, the resulting mantissa may not fall
262 * between 1.0 and 10.0.
263 * @return the exponent of value.
265 int32_t toScientific(DigitList
&value
) const;
268 * Returns TRUE if this object equals rhs.
270 UBool
equals(const ScientificPrecision
&rhs
) const {
271 return fMantissa
.equals(rhs
.fMantissa
) && fMinExponentDigits
== rhs
.fMinExponentDigits
;
275 * Initializes a VisibleDigitsWithExponent.
276 * @param value the value
277 * Caller must not assume that the value of this parameter will remain
279 * @param digits This is the value that is initialized.
280 * @param status any error returned here.
283 VisibleDigitsWithExponent
&initVisibleDigitsWithExponent(
285 VisibleDigitsWithExponent
&digits
,
286 UErrorCode
&status
) const;
289 * Initializes a VisibleDigitsWithExponent.
290 * @param value the value
291 * @param digits This is the value that is initialized.
292 * @param status any error returned here.
295 VisibleDigitsWithExponent
&initVisibleDigitsWithExponent(
297 VisibleDigitsWithExponent
&digits
,
298 UErrorCode
&status
) const;
301 * Initializes a VisibleDigitsWithExponent.
302 * @param value the value
303 * @param digits This is the value that is initialized.
304 * @param status any error returned here.
307 VisibleDigitsWithExponent
&initVisibleDigitsWithExponent(
309 VisibleDigitsWithExponent
&digits
,
310 UErrorCode
&status
) const;
313 int32_t getMultiplier() const;
320 #endif // #if !UCONFIG_NO_FORMATTING
321 #endif // __PRECISION_H__