1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2015, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
* precision.h
*
* created on: 2015jan06
* created by: Travis Keep
*/
#ifndef __PRECISION_H__
#define __PRECISION_H__
#include "unicode/uobject.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/utypes.h"
#include "digitinterval.h"
#include "digitlst.h"
#include "significantdigitinterval.h"
U_NAMESPACE_BEGIN
class VisibleDigits;
class VisibleDigitsWithExponent;
/**
* A precision manager for values to be formatted as fixed point.
* Handles rounding of number to prepare it for formatting.
*/
class U_I18N_API FixedPrecision : public UMemory {
public:
/**
* The smallest format interval allowed. Default is 1 integer digit and no
* fraction digits.
*/
DigitInterval fMin;
/**
* The largest format interval allowed. Must contain fMin.
* Default is all digits.
*/
DigitInterval fMax;
/**
* Min and max significant digits allowed. The default is no constraints.
*/
SignificantDigitInterval fSignificant;
/**
* The rounding increment or zero if there is no rounding increment.
* Default is zero.
*/
DigitList fRoundingIncrement;
/**
* If set, causes round() to set status to U_FORMAT_INEXACT_ERROR if
* any rounding is done. Default is FALSE.
*/
UBool fExactOnly;
/**
* If set, causes round() to set status to U_ILLEGAL_ARGUMENT_ERROR if
* rounded number has more than maximum integer digits. Default is FALSE.
*/
UBool fFailIfOverMax;
/**
* Controls the rounding mode that initVisibleDigits uses.
* Default is DecimalFormat::kRoundHalfEven
*/
DecimalFormat::ERoundingMode fRoundingMode;
FixedPrecision();
/**
* Returns TRUE if this object equals rhs.
*/
UBool equals(const FixedPrecision &rhs) const {
return (fMin.equals(rhs.fMin) &&
fMax.equals(rhs.fMax) &&
fSignificant.equals(rhs.fSignificant) &&
(fRoundingIncrement == rhs.fRoundingIncrement) &&
fExactOnly == rhs.fExactOnly &&
fFailIfOverMax == rhs.fFailIfOverMax &&
fRoundingMode == rhs.fRoundingMode);
}
/**
* Rounds value in place to prepare it for formatting.
* @param value The value to be rounded. It is rounded in place.
* @param exponent Always pass 0 for fixed decimal formatting. scientific
* precision passes the exponent value. Essentially, it divides value by
* 10^exponent, rounds and then multiplies by 10^exponent.
* @param status error returned here.
* @return reference to value.
*/
DigitList &round(DigitList &value, int32_t exponent, UErrorCode &status) const;
/**
* Returns the interval to use to format the rounded value.
* @param roundedValue the already rounded value to format.
* @param interval modified in place to be the interval to use to format
* the rounded value.
* @return a reference to interval.
*/
DigitInterval &getInterval(
const DigitList &roundedValue, DigitInterval &interval) const;
/**
* Returns TRUE if this instance allows for fast formatting of integers.
*/
UBool isFastFormattable() const;
/**
* Initializes a VisibleDigits.
* @param value value for VisibleDigits
* Caller must not assume that the value of this parameter will remain
* unchanged.
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigits &initVisibleDigits(
DigitList &value,
VisibleDigits &digits,
UErrorCode &status) const;
/**
* Initializes a VisibleDigits.
* @param value value for VisibleDigits
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigits &initVisibleDigits(
double value,
VisibleDigits &digits,
UErrorCode &status) const;
/**
* Initializes a VisibleDigits.
* @param value value for VisibleDigits
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigits &initVisibleDigits(
int64_t value,
VisibleDigits &digits,
UErrorCode &status) const;
/**
* Initializes a VisibleDigitsWithExponent.
* @param value value for VisibleDigits
* Caller must not assume that the value of this parameter will remain
* unchanged.
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigitsWithExponent &initVisibleDigitsWithExponent(
DigitList &value,
VisibleDigitsWithExponent &digits,
UErrorCode &status) const;
/**
* Initializes a VisibleDigitsWithExponent.
* @param value value for VisibleDigits
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigitsWithExponent &initVisibleDigitsWithExponent(
double value,
VisibleDigitsWithExponent &digits,
UErrorCode &status) const;
/**
* Initializes a VisibleDigitsWithExponent.
* @param value value for VisibleDigits
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigitsWithExponent &initVisibleDigitsWithExponent(
int64_t value,
VisibleDigitsWithExponent &digits,
UErrorCode &status) const;
private:
/**
* Attempts to initialize 'digits' using simple mod 10 arithmetic.
* Returns FALSE if this is not possible such as when rounding
* would change the value. Otherwise returns TRUE.
*
* If the method returns FALSE, caller should create a DigitList
* and use it to initialize 'digits'. If this method returns TRUE,
* caller should accept the value stored in 'digits'. If this
* method returns TRUE along with a non zero error, caller must accept
* the error and not try again with a DigitList.
*
* Before calling this method, caller must verify that this object
* has no rounding increment set.
*
* The value that 'digits' is initialized to is mantissa * 10^exponent.
* For example mantissa = 54700 and exponent = -3 means 54.7. The
* properties of this object (such as min and max fraction digits),
* not the number of trailing zeros in the mantissa, determine whether or
* not the result contains any trailing 0's after the decimal point.
*
* @param mantissa the digits. May be positive or negative. May contain
* trailing zeros.
* @param exponent must always be zero or negative. An exponent > 0
* yields undefined results!
* @param digits result stored here.
* @param status any error returned here.
*/
UBool
initVisibleDigits(
int64_t mantissa,
int32_t exponent,
VisibleDigits &digits,
UErrorCode &status) const;
UBool isRoundingRequired(
int32_t upperExponent, int32_t lowerExponent) const;
DigitInterval &getIntervalForZero(DigitInterval &interval) const;
DigitInterval &getInterval(
int32_t upperExponent, DigitInterval &interval) const;
static UBool handleNonNumeric(DigitList &value, VisibleDigits &digits);
friend class ScientificPrecision;
};
/**
* A precision manager for values to be expressed as scientific notation.
*/
class U_I18N_API ScientificPrecision : public UMemory {
public:
FixedPrecision fMantissa;
int32_t fMinExponentDigits;
ScientificPrecision();
/**
* rounds value in place to prepare it for formatting.
* @param value The value to be rounded. It is rounded in place.
* @param status error returned here.
* @return reference to value.
*/
DigitList &round(DigitList &value, UErrorCode &status) const;
/**
* Converts value to a mantissa and exponent.
*
* @param value modified in place to be the mantissa. Depending on
* the precision settings, the resulting mantissa may not fall
* between 1.0 and 10.0.
* @return the exponent of value.
*/
int32_t toScientific(DigitList &value) const;
/**
* Returns TRUE if this object equals rhs.
*/
UBool equals(const ScientificPrecision &rhs) const {
return fMantissa.equals(rhs.fMantissa) && fMinExponentDigits == rhs.fMinExponentDigits;
}
/**
* Initializes a VisibleDigitsWithExponent.
* @param value the value
* Caller must not assume that the value of this parameter will remain
* unchanged.
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigitsWithExponent &initVisibleDigitsWithExponent(
DigitList &value,
VisibleDigitsWithExponent &digits,
UErrorCode &status) const;
/**
* Initializes a VisibleDigitsWithExponent.
* @param value the value
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigitsWithExponent &initVisibleDigitsWithExponent(
double value,
VisibleDigitsWithExponent &digits,
UErrorCode &status) const;
/**
* Initializes a VisibleDigitsWithExponent.
* @param value the value
* @param digits This is the value that is initialized.
* @param status any error returned here.
* @return digits
*/
VisibleDigitsWithExponent &initVisibleDigitsWithExponent(
int64_t value,
VisibleDigitsWithExponent &digits,
UErrorCode &status) const;
private:
int32_t getMultiplier() const;
};
U_NAMESPACE_END
#endif // #if !UCONFIG_NO_FORMATTING
#endif // __PRECISION_H__
|