1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_FORMATTING
7
8 #include "charstr.h"
9 #include "uassert.h"
10 #include "unicode/numberformatter.h"
11 #include "number_types.h"
12 #include "number_decimalquantity.h"
13 #include "double-conversion.h"
14 #include "number_roundingutils.h"
15 #include "number_skeletons.h"
16 #include "putilimp.h"
17 #include "string_segment.h"
18
19 using namespace icu;
20 using namespace icu::number;
21 using namespace icu::number::impl;
22
23
24 using double_conversion::DoubleToStringConverter;
25 using icu::StringSegment;
26
parseIncrementOption(const StringSegment & segment,Precision & outPrecision,UErrorCode & status)27 void number::impl::parseIncrementOption(const StringSegment &segment,
28 Precision &outPrecision,
29 UErrorCode &status) {
30 // Need to do char <-> UChar conversion...
31 U_ASSERT(U_SUCCESS(status));
32 CharString buffer;
33 SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
34
35 // Utilize DecimalQuantity/decNumber to parse this for us.
36 DecimalQuantity dq;
37 UErrorCode localStatus = U_ZERO_ERROR;
38 dq.setToDecNumber({buffer.data(), buffer.length()}, localStatus);
39 if (U_FAILURE(localStatus)) {
40 // throw new SkeletonSyntaxException("Invalid rounding increment", segment, e);
41 status = U_NUMBER_SKELETON_SYNTAX_ERROR;
42 return;
43 }
44 double increment = dq.toDouble();
45
46 // We also need to figure out how many digits. Do a brute force string operation.
47 int decimalOffset = 0;
48 while (decimalOffset < segment.length() && segment.charAt(decimalOffset) != '.') {
49 decimalOffset++;
50 }
51 if (decimalOffset == segment.length()) {
52 outPrecision = Precision::increment(increment);
53 } else {
54 int32_t fractionLength = segment.length() - decimalOffset - 1;
55 outPrecision = Precision::increment(increment).withMinFraction(fractionLength);
56 }
57 }
58
59 namespace {
60
getRoundingMagnitudeFraction(int maxFrac)61 int32_t getRoundingMagnitudeFraction(int maxFrac) {
62 if (maxFrac == -1) {
63 return INT32_MIN;
64 }
65 return -maxFrac;
66 }
67
getRoundingMagnitudeSignificant(const DecimalQuantity & value,int maxSig)68 int32_t getRoundingMagnitudeSignificant(const DecimalQuantity &value, int maxSig) {
69 if (maxSig == -1) {
70 return INT32_MIN;
71 }
72 int magnitude = value.isZeroish() ? 0 : value.getMagnitude();
73 return magnitude - maxSig + 1;
74 }
75
getDisplayMagnitudeFraction(int minFrac)76 int32_t getDisplayMagnitudeFraction(int minFrac) {
77 if (minFrac == 0) {
78 return INT32_MAX;
79 }
80 return -minFrac;
81 }
82
getDisplayMagnitudeSignificant(const DecimalQuantity & value,int minSig)83 int32_t getDisplayMagnitudeSignificant(const DecimalQuantity &value, int minSig) {
84 int magnitude = value.isZeroish() ? 0 : value.getMagnitude();
85 return magnitude - minSig + 1;
86 }
87
88 }
89
90
91 MultiplierProducer::~MultiplierProducer() = default;
92
93
doubleFractionLength(double input,int8_t * singleDigit)94 digits_t roundingutils::doubleFractionLength(double input, int8_t* singleDigit) {
95 char buffer[DoubleToStringConverter::kBase10MaximalLength + 1];
96 bool sign; // unused; always positive
97 int32_t length;
98 int32_t point;
99 DoubleToStringConverter::DoubleToAscii(
100 input,
101 DoubleToStringConverter::DtoaMode::SHORTEST,
102 0,
103 buffer,
104 sizeof(buffer),
105 &sign,
106 &length,
107 &point
108 );
109
110 if (singleDigit == nullptr) {
111 // no-op
112 } else if (length == 1) {
113 *singleDigit = buffer[0] - '0';
114 } else {
115 *singleDigit = -1;
116 }
117
118 return static_cast<digits_t>(length - point);
119 }
120
121
unlimited()122 Precision Precision::unlimited() {
123 return Precision(RND_NONE, {});
124 }
125
integer()126 FractionPrecision Precision::integer() {
127 return constructFraction(0, 0);
128 }
129
fixedFraction(int32_t minMaxFractionPlaces)130 FractionPrecision Precision::fixedFraction(int32_t minMaxFractionPlaces) {
131 if (minMaxFractionPlaces >= 0 && minMaxFractionPlaces <= kMaxIntFracSig) {
132 return constructFraction(minMaxFractionPlaces, minMaxFractionPlaces);
133 } else {
134 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
135 }
136 }
137
minFraction(int32_t minFractionPlaces)138 FractionPrecision Precision::minFraction(int32_t minFractionPlaces) {
139 if (minFractionPlaces >= 0 && minFractionPlaces <= kMaxIntFracSig) {
140 return constructFraction(minFractionPlaces, -1);
141 } else {
142 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
143 }
144 }
145
maxFraction(int32_t maxFractionPlaces)146 FractionPrecision Precision::maxFraction(int32_t maxFractionPlaces) {
147 if (maxFractionPlaces >= 0 && maxFractionPlaces <= kMaxIntFracSig) {
148 return constructFraction(0, maxFractionPlaces);
149 } else {
150 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
151 }
152 }
153
minMaxFraction(int32_t minFractionPlaces,int32_t maxFractionPlaces)154 FractionPrecision Precision::minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces) {
155 if (minFractionPlaces >= 0 && maxFractionPlaces <= kMaxIntFracSig &&
156 minFractionPlaces <= maxFractionPlaces) {
157 return constructFraction(minFractionPlaces, maxFractionPlaces);
158 } else {
159 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
160 }
161 }
162
fixedSignificantDigits(int32_t minMaxSignificantDigits)163 Precision Precision::fixedSignificantDigits(int32_t minMaxSignificantDigits) {
164 if (minMaxSignificantDigits >= 1 && minMaxSignificantDigits <= kMaxIntFracSig) {
165 return constructSignificant(minMaxSignificantDigits, minMaxSignificantDigits);
166 } else {
167 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
168 }
169 }
170
minSignificantDigits(int32_t minSignificantDigits)171 Precision Precision::minSignificantDigits(int32_t minSignificantDigits) {
172 if (minSignificantDigits >= 1 && minSignificantDigits <= kMaxIntFracSig) {
173 return constructSignificant(minSignificantDigits, -1);
174 } else {
175 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
176 }
177 }
178
maxSignificantDigits(int32_t maxSignificantDigits)179 Precision Precision::maxSignificantDigits(int32_t maxSignificantDigits) {
180 if (maxSignificantDigits >= 1 && maxSignificantDigits <= kMaxIntFracSig) {
181 return constructSignificant(1, maxSignificantDigits);
182 } else {
183 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
184 }
185 }
186
minMaxSignificantDigits(int32_t minSignificantDigits,int32_t maxSignificantDigits)187 Precision Precision::minMaxSignificantDigits(int32_t minSignificantDigits, int32_t maxSignificantDigits) {
188 if (minSignificantDigits >= 1 && maxSignificantDigits <= kMaxIntFracSig &&
189 minSignificantDigits <= maxSignificantDigits) {
190 return constructSignificant(minSignificantDigits, maxSignificantDigits);
191 } else {
192 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
193 }
194 }
195
trailingZeroDisplay(UNumberTrailingZeroDisplay trailingZeroDisplay) const196 Precision Precision::trailingZeroDisplay(UNumberTrailingZeroDisplay trailingZeroDisplay) const {
197 Precision result(*this); // copy constructor
198 result.fTrailingZeroDisplay = trailingZeroDisplay;
199 return result;
200 }
201
increment(double roundingIncrement)202 IncrementPrecision Precision::increment(double roundingIncrement) {
203 if (roundingIncrement > 0.0) {
204 return constructIncrement(roundingIncrement, 0);
205 } else {
206 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
207 }
208 }
209
currency(UCurrencyUsage currencyUsage)210 CurrencyPrecision Precision::currency(UCurrencyUsage currencyUsage) {
211 return constructCurrency(currencyUsage);
212 }
213
withSignificantDigits(int32_t minSignificantDigits,int32_t maxSignificantDigits,UNumberRoundingPriority priority) const214 Precision FractionPrecision::withSignificantDigits(
215 int32_t minSignificantDigits,
216 int32_t maxSignificantDigits,
217 UNumberRoundingPriority priority) const {
218 if (fType == RND_ERROR) { return *this; } // no-op in error state
219 if (minSignificantDigits >= 1 &&
220 maxSignificantDigits >= minSignificantDigits &&
221 maxSignificantDigits <= kMaxIntFracSig) {
222 return constructFractionSignificant(
223 *this,
224 minSignificantDigits,
225 maxSignificantDigits,
226 priority);
227 } else {
228 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
229 }
230 }
231
withMinDigits(int32_t minSignificantDigits) const232 Precision FractionPrecision::withMinDigits(int32_t minSignificantDigits) const {
233 if (fType == RND_ERROR) { return *this; } // no-op in error state
234 if (minSignificantDigits >= 1 && minSignificantDigits <= kMaxIntFracSig) {
235 return constructFractionSignificant(
236 *this,
237 1,
238 minSignificantDigits,
239 UNUM_ROUNDING_PRIORITY_RELAXED);
240 } else {
241 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
242 }
243 }
244
withMaxDigits(int32_t maxSignificantDigits) const245 Precision FractionPrecision::withMaxDigits(int32_t maxSignificantDigits) const {
246 if (fType == RND_ERROR) { return *this; } // no-op in error state
247 if (maxSignificantDigits >= 1 && maxSignificantDigits <= kMaxIntFracSig) {
248 return constructFractionSignificant(*this,
249 1,
250 maxSignificantDigits,
251 UNUM_ROUNDING_PRIORITY_STRICT);
252 } else {
253 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
254 }
255 }
256
257 // Private method on base class
withCurrency(const CurrencyUnit & currency,UErrorCode & status) const258 Precision Precision::withCurrency(const CurrencyUnit ¤cy, UErrorCode &status) const {
259 if (fType == RND_ERROR) { return *this; } // no-op in error state
260 U_ASSERT(fType == RND_CURRENCY);
261 const char16_t *isoCode = currency.getISOCurrency();
262 double increment = ucurr_getRoundingIncrementForUsage(isoCode, fUnion.currencyUsage, &status);
263 int32_t minMaxFrac = ucurr_getDefaultFractionDigitsForUsage(
264 isoCode, fUnion.currencyUsage, &status);
265 Precision retval = (increment != 0.0)
266 ? static_cast<Precision>(constructIncrement(increment, minMaxFrac))
267 : static_cast<Precision>(constructFraction(minMaxFrac, minMaxFrac));
268 retval.fTrailingZeroDisplay = fTrailingZeroDisplay;
269 return retval;
270 }
271
272 // Public method on CurrencyPrecision subclass
withCurrency(const CurrencyUnit & currency) const273 Precision CurrencyPrecision::withCurrency(const CurrencyUnit ¤cy) const {
274 UErrorCode localStatus = U_ZERO_ERROR;
275 Precision result = Precision::withCurrency(currency, localStatus);
276 if (U_FAILURE(localStatus)) {
277 return {localStatus};
278 }
279 return result;
280 }
281
withMinFraction(int32_t minFrac) const282 Precision IncrementPrecision::withMinFraction(int32_t minFrac) const {
283 if (fType == RND_ERROR) { return *this; } // no-op in error state
284 if (minFrac >= 0 && minFrac <= kMaxIntFracSig) {
285 return constructIncrement(fUnion.increment.fIncrement, minFrac);
286 } else {
287 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
288 }
289 }
290
constructFraction(int32_t minFrac,int32_t maxFrac)291 FractionPrecision Precision::constructFraction(int32_t minFrac, int32_t maxFrac) {
292 FractionSignificantSettings settings;
293 settings.fMinFrac = static_cast<digits_t>(minFrac);
294 settings.fMaxFrac = static_cast<digits_t>(maxFrac);
295 settings.fMinSig = -1;
296 settings.fMaxSig = -1;
297 PrecisionUnion union_;
298 union_.fracSig = settings;
299 return {RND_FRACTION, union_};
300 }
301
constructSignificant(int32_t minSig,int32_t maxSig)302 Precision Precision::constructSignificant(int32_t minSig, int32_t maxSig) {
303 FractionSignificantSettings settings;
304 settings.fMinFrac = -1;
305 settings.fMaxFrac = -1;
306 settings.fMinSig = static_cast<digits_t>(minSig);
307 settings.fMaxSig = static_cast<digits_t>(maxSig);
308 PrecisionUnion union_;
309 union_.fracSig = settings;
310 return {RND_SIGNIFICANT, union_};
311 }
312
313 Precision
constructFractionSignificant(const FractionPrecision & base,int32_t minSig,int32_t maxSig,UNumberRoundingPriority priority)314 Precision::constructFractionSignificant(
315 const FractionPrecision &base,
316 int32_t minSig,
317 int32_t maxSig,
318 UNumberRoundingPriority priority) {
319 FractionSignificantSettings settings = base.fUnion.fracSig;
320 settings.fMinSig = static_cast<digits_t>(minSig);
321 settings.fMaxSig = static_cast<digits_t>(maxSig);
322 settings.fPriority = priority;
323 PrecisionUnion union_;
324 union_.fracSig = settings;
325 return {RND_FRACTION_SIGNIFICANT, union_};
326 }
327
constructIncrement(double increment,int32_t minFrac)328 IncrementPrecision Precision::constructIncrement(double increment, int32_t minFrac) {
329 IncrementSettings settings;
330 // Note: For number formatting, fIncrement is used for RND_INCREMENT but not
331 // RND_INCREMENT_ONE or RND_INCREMENT_FIVE. However, fIncrement is used in all
332 // three when constructing a skeleton.
333 settings.fIncrement = increment;
334 settings.fMinFrac = static_cast<digits_t>(minFrac);
335 // One of the few pre-computed quantities:
336 // Note: it is possible for minFrac to be more than maxFrac... (misleading)
337 int8_t singleDigit;
338 settings.fMaxFrac = roundingutils::doubleFractionLength(increment, &singleDigit);
339 PrecisionUnion union_;
340 union_.increment = settings;
341 if (singleDigit == 1) {
342 // NOTE: In C++, we must return the correct value type with the correct union.
343 // It would be invalid to return a RND_FRACTION here because the methods on the
344 // IncrementPrecision type assume that the union is backed by increment data.
345 return {RND_INCREMENT_ONE, union_};
346 } else if (singleDigit == 5) {
347 return {RND_INCREMENT_FIVE, union_};
348 } else {
349 return {RND_INCREMENT, union_};
350 }
351 }
352
constructCurrency(UCurrencyUsage usage)353 CurrencyPrecision Precision::constructCurrency(UCurrencyUsage usage) {
354 PrecisionUnion union_;
355 union_.currencyUsage = usage;
356 return {RND_CURRENCY, union_};
357 }
358
359
RoundingImpl(const Precision & precision,UNumberFormatRoundingMode roundingMode,const CurrencyUnit & currency,UErrorCode & status)360 RoundingImpl::RoundingImpl(const Precision& precision, UNumberFormatRoundingMode roundingMode,
361 const CurrencyUnit& currency, UErrorCode& status)
362 : fPrecision(precision), fRoundingMode(roundingMode), fPassThrough(false) {
363 if (precision.fType == Precision::RND_CURRENCY) {
364 fPrecision = precision.withCurrency(currency, status);
365 }
366 }
367
passThrough()368 RoundingImpl RoundingImpl::passThrough() {
369 return {};
370 }
371
isSignificantDigits() const372 bool RoundingImpl::isSignificantDigits() const {
373 return fPrecision.fType == Precision::RND_SIGNIFICANT;
374 }
375
376 int32_t
chooseMultiplierAndApply(impl::DecimalQuantity & input,const impl::MultiplierProducer & producer,UErrorCode & status)377 RoundingImpl::chooseMultiplierAndApply(impl::DecimalQuantity &input, const impl::MultiplierProducer &producer,
378 UErrorCode &status) {
379 // Do not call this method with zero, NaN, or infinity.
380 U_ASSERT(!input.isZeroish());
381
382 // Perform the first attempt at rounding.
383 int magnitude = input.getMagnitude();
384 int multiplier = producer.getMultiplier(magnitude);
385 input.adjustMagnitude(multiplier);
386 apply(input, status);
387
388 // If the number rounded to zero, exit.
389 if (input.isZeroish() || U_FAILURE(status)) {
390 return multiplier;
391 }
392
393 // If the new magnitude after rounding is the same as it was before rounding, then we are done.
394 // This case applies to most numbers.
395 if (input.getMagnitude() == magnitude + multiplier) {
396 return multiplier;
397 }
398
399 // If the above case DIDN'T apply, then we have a case like 99.9 -> 100 or 999.9 -> 1000:
400 // The number rounded up to the next magnitude. Check if the multiplier changes; if it doesn't,
401 // we do not need to make any more adjustments.
402 int _multiplier = producer.getMultiplier(magnitude + 1);
403 if (multiplier == _multiplier) {
404 return multiplier;
405 }
406
407 // We have a case like 999.9 -> 1000, where the correct output is "1K", not "1000".
408 // Fix the magnitude and re-apply the rounding strategy.
409 input.adjustMagnitude(_multiplier - multiplier);
410 apply(input, status);
411 return _multiplier;
412 }
413
414 /** This is the method that contains the actual rounding logic. */
apply(impl::DecimalQuantity & value,UErrorCode & status) const415 void RoundingImpl::apply(impl::DecimalQuantity &value, UErrorCode& status) const {
416 if (U_FAILURE(status)) {
417 return;
418 }
419 if (fPassThrough) {
420 return;
421 }
422 int32_t resolvedMinFraction = 0;
423 switch (fPrecision.fType) {
424 case Precision::RND_BOGUS:
425 case Precision::RND_ERROR:
426 // Errors should be caught before the apply() method is called
427 status = U_INTERNAL_PROGRAM_ERROR;
428 break;
429
430 case Precision::RND_NONE:
431 value.roundToInfinity();
432 break;
433
434 case Precision::RND_FRACTION:
435 value.roundToMagnitude(
436 getRoundingMagnitudeFraction(fPrecision.fUnion.fracSig.fMaxFrac),
437 fRoundingMode,
438 status);
439 resolvedMinFraction =
440 uprv_max(0, -getDisplayMagnitudeFraction(fPrecision.fUnion.fracSig.fMinFrac));
441 break;
442
443 case Precision::RND_SIGNIFICANT:
444 value.roundToMagnitude(
445 getRoundingMagnitudeSignificant(value, fPrecision.fUnion.fracSig.fMaxSig),
446 fRoundingMode,
447 status);
448 resolvedMinFraction =
449 uprv_max(0, -getDisplayMagnitudeSignificant(value, fPrecision.fUnion.fracSig.fMinSig));
450 // Make sure that digits are displayed on zero.
451 if (value.isZeroish() && fPrecision.fUnion.fracSig.fMinSig > 0) {
452 value.setMinInteger(1);
453 }
454 break;
455
456 case Precision::RND_FRACTION_SIGNIFICANT: {
457 int32_t roundingMag1 = getRoundingMagnitudeFraction(fPrecision.fUnion.fracSig.fMaxFrac);
458 int32_t roundingMag2 = getRoundingMagnitudeSignificant(value, fPrecision.fUnion.fracSig.fMaxSig);
459 int32_t roundingMag;
460 if (fPrecision.fUnion.fracSig.fPriority == UNUM_ROUNDING_PRIORITY_RELAXED) {
461 roundingMag = uprv_min(roundingMag1, roundingMag2);
462 } else {
463 roundingMag = uprv_max(roundingMag1, roundingMag2);
464 }
465 value.roundToMagnitude(roundingMag, fRoundingMode, status);
466
467 int32_t displayMag1 = getDisplayMagnitudeFraction(fPrecision.fUnion.fracSig.fMinFrac);
468 int32_t displayMag2 = getDisplayMagnitudeSignificant(value, fPrecision.fUnion.fracSig.fMinSig);
469 int32_t displayMag = uprv_min(displayMag1, displayMag2);
470 resolvedMinFraction = uprv_max(0, -displayMag);
471
472 break;
473 }
474
475 case Precision::RND_INCREMENT:
476 value.roundToIncrement(
477 fPrecision.fUnion.increment.fIncrement,
478 fRoundingMode,
479 status);
480 resolvedMinFraction = fPrecision.fUnion.increment.fMinFrac;
481 break;
482
483 case Precision::RND_INCREMENT_ONE:
484 value.roundToMagnitude(
485 -fPrecision.fUnion.increment.fMaxFrac,
486 fRoundingMode,
487 status);
488 resolvedMinFraction = fPrecision.fUnion.increment.fMinFrac;
489 break;
490
491 case Precision::RND_INCREMENT_FIVE:
492 value.roundToNickel(
493 -fPrecision.fUnion.increment.fMaxFrac,
494 fRoundingMode,
495 status);
496 resolvedMinFraction = fPrecision.fUnion.increment.fMinFrac;
497 break;
498
499 case Precision::RND_CURRENCY:
500 // Call .withCurrency() before .apply()!
501 UPRV_UNREACHABLE;
502
503 default:
504 UPRV_UNREACHABLE;
505 }
506
507 if (fPrecision.fTrailingZeroDisplay == UNUM_TRAILING_ZERO_AUTO ||
508 // PLURAL_OPERAND_T returns fraction digits as an integer
509 value.getPluralOperand(PLURAL_OPERAND_T) != 0) {
510 value.setMinFraction(resolvedMinFraction);
511 }
512 }
513
apply(impl::DecimalQuantity & value,int32_t minInt,UErrorCode)514 void RoundingImpl::apply(impl::DecimalQuantity &value, int32_t minInt, UErrorCode /*status*/) {
515 // This method is intended for the one specific purpose of helping print "00.000E0".
516 // Question: Is it useful to look at trailingZeroDisplay here?
517 U_ASSERT(isSignificantDigits());
518 U_ASSERT(value.isZeroish());
519 value.setMinFraction(fPrecision.fUnion.fracSig.fMinSig - minInt);
520 }
521
522 #endif /* #if !UCONFIG_NO_FORMATTING */
523