2017-09-27 00:25:20 +00:00
|
|
|
// © 2017 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
2017-10-04 22:51:06 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2018-04-23 23:02:26 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2017-09-27 05:31:57 +00:00
|
|
|
|
|
|
|
#include "uassert.h"
|
2017-09-27 00:25:20 +00:00
|
|
|
#include "unicode/numberformatter.h"
|
|
|
|
#include "number_types.h"
|
|
|
|
#include "number_decimalquantity.h"
|
2018-03-17 07:23:08 +00:00
|
|
|
#include "double-conversion.h"
|
|
|
|
#include "number_roundingutils.h"
|
2018-04-05 21:54:04 +00:00
|
|
|
#include "putilimp.h"
|
2017-09-27 00:25:20 +00:00
|
|
|
|
2017-10-25 00:25:04 +00:00
|
|
|
using namespace icu;
|
2017-09-27 00:25:20 +00:00
|
|
|
using namespace icu::number;
|
|
|
|
using namespace icu::number::impl;
|
|
|
|
|
2018-03-17 07:23:08 +00:00
|
|
|
|
|
|
|
using double_conversion::DoubleToStringConverter;
|
|
|
|
|
2017-09-27 00:25:20 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
int32_t getRoundingMagnitudeFraction(int maxFrac) {
|
|
|
|
if (maxFrac == -1) {
|
|
|
|
return INT32_MIN;
|
|
|
|
}
|
|
|
|
return -maxFrac;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t getRoundingMagnitudeSignificant(const DecimalQuantity &value, int maxSig) {
|
|
|
|
if (maxSig == -1) {
|
|
|
|
return INT32_MIN;
|
|
|
|
}
|
2019-07-23 04:41:43 +00:00
|
|
|
int magnitude = value.isZeroish() ? 0 : value.getMagnitude();
|
2017-09-27 00:25:20 +00:00
|
|
|
return magnitude - maxSig + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t getDisplayMagnitudeFraction(int minFrac) {
|
|
|
|
if (minFrac == 0) {
|
|
|
|
return INT32_MAX;
|
|
|
|
}
|
|
|
|
return -minFrac;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t getDisplayMagnitudeSignificant(const DecimalQuantity &value, int minSig) {
|
2019-07-23 04:41:43 +00:00
|
|
|
int magnitude = value.isZeroish() ? 0 : value.getMagnitude();
|
2017-09-27 00:25:20 +00:00
|
|
|
return magnitude - minSig + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-03 09:13:46 +00:00
|
|
|
MultiplierProducer::~MultiplierProducer() = default;
|
|
|
|
|
|
|
|
|
2018-11-03 05:19:53 +00:00
|
|
|
digits_t roundingutils::doubleFractionLength(double input, int8_t* singleDigit) {
|
2018-03-17 07:23:08 +00:00
|
|
|
char buffer[DoubleToStringConverter::kBase10MaximalLength + 1];
|
|
|
|
bool sign; // unused; always positive
|
|
|
|
int32_t length;
|
|
|
|
int32_t point;
|
|
|
|
DoubleToStringConverter::DoubleToAscii(
|
|
|
|
input,
|
|
|
|
DoubleToStringConverter::DtoaMode::SHORTEST,
|
|
|
|
0,
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
&sign,
|
|
|
|
&length,
|
|
|
|
&point
|
|
|
|
);
|
|
|
|
|
2018-11-03 05:19:53 +00:00
|
|
|
if (singleDigit == nullptr) {
|
|
|
|
// no-op
|
|
|
|
} else if (length == 1) {
|
|
|
|
*singleDigit = buffer[0] - '0';
|
|
|
|
} else {
|
|
|
|
*singleDigit = -1;
|
|
|
|
}
|
|
|
|
|
2018-03-17 07:23:08 +00:00
|
|
|
return static_cast<digits_t>(length - point);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision Precision::unlimited() {
|
|
|
|
return Precision(RND_NONE, {}, kDefaultMode);
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
FractionPrecision Precision::integer() {
|
2017-09-27 00:25:20 +00:00
|
|
|
return constructFraction(0, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
FractionPrecision Precision::fixedFraction(int32_t minMaxFractionPlaces) {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (minMaxFractionPlaces >= 0 && minMaxFractionPlaces <= kMaxIntFracSig) {
|
|
|
|
return constructFraction(minMaxFractionPlaces, minMaxFractionPlaces);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
FractionPrecision Precision::minFraction(int32_t minFractionPlaces) {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (minFractionPlaces >= 0 && minFractionPlaces <= kMaxIntFracSig) {
|
|
|
|
return constructFraction(minFractionPlaces, -1);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
FractionPrecision Precision::maxFraction(int32_t maxFractionPlaces) {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (maxFractionPlaces >= 0 && maxFractionPlaces <= kMaxIntFracSig) {
|
|
|
|
return constructFraction(0, maxFractionPlaces);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
FractionPrecision Precision::minMaxFraction(int32_t minFractionPlaces, int32_t maxFractionPlaces) {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (minFractionPlaces >= 0 && maxFractionPlaces <= kMaxIntFracSig &&
|
|
|
|
minFractionPlaces <= maxFractionPlaces) {
|
|
|
|
return constructFraction(minFractionPlaces, maxFractionPlaces);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision Precision::fixedSignificantDigits(int32_t minMaxSignificantDigits) {
|
2018-02-08 06:06:08 +00:00
|
|
|
if (minMaxSignificantDigits >= 1 && minMaxSignificantDigits <= kMaxIntFracSig) {
|
2017-09-27 00:25:20 +00:00
|
|
|
return constructSignificant(minMaxSignificantDigits, minMaxSignificantDigits);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision Precision::minSignificantDigits(int32_t minSignificantDigits) {
|
2018-02-08 06:06:08 +00:00
|
|
|
if (minSignificantDigits >= 1 && minSignificantDigits <= kMaxIntFracSig) {
|
2017-09-27 00:25:20 +00:00
|
|
|
return constructSignificant(minSignificantDigits, -1);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision Precision::maxSignificantDigits(int32_t maxSignificantDigits) {
|
2018-02-08 06:06:08 +00:00
|
|
|
if (maxSignificantDigits >= 1 && maxSignificantDigits <= kMaxIntFracSig) {
|
|
|
|
return constructSignificant(1, maxSignificantDigits);
|
2017-09-27 00:25:20 +00:00
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision Precision::minMaxSignificantDigits(int32_t minSignificantDigits, int32_t maxSignificantDigits) {
|
2018-02-08 06:06:08 +00:00
|
|
|
if (minSignificantDigits >= 1 && maxSignificantDigits <= kMaxIntFracSig &&
|
2017-09-27 00:25:20 +00:00
|
|
|
minSignificantDigits <= maxSignificantDigits) {
|
|
|
|
return constructSignificant(minSignificantDigits, maxSignificantDigits);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
IncrementPrecision Precision::increment(double roundingIncrement) {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (roundingIncrement > 0.0) {
|
|
|
|
return constructIncrement(roundingIncrement, 0);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
CurrencyPrecision Precision::currency(UCurrencyUsage currencyUsage) {
|
2017-09-27 00:25:20 +00:00
|
|
|
return constructCurrency(currencyUsage);
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision FractionPrecision::withMinDigits(int32_t minSignificantDigits) const {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (fType == RND_ERROR) { return *this; } // no-op in error state
|
2018-02-08 06:06:08 +00:00
|
|
|
if (minSignificantDigits >= 1 && minSignificantDigits <= kMaxIntFracSig) {
|
2017-09-27 00:25:20 +00:00
|
|
|
return constructFractionSignificant(*this, minSignificantDigits, -1);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision FractionPrecision::withMaxDigits(int32_t maxSignificantDigits) const {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (fType == RND_ERROR) { return *this; } // no-op in error state
|
2018-02-08 06:06:08 +00:00
|
|
|
if (maxSignificantDigits >= 1 && maxSignificantDigits <= kMaxIntFracSig) {
|
2017-09-27 00:25:20 +00:00
|
|
|
return constructFractionSignificant(*this, -1, maxSignificantDigits);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private method on base class
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision Precision::withCurrency(const CurrencyUnit ¤cy, UErrorCode &status) const {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (fType == RND_ERROR) { return *this; } // no-op in error state
|
|
|
|
U_ASSERT(fType == RND_CURRENCY);
|
|
|
|
const char16_t *isoCode = currency.getISOCurrency();
|
|
|
|
double increment = ucurr_getRoundingIncrementForUsage(isoCode, fUnion.currencyUsage, &status);
|
|
|
|
int32_t minMaxFrac = ucurr_getDefaultFractionDigitsForUsage(
|
|
|
|
isoCode, fUnion.currencyUsage, &status);
|
|
|
|
if (increment != 0.0) {
|
|
|
|
return constructIncrement(increment, minMaxFrac);
|
|
|
|
} else {
|
|
|
|
return constructFraction(minMaxFrac, minMaxFrac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
// Public method on CurrencyPrecision subclass
|
|
|
|
Precision CurrencyPrecision::withCurrency(const CurrencyUnit ¤cy) const {
|
2017-09-27 00:25:20 +00:00
|
|
|
UErrorCode localStatus = U_ZERO_ERROR;
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision result = Precision::withCurrency(currency, localStatus);
|
2017-09-27 00:25:20 +00:00
|
|
|
if (U_FAILURE(localStatus)) {
|
|
|
|
return {localStatus};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision IncrementPrecision::withMinFraction(int32_t minFrac) const {
|
2017-09-27 00:25:20 +00:00
|
|
|
if (fType == RND_ERROR) { return *this; } // no-op in error state
|
|
|
|
if (minFrac >= 0 && minFrac <= kMaxIntFracSig) {
|
|
|
|
return constructIncrement(fUnion.increment.fIncrement, minFrac);
|
|
|
|
} else {
|
2018-02-08 06:06:08 +00:00
|
|
|
return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
FractionPrecision Precision::constructFraction(int32_t minFrac, int32_t maxFrac) {
|
2017-09-27 00:25:20 +00:00
|
|
|
FractionSignificantSettings settings;
|
2018-02-08 06:06:08 +00:00
|
|
|
settings.fMinFrac = static_cast<digits_t>(minFrac);
|
|
|
|
settings.fMaxFrac = static_cast<digits_t>(maxFrac);
|
2017-09-27 00:25:20 +00:00
|
|
|
settings.fMinSig = -1;
|
|
|
|
settings.fMaxSig = -1;
|
2018-05-05 06:32:29 +00:00
|
|
|
PrecisionUnion union_;
|
2017-09-27 00:25:20 +00:00
|
|
|
union_.fracSig = settings;
|
|
|
|
return {RND_FRACTION, union_, kDefaultMode};
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision Precision::constructSignificant(int32_t minSig, int32_t maxSig) {
|
2017-09-27 00:25:20 +00:00
|
|
|
FractionSignificantSettings settings;
|
|
|
|
settings.fMinFrac = -1;
|
|
|
|
settings.fMaxFrac = -1;
|
2018-02-08 06:06:08 +00:00
|
|
|
settings.fMinSig = static_cast<digits_t>(minSig);
|
|
|
|
settings.fMaxSig = static_cast<digits_t>(maxSig);
|
2018-05-05 06:32:29 +00:00
|
|
|
PrecisionUnion union_;
|
2017-09-27 00:25:20 +00:00
|
|
|
union_.fracSig = settings;
|
|
|
|
return {RND_SIGNIFICANT, union_, kDefaultMode};
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
Precision
|
|
|
|
Precision::constructFractionSignificant(const FractionPrecision &base, int32_t minSig, int32_t maxSig) {
|
2017-09-27 00:25:20 +00:00
|
|
|
FractionSignificantSettings settings = base.fUnion.fracSig;
|
2018-02-08 06:06:08 +00:00
|
|
|
settings.fMinSig = static_cast<digits_t>(minSig);
|
|
|
|
settings.fMaxSig = static_cast<digits_t>(maxSig);
|
2018-05-05 06:32:29 +00:00
|
|
|
PrecisionUnion union_;
|
2017-09-27 00:25:20 +00:00
|
|
|
union_.fracSig = settings;
|
|
|
|
return {RND_FRACTION_SIGNIFICANT, union_, kDefaultMode};
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
IncrementPrecision Precision::constructIncrement(double increment, int32_t minFrac) {
|
2017-09-27 00:25:20 +00:00
|
|
|
IncrementSettings settings;
|
2018-11-03 05:19:53 +00:00
|
|
|
// Note: For number formatting, fIncrement is used for RND_INCREMENT but not
|
|
|
|
// RND_INCREMENT_ONE or RND_INCREMENT_FIVE. However, fIncrement is used in all
|
|
|
|
// three when constructing a skeleton.
|
2017-09-27 00:25:20 +00:00
|
|
|
settings.fIncrement = increment;
|
2018-02-08 06:06:08 +00:00
|
|
|
settings.fMinFrac = static_cast<digits_t>(minFrac);
|
2018-03-17 07:23:08 +00:00
|
|
|
// One of the few pre-computed quantities:
|
2018-03-24 08:06:34 +00:00
|
|
|
// Note: it is possible for minFrac to be more than maxFrac... (misleading)
|
2018-11-03 05:19:53 +00:00
|
|
|
int8_t singleDigit;
|
|
|
|
settings.fMaxFrac = roundingutils::doubleFractionLength(increment, &singleDigit);
|
2018-05-05 06:32:29 +00:00
|
|
|
PrecisionUnion union_;
|
2017-09-27 00:25:20 +00:00
|
|
|
union_.increment = settings;
|
2018-11-03 05:19:53 +00:00
|
|
|
if (singleDigit == 1) {
|
|
|
|
// NOTE: In C++, we must return the correct value type with the correct union.
|
|
|
|
// It would be invalid to return a RND_FRACTION here because the methods on the
|
|
|
|
// IncrementPrecision type assume that the union is backed by increment data.
|
|
|
|
return {RND_INCREMENT_ONE, union_, kDefaultMode};
|
|
|
|
} else if (singleDigit == 5) {
|
|
|
|
return {RND_INCREMENT_FIVE, union_, kDefaultMode};
|
|
|
|
} else {
|
|
|
|
return {RND_INCREMENT, union_, kDefaultMode};
|
|
|
|
}
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
CurrencyPrecision Precision::constructCurrency(UCurrencyUsage usage) {
|
|
|
|
PrecisionUnion union_;
|
2017-09-27 00:25:20 +00:00
|
|
|
union_.currencyUsage = usage;
|
|
|
|
return {RND_CURRENCY, union_, kDefaultMode};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
RoundingImpl::RoundingImpl(const Precision& precision, UNumberFormatRoundingMode roundingMode,
|
|
|
|
const CurrencyUnit& currency, UErrorCode& status)
|
|
|
|
: fPrecision(precision), fRoundingMode(roundingMode), fPassThrough(false) {
|
|
|
|
if (precision.fType == Precision::RND_CURRENCY) {
|
|
|
|
fPrecision = precision.withCurrency(currency, status);
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
RoundingImpl RoundingImpl::passThrough() {
|
2019-09-27 23:33:32 +00:00
|
|
|
return {};
|
2018-05-05 06:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RoundingImpl::isSignificantDigits() const {
|
|
|
|
return fPrecision.fType == Precision::RND_SIGNIFICANT;
|
|
|
|
}
|
|
|
|
|
2017-09-27 00:25:20 +00:00
|
|
|
int32_t
|
2018-05-05 06:32:29 +00:00
|
|
|
RoundingImpl::chooseMultiplierAndApply(impl::DecimalQuantity &input, const impl::MultiplierProducer &producer,
|
2017-09-27 00:25:20 +00:00
|
|
|
UErrorCode &status) {
|
2019-07-23 04:41:43 +00:00
|
|
|
// Do not call this method with zero, NaN, or infinity.
|
|
|
|
U_ASSERT(!input.isZeroish());
|
2017-12-14 00:47:43 +00:00
|
|
|
|
|
|
|
// Perform the first attempt at rounding.
|
|
|
|
int magnitude = input.getMagnitude();
|
|
|
|
int multiplier = producer.getMultiplier(magnitude);
|
2017-09-27 00:25:20 +00:00
|
|
|
input.adjustMagnitude(multiplier);
|
|
|
|
apply(input, status);
|
|
|
|
|
2017-12-14 00:47:43 +00:00
|
|
|
// If the number rounded to zero, exit.
|
2019-07-23 04:41:43 +00:00
|
|
|
if (input.isZeroish() || U_FAILURE(status)) {
|
2017-12-14 00:47:43 +00:00
|
|
|
return multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the new magnitude after rounding is the same as it was before rounding, then we are done.
|
|
|
|
// This case applies to most numbers.
|
|
|
|
if (input.getMagnitude() == magnitude + multiplier) {
|
|
|
|
return multiplier;
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:47:43 +00:00
|
|
|
// If the above case DIDN'T apply, then we have a case like 99.9 -> 100 or 999.9 -> 1000:
|
|
|
|
// The number rounded up to the next magnitude. Check if the multiplier changes; if it doesn't,
|
|
|
|
// we do not need to make any more adjustments.
|
|
|
|
int _multiplier = producer.getMultiplier(magnitude + 1);
|
|
|
|
if (multiplier == _multiplier) {
|
|
|
|
return multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a case like 999.9 -> 1000, where the correct output is "1K", not "1000".
|
|
|
|
// Fix the magnitude and re-apply the rounding strategy.
|
|
|
|
input.adjustMagnitude(_multiplier - multiplier);
|
|
|
|
apply(input, status);
|
|
|
|
return _multiplier;
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** This is the method that contains the actual rounding logic. */
|
2018-05-05 06:32:29 +00:00
|
|
|
void RoundingImpl::apply(impl::DecimalQuantity &value, UErrorCode& status) const {
|
|
|
|
if (fPassThrough) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (fPrecision.fType) {
|
|
|
|
case Precision::RND_BOGUS:
|
|
|
|
case Precision::RND_ERROR:
|
2017-09-27 00:25:20 +00:00
|
|
|
// Errors should be caught before the apply() method is called
|
|
|
|
status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
break;
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
case Precision::RND_NONE:
|
2017-09-27 00:25:20 +00:00
|
|
|
value.roundToInfinity();
|
|
|
|
break;
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
case Precision::RND_FRACTION:
|
2017-09-27 00:25:20 +00:00
|
|
|
value.roundToMagnitude(
|
2018-05-05 06:32:29 +00:00
|
|
|
getRoundingMagnitudeFraction(fPrecision.fUnion.fracSig.fMaxFrac),
|
|
|
|
fRoundingMode,
|
|
|
|
status);
|
2018-10-25 04:25:39 +00:00
|
|
|
value.setMinFraction(
|
|
|
|
uprv_max(0, -getDisplayMagnitudeFraction(fPrecision.fUnion.fracSig.fMinFrac)));
|
2017-09-27 00:25:20 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
case Precision::RND_SIGNIFICANT:
|
2017-09-27 00:25:20 +00:00
|
|
|
value.roundToMagnitude(
|
2018-05-05 06:32:29 +00:00
|
|
|
getRoundingMagnitudeSignificant(value, fPrecision.fUnion.fracSig.fMaxSig),
|
2017-09-27 00:25:20 +00:00
|
|
|
fRoundingMode,
|
|
|
|
status);
|
2018-10-25 04:25:39 +00:00
|
|
|
value.setMinFraction(
|
|
|
|
uprv_max(0, -getDisplayMagnitudeSignificant(value, fPrecision.fUnion.fracSig.fMinSig)));
|
2018-03-30 08:21:06 +00:00
|
|
|
// Make sure that digits are displayed on zero.
|
2019-07-23 04:41:43 +00:00
|
|
|
if (value.isZeroish() && fPrecision.fUnion.fracSig.fMinSig > 0) {
|
2018-10-25 04:25:39 +00:00
|
|
|
value.setMinInteger(1);
|
2018-03-30 08:21:06 +00:00
|
|
|
}
|
2017-09-27 00:25:20 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
case Precision::RND_FRACTION_SIGNIFICANT: {
|
|
|
|
int32_t displayMag = getDisplayMagnitudeFraction(fPrecision.fUnion.fracSig.fMinFrac);
|
|
|
|
int32_t roundingMag = getRoundingMagnitudeFraction(fPrecision.fUnion.fracSig.fMaxFrac);
|
|
|
|
if (fPrecision.fUnion.fracSig.fMinSig == -1) {
|
2017-09-27 00:25:20 +00:00
|
|
|
// Max Sig override
|
2018-05-05 06:32:29 +00:00
|
|
|
int32_t candidate = getRoundingMagnitudeSignificant(
|
|
|
|
value,
|
|
|
|
fPrecision.fUnion.fracSig.fMaxSig);
|
2017-09-27 00:25:20 +00:00
|
|
|
roundingMag = uprv_max(roundingMag, candidate);
|
|
|
|
} else {
|
|
|
|
// Min Sig override
|
2018-05-05 06:32:29 +00:00
|
|
|
int32_t candidate = getDisplayMagnitudeSignificant(
|
|
|
|
value,
|
|
|
|
fPrecision.fUnion.fracSig.fMinSig);
|
2017-09-27 00:25:20 +00:00
|
|
|
roundingMag = uprv_min(roundingMag, candidate);
|
|
|
|
}
|
|
|
|
value.roundToMagnitude(roundingMag, fRoundingMode, status);
|
2018-10-25 04:25:39 +00:00
|
|
|
value.setMinFraction(uprv_max(0, -displayMag));
|
2017-09-27 00:25:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
case Precision::RND_INCREMENT:
|
2017-09-27 03:02:02 +00:00
|
|
|
value.roundToIncrement(
|
2018-05-05 06:32:29 +00:00
|
|
|
fPrecision.fUnion.increment.fIncrement,
|
|
|
|
fRoundingMode,
|
2018-11-03 05:19:53 +00:00
|
|
|
status);
|
|
|
|
value.setMinFraction(fPrecision.fUnion.increment.fMinFrac);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Precision::RND_INCREMENT_ONE:
|
|
|
|
value.roundToMagnitude(
|
|
|
|
-fPrecision.fUnion.increment.fMaxFrac,
|
|
|
|
fRoundingMode,
|
|
|
|
status);
|
|
|
|
value.setMinFraction(fPrecision.fUnion.increment.fMinFrac);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Precision::RND_INCREMENT_FIVE:
|
|
|
|
value.roundToNickel(
|
|
|
|
-fPrecision.fUnion.increment.fMaxFrac,
|
|
|
|
fRoundingMode,
|
2018-05-05 06:32:29 +00:00
|
|
|
status);
|
2018-10-25 04:25:39 +00:00
|
|
|
value.setMinFraction(fPrecision.fUnion.increment.fMinFrac);
|
2017-09-27 00:25:20 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
case Precision::RND_CURRENCY:
|
2017-09-27 00:25:20 +00:00
|
|
|
// Call .withCurrency() before .apply()!
|
2019-01-09 21:29:18 +00:00
|
|
|
UPRV_UNREACHABLE;
|
2018-11-03 05:19:53 +00:00
|
|
|
|
|
|
|
default:
|
2019-01-09 21:29:18 +00:00
|
|
|
UPRV_UNREACHABLE;
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 06:32:29 +00:00
|
|
|
void RoundingImpl::apply(impl::DecimalQuantity &value, int32_t minInt, UErrorCode /*status*/) {
|
2017-09-27 00:25:20 +00:00
|
|
|
// This method is intended for the one specific purpose of helping print "00.000E0".
|
2018-05-05 06:32:29 +00:00
|
|
|
U_ASSERT(isSignificantDigits());
|
2019-07-23 04:41:43 +00:00
|
|
|
U_ASSERT(value.isZeroish());
|
2018-10-25 04:25:39 +00:00
|
|
|
value.setMinFraction(fPrecision.fUnion.fracSig.fMinSig - minInt);
|
2017-09-27 00:25:20 +00:00
|
|
|
}
|
2017-09-27 05:31:57 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|