2018-03-03 08:26:58 +00:00
|
|
|
// © 2018 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
#include <cmath>
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2018-03-03 08:26:58 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING && !UPRV_INCOMPLETE_CPP11_SUPPORT
|
2002-09-20 01:54:48 +00:00
|
|
|
|
2018-03-03 08:26:58 +00:00
|
|
|
// Allow implicit conversion from char16_t* to UnicodeString for this file:
|
|
|
|
// Helpful in toString methods and elsewhere.
|
|
|
|
#define UNISTR_FROM_STRING_EXPLICIT
|
2013-10-17 18:44:44 +00:00
|
|
|
|
2018-03-13 09:25:41 +00:00
|
|
|
#include "unicode/errorcode.h"
|
2018-03-03 10:53:01 +00:00
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "number_decimalquantity.h"
|
2018-03-13 09:25:41 +00:00
|
|
|
#include "number_types.h"
|
|
|
|
#include "numparse_impl.h"
|
|
|
|
#include "number_mapper.h"
|
|
|
|
#include "number_patternstring.h"
|
2018-03-14 09:15:27 +00:00
|
|
|
#include "putilimp.h"
|
|
|
|
#include "number_utils.h"
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-03 08:26:58 +00:00
|
|
|
using namespace icu;
|
2018-03-03 10:53:01 +00:00
|
|
|
using namespace icu::number;
|
|
|
|
using namespace icu::number::impl;
|
2018-03-13 09:25:41 +00:00
|
|
|
using namespace icu::numparse;
|
|
|
|
using namespace icu::numparse::impl;
|
2018-03-03 10:53:01 +00:00
|
|
|
using ERoundingMode = icu::DecimalFormat::ERoundingMode;
|
|
|
|
using EPadPosition = icu::DecimalFormat::EPadPosition;
|
|
|
|
|
|
|
|
|
2018-03-16 09:20:43 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormat)
|
|
|
|
|
|
|
|
|
2018-03-13 09:25:41 +00:00
|
|
|
DecimalFormat::DecimalFormat(UErrorCode& status)
|
|
|
|
: DecimalFormat(nullptr, status) {
|
2018-04-14 08:10:45 +00:00
|
|
|
// Use the default locale and decimal pattern.
|
|
|
|
const char* localeName = Locale::getDefault().getName();
|
|
|
|
LocalPointer<NumberingSystem> ns(NumberingSystem::createInstance(status));
|
|
|
|
UnicodeString patternString = utils::getPatternForStyle(
|
|
|
|
localeName,
|
|
|
|
ns->getName(),
|
|
|
|
CLDR_PATTERN_STYLE_DECIMAL,
|
|
|
|
status);
|
|
|
|
setPropertiesFromPattern(patternString, IGNORE_ROUNDING_IF_CURRENCY, status);
|
|
|
|
refreshFormatter(status);
|
2018-03-13 09:25:41 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 09:25:41 +00:00
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern, UErrorCode& status)
|
|
|
|
: DecimalFormat(nullptr, status) {
|
|
|
|
setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
|
|
|
|
refreshFormatter(status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
|
2018-03-13 09:25:41 +00:00
|
|
|
UErrorCode& status)
|
|
|
|
: DecimalFormat(symbolsToAdopt, status) {
|
|
|
|
setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
|
2018-03-17 01:31:52 +00:00
|
|
|
refreshFormatter(status);
|
2018-03-13 09:25:41 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
|
2018-03-13 09:25:41 +00:00
|
|
|
UNumberFormatStyle style, UErrorCode& status)
|
|
|
|
: DecimalFormat(symbolsToAdopt, status) {
|
|
|
|
// If choice is a currency type, ignore the rounding information.
|
|
|
|
if (style == UNumberFormatStyle::UNUM_CURRENCY || style == UNumberFormatStyle::UNUM_CURRENCY_ISO ||
|
|
|
|
style == UNumberFormatStyle::UNUM_CURRENCY_ACCOUNTING ||
|
|
|
|
style == UNumberFormatStyle::UNUM_CASH_CURRENCY ||
|
|
|
|
style == UNumberFormatStyle::UNUM_CURRENCY_STANDARD ||
|
|
|
|
style == UNumberFormatStyle::UNUM_CURRENCY_PLURAL) {
|
|
|
|
setPropertiesFromPattern(pattern, IGNORE_ROUNDING_ALWAYS, status);
|
|
|
|
} else {
|
|
|
|
setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
|
|
|
|
}
|
2018-04-07 08:49:11 +00:00
|
|
|
// Note: in Java, CurrencyPluralInfo is set in NumberFormat.java, but in C++, it is not set there,
|
|
|
|
// so we have to set it here.
|
|
|
|
if (style == UNumberFormatStyle::UNUM_CURRENCY_PLURAL) {
|
|
|
|
LocalPointer<CurrencyPluralInfo> cpi(
|
|
|
|
new CurrencyPluralInfo(fSymbols->getLocale(), status),
|
|
|
|
status);
|
|
|
|
if (U_FAILURE(status)) { return; }
|
|
|
|
fProperties->currencyPluralInfo.fPtr.adoptInstead(cpi.orphan());
|
|
|
|
}
|
2018-03-13 09:25:41 +00:00
|
|
|
refreshFormatter(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
DecimalFormat::DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status) {
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties.adoptInsteadAndCheckErrorCode(new DecimalFormatProperties(), status);
|
|
|
|
fExportedProperties.adoptInsteadAndCheckErrorCode(new DecimalFormatProperties(), status);
|
2018-03-15 10:08:26 +00:00
|
|
|
fWarehouse.adoptInsteadAndCheckErrorCode(new DecimalFormatWarehouse(), status);
|
2018-03-13 09:25:41 +00:00
|
|
|
if (symbolsToAdopt == nullptr) {
|
2018-03-14 09:15:27 +00:00
|
|
|
fSymbols.adoptInsteadAndCheckErrorCode(new DecimalFormatSymbols(status), status);
|
2018-03-13 09:25:41 +00:00
|
|
|
} else {
|
2018-03-14 09:15:27 +00:00
|
|
|
fSymbols.adoptInsteadAndCheckErrorCode(symbolsToAdopt, status);
|
2018-03-13 09:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
#if UCONFIG_HAVE_PARSEALLINPUT
|
|
|
|
|
|
|
|
void DecimalFormat::setParseAllInput(UNumberFormatAttributeValue value) {
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->parseAllInput = value;
|
2018-03-13 10:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
DecimalFormat&
|
2018-03-13 10:11:36 +00:00
|
|
|
DecimalFormat::setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) { return *this; }
|
|
|
|
|
|
|
|
switch (attr) {
|
|
|
|
case UNUM_LENIENT_PARSE:
|
|
|
|
setLenient(newValue != 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_PARSE_INT_ONLY:
|
|
|
|
setParseIntegerOnly(newValue != 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_GROUPING_USED:
|
|
|
|
setGroupingUsed(newValue != 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_DECIMAL_ALWAYS_SHOWN:
|
|
|
|
setDecimalSeparatorAlwaysShown(newValue != 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_INTEGER_DIGITS:
|
|
|
|
setMaximumIntegerDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_INTEGER_DIGITS:
|
|
|
|
setMinimumIntegerDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_INTEGER_DIGITS:
|
|
|
|
setMinimumIntegerDigits(newValue);
|
|
|
|
setMaximumIntegerDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_FRACTION_DIGITS:
|
|
|
|
setMaximumFractionDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_FRACTION_DIGITS:
|
|
|
|
setMinimumFractionDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_FRACTION_DIGITS:
|
|
|
|
setMinimumFractionDigits(newValue);
|
|
|
|
setMaximumFractionDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_SIGNIFICANT_DIGITS_USED:
|
|
|
|
setSignificantDigitsUsed(newValue != 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_SIGNIFICANT_DIGITS:
|
|
|
|
setMaximumSignificantDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_SIGNIFICANT_DIGITS:
|
|
|
|
setMinimumSignificantDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MULTIPLIER:
|
|
|
|
setMultiplier(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_GROUPING_SIZE:
|
|
|
|
setGroupingSize(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_ROUNDING_MODE:
|
|
|
|
setRoundingMode((DecimalFormat::ERoundingMode) newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_FORMAT_WIDTH:
|
|
|
|
setFormatWidth(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_PADDING_POSITION:
|
|
|
|
/** The position at which padding will take place. */
|
|
|
|
setPadPosition((DecimalFormat::EPadPosition) newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_SECONDARY_GROUPING_SIZE:
|
|
|
|
setSecondaryGroupingSize(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if UCONFIG_HAVE_PARSEALLINPUT
|
|
|
|
case UNUM_PARSE_ALL_INPUT:
|
|
|
|
setParseAllInput((UNumberFormatAttributeValue) newValue);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case UNUM_PARSE_NO_EXPONENT:
|
|
|
|
setParseNoExponent((UBool) newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_PARSE_DECIMAL_MARK_REQUIRED:
|
|
|
|
setDecimalPatternMatchRequired((UBool) newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_CURRENCY_USAGE:
|
|
|
|
setCurrencyUsage((UCurrencyUsage) newValue, &status);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MINIMUM_GROUPING_DIGITS:
|
|
|
|
setMinimumGroupingDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
case UNUM_PARSE_CASE_SENSITIVE:
|
|
|
|
setParseCaseSensitive(static_cast<UBool>(newValue));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_SIGN_ALWAYS_SHOWN:
|
|
|
|
setSignAlwaysShown(static_cast<UBool>(newValue));
|
|
|
|
break;
|
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
default:
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// TODO: UNUM_SCALE?
|
|
|
|
// TODO: UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS?
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DecimalFormat::getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const {
|
|
|
|
if (U_FAILURE(status)) { return -1; }
|
|
|
|
switch (attr) {
|
|
|
|
case UNUM_LENIENT_PARSE:
|
|
|
|
return isLenient();
|
|
|
|
|
|
|
|
case UNUM_PARSE_INT_ONLY:
|
|
|
|
return isParseIntegerOnly();
|
|
|
|
|
|
|
|
case UNUM_GROUPING_USED:
|
|
|
|
return isGroupingUsed();
|
|
|
|
|
|
|
|
case UNUM_DECIMAL_ALWAYS_SHOWN:
|
|
|
|
return isDecimalSeparatorAlwaysShown();
|
|
|
|
|
|
|
|
case UNUM_MAX_INTEGER_DIGITS:
|
|
|
|
return getMaximumIntegerDigits();
|
|
|
|
|
|
|
|
case UNUM_MIN_INTEGER_DIGITS:
|
|
|
|
return getMinimumIntegerDigits();
|
|
|
|
|
|
|
|
case UNUM_INTEGER_DIGITS:
|
|
|
|
// TBD: what should this return?
|
|
|
|
return getMinimumIntegerDigits();
|
|
|
|
|
|
|
|
case UNUM_MAX_FRACTION_DIGITS:
|
|
|
|
return getMaximumFractionDigits();
|
|
|
|
|
|
|
|
case UNUM_MIN_FRACTION_DIGITS:
|
|
|
|
return getMinimumFractionDigits();
|
|
|
|
|
|
|
|
case UNUM_FRACTION_DIGITS:
|
|
|
|
// TBD: what should this return?
|
|
|
|
return getMinimumFractionDigits();
|
|
|
|
|
|
|
|
case UNUM_SIGNIFICANT_DIGITS_USED:
|
|
|
|
return areSignificantDigitsUsed();
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
case UNUM_MAX_SIGNIFICANT_DIGITS:
|
|
|
|
return getMaximumSignificantDigits();
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
case UNUM_MIN_SIGNIFICANT_DIGITS:
|
|
|
|
return getMinimumSignificantDigits();
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
case UNUM_MULTIPLIER:
|
|
|
|
return getMultiplier();
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
case UNUM_GROUPING_SIZE:
|
|
|
|
return getGroupingSize();
|
|
|
|
|
|
|
|
case UNUM_ROUNDING_MODE:
|
|
|
|
return getRoundingMode();
|
|
|
|
|
|
|
|
case UNUM_FORMAT_WIDTH:
|
|
|
|
return getFormatWidth();
|
|
|
|
|
|
|
|
case UNUM_PADDING_POSITION:
|
|
|
|
return getPadPosition();
|
|
|
|
|
|
|
|
case UNUM_SECONDARY_GROUPING_SIZE:
|
|
|
|
return getSecondaryGroupingSize();
|
|
|
|
|
|
|
|
case UNUM_PARSE_NO_EXPONENT:
|
2018-03-21 09:48:55 +00:00
|
|
|
return isParseNoExponent();
|
2018-03-13 10:11:36 +00:00
|
|
|
|
|
|
|
case UNUM_PARSE_DECIMAL_MARK_REQUIRED:
|
|
|
|
return isDecimalPatternMatchRequired();
|
|
|
|
|
|
|
|
case UNUM_CURRENCY_USAGE:
|
|
|
|
return getCurrencyUsage();
|
|
|
|
|
|
|
|
case UNUM_MINIMUM_GROUPING_DIGITS:
|
|
|
|
return getMinimumGroupingDigits();
|
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
case UNUM_PARSE_CASE_SENSITIVE:
|
|
|
|
return isParseCaseSensitive();
|
|
|
|
|
|
|
|
case UNUM_SIGN_ALWAYS_SHOWN:
|
|
|
|
return isSignAlwaysShown();
|
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
default:
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// TODO: UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS?
|
|
|
|
// TODO: UNUM_SCALE?
|
|
|
|
|
|
|
|
return -1; /* undefined */
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setGroupingUsed(UBool enabled) {
|
2018-03-31 03:10:44 +00:00
|
|
|
NumberFormat::setGroupingUsed(enabled); // to set field for compatibility
|
2018-04-14 07:15:19 +00:00
|
|
|
fProperties->groupingUsed = enabled;
|
2018-03-13 10:11:36 +00:00
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setParseIntegerOnly(UBool value) {
|
2018-03-21 09:48:55 +00:00
|
|
|
NumberFormat::setParseIntegerOnly(value); // to set field for compatibility
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->parseIntegerOnly = value;
|
2018-03-13 10:11:36 +00:00
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
void DecimalFormat::setLenient(UBool enable) {
|
|
|
|
NumberFormat::setLenient(enable); // to set field for compatibility
|
|
|
|
fProperties->parseMode = enable ? PARSE_MODE_LENIENT : PARSE_MODE_STRICT;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
|
|
|
|
2018-03-03 10:53:01 +00:00
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
|
2018-03-14 09:15:27 +00:00
|
|
|
UParseError&, UErrorCode& status)
|
2018-03-13 09:25:41 +00:00
|
|
|
: DecimalFormat(symbolsToAdopt, status) {
|
|
|
|
// TODO: What is parseError for?
|
|
|
|
setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
|
|
|
|
refreshFormatter(status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols,
|
2018-03-13 09:25:41 +00:00
|
|
|
UErrorCode& status)
|
|
|
|
: DecimalFormat(new DecimalFormatSymbols(symbols), status) {
|
|
|
|
setPropertiesFromPattern(pattern, IGNORE_ROUNDING_IF_CURRENCY, status);
|
|
|
|
refreshFormatter(status);
|
|
|
|
}
|
|
|
|
|
2018-04-14 07:15:19 +00:00
|
|
|
DecimalFormat::DecimalFormat(const DecimalFormat& source) : NumberFormat(source) {
|
2018-03-30 10:37:24 +00:00
|
|
|
fProperties.adoptInstead(new DecimalFormatProperties(*source.fProperties));
|
2018-03-14 09:15:27 +00:00
|
|
|
fExportedProperties.adoptInstead(new DecimalFormatProperties());
|
2018-03-15 10:08:26 +00:00
|
|
|
fWarehouse.adoptInstead(new DecimalFormatWarehouse());
|
2018-03-14 09:15:27 +00:00
|
|
|
fSymbols.adoptInstead(new DecimalFormatSymbols(*source.fSymbols));
|
2018-03-15 10:08:26 +00:00
|
|
|
if (fProperties == nullptr || fExportedProperties == nullptr || fWarehouse == nullptr ||
|
|
|
|
fSymbols == nullptr) {
|
2018-03-13 09:25:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-13 10:11:36 +00:00
|
|
|
refreshFormatterNoError();
|
2018-03-13 09:25:41 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
DecimalFormat& DecimalFormat::operator=(const DecimalFormat& rhs) {
|
2018-03-14 09:15:27 +00:00
|
|
|
*fProperties = *rhs.fProperties;
|
|
|
|
fExportedProperties->clear();
|
|
|
|
fSymbols.adoptInstead(new DecimalFormatSymbols(*rhs.fSymbols));
|
2018-03-13 10:11:36 +00:00
|
|
|
refreshFormatterNoError();
|
2018-03-14 09:15:27 +00:00
|
|
|
return *this;
|
2018-03-13 10:11:36 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
DecimalFormat::~DecimalFormat() = default;
|
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
Format* DecimalFormat::clone() const {
|
|
|
|
return new DecimalFormat(*this);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
UBool DecimalFormat::operator==(const Format& other) const {
|
2018-03-14 09:15:27 +00:00
|
|
|
auto* otherDF = dynamic_cast<const DecimalFormat*>(&other);
|
2018-03-13 10:11:36 +00:00
|
|
|
if (otherDF == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
return *fProperties == *otherDF->fProperties && *fSymbols == *otherDF->fSymbols;
|
2018-03-13 10:11:36 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
UnicodeString& DecimalFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos) const {
|
|
|
|
ErrorCode localStatus;
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatDouble(number, localStatus);
|
2018-03-13 10:11:36 +00:00
|
|
|
output.populateFieldPosition(pos, localStatus);
|
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
UnicodeString& DecimalFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatDouble(number, status);
|
2018-03-13 10:11:36 +00:00
|
|
|
output.populateFieldPosition(pos, status);
|
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatDouble(number, status);
|
2018-04-11 05:52:58 +00:00
|
|
|
if (posIter != nullptr) {
|
|
|
|
output.populateFieldPositionIterator(*posIter, status);
|
|
|
|
}
|
2018-03-13 10:11:36 +00:00
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
UnicodeString& DecimalFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const {
|
|
|
|
return format(static_cast<int64_t> (number), appendTo, pos);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
UnicodeString& DecimalFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
|
|
|
return format(static_cast<int64_t> (number), appendTo, pos, status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
|
|
|
return format(static_cast<int64_t> (number), appendTo, posIter, status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
UnicodeString& DecimalFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const {
|
|
|
|
ErrorCode localStatus;
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatInt(number, localStatus);
|
2018-03-13 10:11:36 +00:00
|
|
|
output.populateFieldPosition(pos, localStatus);
|
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
UnicodeString& DecimalFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatInt(number, status);
|
2018-03-13 10:11:36 +00:00
|
|
|
output.populateFieldPosition(pos, status);
|
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatInt(number, status);
|
2018-04-11 05:52:58 +00:00
|
|
|
if (posIter != nullptr) {
|
|
|
|
output.populateFieldPositionIterator(*posIter, status);
|
|
|
|
}
|
2018-03-13 10:11:36 +00:00
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
|
|
|
ErrorCode localStatus;
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatDecimal(number, localStatus);
|
2018-04-11 05:52:58 +00:00
|
|
|
if (posIter != nullptr) {
|
|
|
|
output.populateFieldPositionIterator(*posIter, status);
|
|
|
|
}
|
2018-03-13 10:11:36 +00:00
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-13 08:12:05 +00:00
|
|
|
UnicodeString& DecimalFormat::format(const DecimalQuantity& number, UnicodeString& appendTo,
|
2018-03-13 10:11:36 +00:00
|
|
|
FieldPositionIterator* posIter, UErrorCode& status) const {
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatDecimalQuantity(number, status);
|
2018-04-11 05:52:58 +00:00
|
|
|
if (posIter != nullptr) {
|
|
|
|
output.populateFieldPositionIterator(*posIter, status);
|
|
|
|
}
|
2018-03-13 10:11:36 +00:00
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-13 08:12:05 +00:00
|
|
|
|
2018-03-03 10:53:01 +00:00
|
|
|
UnicodeString&
|
2018-03-13 08:12:05 +00:00
|
|
|
DecimalFormat::format(const DecimalQuantity& number, UnicodeString& appendTo, FieldPosition& pos,
|
2018-03-13 10:11:36 +00:00
|
|
|
UErrorCode& status) const {
|
2018-03-14 09:15:27 +00:00
|
|
|
FormattedNumber output = fFormatter->formatDecimalQuantity(number, status);
|
2018-03-13 10:11:36 +00:00
|
|
|
output.populateFieldPosition(pos, status);
|
|
|
|
auto appendable = UnicodeStringAppendable(appendTo);
|
|
|
|
output.appendTo(appendable);
|
|
|
|
return appendTo;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
void DecimalFormat::parse(const UnicodeString& text, Formattable& output,
|
|
|
|
ParsePosition& parsePosition) const {
|
|
|
|
if (parsePosition.getIndex() < 0 || parsePosition.getIndex() >= text.length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorCode status;
|
|
|
|
ParsedNumber result;
|
|
|
|
// Note: if this is a currency instance, currencies will be matched despite the fact that we are not in the
|
|
|
|
// parseCurrency method (backwards compatibility)
|
|
|
|
int32_t startIndex = parsePosition.getIndex();
|
|
|
|
fParser->parse(text, startIndex, true, result, status);
|
2018-04-14 07:15:19 +00:00
|
|
|
// TODO: Do we need to check for fProperties->parseAllInput (UCONFIG_HAVE_PARSEALLINPUT) here?
|
2018-03-21 09:48:55 +00:00
|
|
|
if (result.success()) {
|
|
|
|
parsePosition.setIndex(result.charEnd);
|
|
|
|
result.populateFormattable(output);
|
|
|
|
} else {
|
|
|
|
parsePosition.setErrorIndex(startIndex + result.charEnd);
|
|
|
|
}
|
2018-03-13 10:11:36 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
CurrencyAmount* DecimalFormat::parseCurrency(const UnicodeString& text, ParsePosition& parsePosition) const {
|
|
|
|
if (parsePosition.getIndex() < 0 || parsePosition.getIndex() >= text.length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorCode status;
|
|
|
|
ParsedNumber result;
|
|
|
|
// Note: if this is a currency instance, currencies will be matched despite the fact that we are not in the
|
|
|
|
// parseCurrency method (backwards compatibility)
|
|
|
|
int32_t startIndex = parsePosition.getIndex();
|
|
|
|
fParserWithCurrency->parse(text, startIndex, true, result, status);
|
2018-04-14 07:15:19 +00:00
|
|
|
// TODO: Do we need to check for fProperties->parseAllInput (UCONFIG_HAVE_PARSEALLINPUT) here?
|
2018-03-21 09:48:55 +00:00
|
|
|
if (result.success()) {
|
|
|
|
parsePosition.setIndex(result.charEnd);
|
|
|
|
Formattable formattable;
|
|
|
|
result.populateFormattable(formattable);
|
|
|
|
return new CurrencyAmount(formattable, result.currencyCode, status);
|
|
|
|
} else {
|
|
|
|
parsePosition.setErrorIndex(startIndex + result.charEnd);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-13 10:11:36 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
const DecimalFormatSymbols* DecimalFormat::getDecimalFormatSymbols(void) const {
|
|
|
|
return fSymbols.getAlias();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt) {
|
|
|
|
if (symbolsToAdopt == nullptr) {
|
|
|
|
return; // do not allow caller to set fSymbols to NULL
|
|
|
|
}
|
|
|
|
fSymbols.adoptInstead(symbolsToAdopt);
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols) {
|
|
|
|
fSymbols.adoptInstead(new DecimalFormatSymbols(symbols));
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
const CurrencyPluralInfo* DecimalFormat::getCurrencyPluralInfo(void) const {
|
|
|
|
return fProperties->currencyPluralInfo.fPtr.getAlias();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt) {
|
|
|
|
fProperties->currencyPluralInfo.fPtr.adoptInstead(toAdopt);
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setCurrencyPluralInfo(const CurrencyPluralInfo& info) {
|
|
|
|
*fProperties->currencyPluralInfo.fPtr = info; // copy-assignment operator
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UnicodeString& DecimalFormat::getPositivePrefix(UnicodeString& result) const {
|
|
|
|
ErrorCode localStatus;
|
2018-04-11 11:10:52 +00:00
|
|
|
fFormatter->getAffix(true, false, result, localStatus);
|
2018-03-14 09:15:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setPositivePrefix(const UnicodeString& newValue) {
|
|
|
|
fProperties->positivePrefix = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UnicodeString& DecimalFormat::getNegativePrefix(UnicodeString& result) const {
|
|
|
|
ErrorCode localStatus;
|
2018-04-11 11:10:52 +00:00
|
|
|
fFormatter->getAffix(true, true, result, localStatus);
|
2018-03-14 09:15:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setNegativePrefix(const UnicodeString& newValue) {
|
|
|
|
fProperties->negativePrefix = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UnicodeString& DecimalFormat::getPositiveSuffix(UnicodeString& result) const {
|
|
|
|
ErrorCode localStatus;
|
2018-04-11 11:10:52 +00:00
|
|
|
fFormatter->getAffix(false, false, result, localStatus);
|
2018-03-14 09:15:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setPositiveSuffix(const UnicodeString& newValue) {
|
|
|
|
fProperties->positiveSuffix = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UnicodeString& DecimalFormat::getNegativeSuffix(UnicodeString& result) const {
|
|
|
|
ErrorCode localStatus;
|
2018-04-11 11:10:52 +00:00
|
|
|
fFormatter->getAffix(false, true, result, localStatus);
|
2018-03-14 09:15:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setNegativeSuffix(const UnicodeString& newValue) {
|
|
|
|
fProperties->negativeSuffix = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
UBool DecimalFormat::isSignAlwaysShown() const {
|
|
|
|
return fProperties->signAlwaysShown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setSignAlwaysShown(UBool value) {
|
|
|
|
fProperties->signAlwaysShown = value;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
int32_t DecimalFormat::getMultiplier(void) const {
|
|
|
|
if (fProperties->multiplier != 1) {
|
|
|
|
return fProperties->multiplier;
|
|
|
|
} else if (fProperties->magnitudeMultiplier != 0) {
|
|
|
|
return static_cast<int32_t>(uprv_pow10(fProperties->magnitudeMultiplier));
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMultiplier(int32_t multiplier) {
|
|
|
|
if (multiplier == 0) {
|
|
|
|
multiplier = 1; // one being the benign default value for a multiplier.
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
// Try to convert to a magnitude multiplier first
|
|
|
|
int delta = 0;
|
|
|
|
int value = multiplier;
|
|
|
|
while (value != 1) {
|
|
|
|
delta++;
|
|
|
|
int temp = value / 10;
|
|
|
|
if (temp * 10 != value) {
|
|
|
|
delta = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value = temp;
|
|
|
|
}
|
|
|
|
if (delta != -1) {
|
|
|
|
fProperties->magnitudeMultiplier = delta;
|
|
|
|
fProperties->multiplier = 1;
|
|
|
|
} else {
|
|
|
|
fProperties->magnitudeMultiplier = 0;
|
|
|
|
fProperties->multiplier = multiplier;
|
|
|
|
}
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
double DecimalFormat::getRoundingIncrement(void) const {
|
|
|
|
return fExportedProperties->roundingIncrement;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setRoundingIncrement(double newValue) {
|
|
|
|
fProperties->roundingIncrement = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
ERoundingMode DecimalFormat::getRoundingMode(void) const {
|
|
|
|
// UNumberFormatRoundingMode and ERoundingMode have the same values.
|
|
|
|
return static_cast<ERoundingMode>(fExportedProperties->roundingMode.getNoError());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setRoundingMode(ERoundingMode roundingMode) {
|
2018-03-31 03:10:44 +00:00
|
|
|
NumberFormat::setMaximumIntegerDigits(roundingMode); // to set field for compatibility
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->roundingMode = static_cast<UNumberFormatRoundingMode>(roundingMode);
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DecimalFormat::getFormatWidth(void) const {
|
|
|
|
return fProperties->formatWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setFormatWidth(int32_t width) {
|
|
|
|
fProperties->formatWidth = width;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UnicodeString DecimalFormat::getPadCharacterString() const {
|
2018-04-11 11:10:52 +00:00
|
|
|
if (fProperties->padString.isBogus()) {
|
|
|
|
// Readonly-alias the static string kFallbackPaddingString
|
|
|
|
return {TRUE, kFallbackPaddingString, -1};
|
|
|
|
} else {
|
|
|
|
return fProperties->padString;
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setPadCharacter(const UnicodeString& padChar) {
|
|
|
|
if (padChar.length() > 0) {
|
|
|
|
fProperties->padString = UnicodeString(padChar.char32At(0));
|
|
|
|
} else {
|
|
|
|
fProperties->padString.setToBogus();
|
|
|
|
}
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
EPadPosition DecimalFormat::getPadPosition(void) const {
|
|
|
|
if (fProperties->padPosition.isNull()) {
|
|
|
|
return EPadPosition::kPadBeforePrefix;
|
|
|
|
} else {
|
|
|
|
// UNumberFormatPadPosition and EPadPosition have the same values.
|
|
|
|
return static_cast<EPadPosition>(fProperties->padPosition.getNoError());
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setPadPosition(EPadPosition padPos) {
|
|
|
|
fProperties->padPosition = static_cast<UNumberFormatPadPosition>(padPos);
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UBool DecimalFormat::isScientificNotation(void) const {
|
|
|
|
return fProperties->minimumExponentDigits != -1;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setScientificNotation(UBool useScientific) {
|
|
|
|
if (useScientific) {
|
|
|
|
fProperties->minimumExponentDigits = 1;
|
|
|
|
} else {
|
|
|
|
fProperties->minimumExponentDigits = -1;
|
|
|
|
}
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
int8_t DecimalFormat::getMinimumExponentDigits(void) const {
|
|
|
|
return static_cast<int8_t>(fProperties->minimumExponentDigits);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMinimumExponentDigits(int8_t minExpDig) {
|
|
|
|
fProperties->minimumExponentDigits = minExpDig;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UBool DecimalFormat::isExponentSignAlwaysShown(void) const {
|
|
|
|
return fProperties->exponentSignAlwaysShown;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setExponentSignAlwaysShown(UBool expSignAlways) {
|
|
|
|
fProperties->exponentSignAlwaysShown = expSignAlways;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
int32_t DecimalFormat::getGroupingSize(void) const {
|
2018-04-14 07:15:19 +00:00
|
|
|
if (fProperties->groupingSize < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
return fProperties->groupingSize;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setGroupingSize(int32_t newValue) {
|
|
|
|
fProperties->groupingSize = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
int32_t DecimalFormat::getSecondaryGroupingSize(void) const {
|
|
|
|
int grouping1 = fProperties->groupingSize;
|
|
|
|
int grouping2 = fProperties->secondaryGroupingSize;
|
|
|
|
if (grouping1 == grouping2 || grouping2 < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return grouping2;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setSecondaryGroupingSize(int32_t newValue) {
|
|
|
|
fProperties->secondaryGroupingSize = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
int32_t DecimalFormat::getMinimumGroupingDigits() const {
|
|
|
|
return fProperties->minimumGroupingDigits;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMinimumGroupingDigits(int32_t newValue) {
|
|
|
|
fProperties->minimumGroupingDigits = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UBool DecimalFormat::isDecimalSeparatorAlwaysShown(void) const {
|
|
|
|
return fProperties->decimalSeparatorAlwaysShown;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setDecimalSeparatorAlwaysShown(UBool newValue) {
|
|
|
|
fProperties->decimalSeparatorAlwaysShown = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UBool DecimalFormat::isDecimalPatternMatchRequired(void) const {
|
|
|
|
return fProperties->decimalPatternMatchRequired;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setDecimalPatternMatchRequired(UBool newValue) {
|
|
|
|
fProperties->decimalPatternMatchRequired = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
UBool DecimalFormat::isParseNoExponent() const {
|
2018-03-14 09:15:27 +00:00
|
|
|
return fProperties->parseNoExponent;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setParseNoExponent(UBool value) {
|
|
|
|
fProperties->parseNoExponent = value;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
UBool DecimalFormat::isParseCaseSensitive() const {
|
|
|
|
return fProperties->parseCaseSensitive;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setParseCaseSensitive(UBool value) {
|
|
|
|
fProperties->parseCaseSensitive = value;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UnicodeString& DecimalFormat::toPattern(UnicodeString& result) const {
|
|
|
|
// Pull some properties from exportedProperties and others from properties
|
|
|
|
// to keep affix patterns intact. In particular, pull rounding properties
|
|
|
|
// so that CurrencyUsage is reflected properly.
|
|
|
|
// TODO: Consider putting this logic in number_patternstring.cpp instead.
|
|
|
|
ErrorCode localStatus;
|
|
|
|
DecimalFormatProperties tprops(*fProperties);
|
|
|
|
bool useCurrency = ((!tprops.currency.isNull()) || !tprops.currencyPluralInfo.fPtr.isNull() ||
|
|
|
|
!tprops.currencyUsage.isNull() || AffixUtils::hasCurrencySymbols(
|
|
|
|
UnicodeStringCharSequence(tprops.positivePrefixPattern), localStatus) ||
|
|
|
|
AffixUtils::hasCurrencySymbols(
|
|
|
|
UnicodeStringCharSequence(tprops.positiveSuffixPattern), localStatus) ||
|
|
|
|
AffixUtils::hasCurrencySymbols(
|
|
|
|
UnicodeStringCharSequence(tprops.negativePrefixPattern), localStatus) ||
|
|
|
|
AffixUtils::hasCurrencySymbols(
|
|
|
|
UnicodeStringCharSequence(tprops.negativeSuffixPattern), localStatus));
|
|
|
|
if (useCurrency) {
|
|
|
|
tprops.minimumFractionDigits = fExportedProperties->minimumFractionDigits;
|
|
|
|
tprops.maximumFractionDigits = fExportedProperties->maximumFractionDigits;
|
|
|
|
tprops.roundingIncrement = fExportedProperties->roundingIncrement;
|
|
|
|
}
|
|
|
|
result = PatternStringUtils::propertiesToPatternString(tprops, localStatus);
|
|
|
|
return result;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UnicodeString& DecimalFormat::toLocalizedPattern(UnicodeString& result) const {
|
|
|
|
ErrorCode localStatus;
|
|
|
|
result = toPattern(result);
|
|
|
|
result = PatternStringUtils::convertLocalized(result, *fSymbols, true, localStatus);
|
|
|
|
return result;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::applyPattern(const UnicodeString& pattern, UParseError&, UErrorCode& status) {
|
|
|
|
// TODO: What is parseError for?
|
|
|
|
applyPattern(pattern, status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status) {
|
|
|
|
setPropertiesFromPattern(pattern, IGNORE_ROUNDING_NEVER, status);
|
|
|
|
refreshFormatter(status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::applyLocalizedPattern(const UnicodeString& localizedPattern, UParseError&,
|
|
|
|
UErrorCode& status) {
|
|
|
|
// TODO: What is parseError for?
|
|
|
|
applyLocalizedPattern(localizedPattern, status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::applyLocalizedPattern(const UnicodeString& localizedPattern, UErrorCode& status) {
|
|
|
|
UnicodeString pattern = PatternStringUtils::convertLocalized(
|
|
|
|
localizedPattern, *fSymbols, false, status);
|
|
|
|
applyPattern(pattern, status);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMaximumIntegerDigits(int32_t newValue) {
|
2018-04-12 05:15:19 +00:00
|
|
|
// For backwards compatibility, conflicting min/max need to keep the most recent setting.
|
|
|
|
int32_t min = fProperties->minimumIntegerDigits;
|
|
|
|
if (min >= 0 && min > newValue) {
|
|
|
|
fProperties->minimumIntegerDigits = newValue;
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->maximumIntegerDigits = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
|
2018-04-12 05:15:19 +00:00
|
|
|
// For backwards compatibility, conflicting min/max need to keep the most recent setting.
|
|
|
|
int32_t max = fProperties->maximumIntegerDigits;
|
|
|
|
if (max >= 0 && max < newValue) {
|
|
|
|
fProperties->maximumIntegerDigits = newValue;
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->minimumIntegerDigits = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
|
2018-04-12 05:15:19 +00:00
|
|
|
// For backwards compatibility, conflicting min/max need to keep the most recent setting.
|
|
|
|
int32_t min = fProperties->minimumFractionDigits;
|
|
|
|
if (min >= 0 && min > newValue) {
|
|
|
|
fProperties->minimumFractionDigits = newValue;
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->maximumFractionDigits = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
|
2018-04-12 05:15:19 +00:00
|
|
|
// For backwards compatibility, conflicting min/max need to keep the most recent setting.
|
|
|
|
int32_t max = fProperties->maximumFractionDigits;
|
|
|
|
if (max >= 0 && max < newValue) {
|
|
|
|
fProperties->maximumFractionDigits = newValue;
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->minimumFractionDigits = newValue;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
int32_t DecimalFormat::getMinimumSignificantDigits() const {
|
|
|
|
return fExportedProperties->minimumSignificantDigits;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
int32_t DecimalFormat::getMaximumSignificantDigits() const {
|
|
|
|
return fExportedProperties->maximumSignificantDigits;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMinimumSignificantDigits(int32_t value) {
|
|
|
|
int32_t max = fProperties->maximumSignificantDigits;
|
|
|
|
if (max >= 0 && max < value) {
|
|
|
|
fProperties->maximumSignificantDigits = value;
|
|
|
|
}
|
|
|
|
fProperties->minimumSignificantDigits = value;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setMaximumSignificantDigits(int32_t value) {
|
|
|
|
int32_t min = fProperties->minimumSignificantDigits;
|
|
|
|
if (min >= 0 && min > value) {
|
|
|
|
fProperties->minimumSignificantDigits = value;
|
|
|
|
}
|
|
|
|
fProperties->maximumSignificantDigits = value;
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UBool DecimalFormat::areSignificantDigitsUsed() const {
|
|
|
|
return fProperties->minimumSignificantDigits != -1 || fProperties->maximumSignificantDigits != -1;
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setSignificantDigitsUsed(UBool useSignificantDigits) {
|
|
|
|
if (useSignificantDigits) {
|
|
|
|
// These are the default values from the old implementation.
|
|
|
|
fProperties->minimumSignificantDigits = 1;
|
|
|
|
fProperties->maximumSignificantDigits = 6;
|
|
|
|
} else {
|
|
|
|
fProperties->minimumSignificantDigits = -1;
|
|
|
|
fProperties->maximumSignificantDigits = -1;
|
|
|
|
}
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setCurrency(const char16_t* theCurrency, UErrorCode& ec) {
|
2018-04-11 05:52:58 +00:00
|
|
|
NumberFormat::setCurrency(theCurrency, ec); // to set field for compatibility
|
2018-03-14 09:15:27 +00:00
|
|
|
fProperties->currency = CurrencyUnit(theCurrency, ec);
|
|
|
|
// TODO: Set values in fSymbols, too?
|
|
|
|
refreshFormatterNoError();
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setCurrency(const char16_t* theCurrency) {
|
|
|
|
ErrorCode localStatus;
|
2018-04-11 05:52:58 +00:00
|
|
|
NumberFormat::setCurrency(theCurrency, localStatus); // to set field for compatibility
|
2018-03-14 09:15:27 +00:00
|
|
|
setCurrency(theCurrency, localStatus);
|
|
|
|
}
|
2018-03-03 10:53:01 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
void DecimalFormat::setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec) {
|
|
|
|
fProperties->currencyUsage = newUsage;
|
|
|
|
refreshFormatter(*ec);
|
|
|
|
}
|
2013-02-21 22:37:26 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
UCurrencyUsage DecimalFormat::getCurrencyUsage() const {
|
|
|
|
// CurrencyUsage is not exported, so we have to get it from the input property bag.
|
|
|
|
// TODO: Should we export CurrencyUsage instead?
|
|
|
|
if (fProperties->currencyUsage.isNull()) {
|
|
|
|
return UCURR_USAGE_STANDARD;
|
|
|
|
}
|
|
|
|
return fProperties->currencyUsage.getNoError();
|
|
|
|
}
|
2013-06-20 22:09:42 +00:00
|
|
|
|
2018-03-13 08:12:05 +00:00
|
|
|
void
|
2018-03-14 09:15:27 +00:00
|
|
|
DecimalFormat::formatToDecimalQuantity(double number, DecimalQuantity& output, UErrorCode& status) const {
|
2018-03-16 09:20:43 +00:00
|
|
|
fFormatter->formatDouble(number, status).getDecimalQuantity(output, status);
|
2018-03-14 09:15:27 +00:00
|
|
|
}
|
2018-03-13 08:12:05 +00:00
|
|
|
|
|
|
|
void DecimalFormat::formatToDecimalQuantity(const Formattable& number, DecimalQuantity& output,
|
2018-03-14 09:15:27 +00:00
|
|
|
UErrorCode& status) const {
|
2018-03-16 09:20:43 +00:00
|
|
|
// Check if the Formattable is a DecimalQuantity
|
|
|
|
DecimalQuantity* dq = number.getDecimalQuantity();
|
|
|
|
if (dq != nullptr) {
|
|
|
|
fFormatter->formatDecimalQuantity(*dq, status).getDecimalQuantity(output, status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, it must be Double, Long (int32_t), or Int64:
|
|
|
|
switch (number.getType()) {
|
|
|
|
case Formattable::kDouble:
|
|
|
|
fFormatter->formatDouble(number.getDouble(), status).getDecimalQuantity(output, status);
|
|
|
|
break;
|
|
|
|
case Formattable::kLong:
|
|
|
|
fFormatter->formatInt(number.getLong(), status).getDecimalQuantity(output, status);
|
|
|
|
break;
|
|
|
|
case Formattable::kInt64:
|
|
|
|
default:
|
|
|
|
fFormatter->formatInt(number.getInt64(), status).getDecimalQuantity(output, status);
|
|
|
|
}
|
2018-03-14 09:15:27 +00:00
|
|
|
}
|
2018-03-13 08:12:05 +00:00
|
|
|
|
2018-03-15 10:08:26 +00:00
|
|
|
const number::LocalizedNumberFormatter& DecimalFormat::toNumberFormatter() const {
|
2018-03-14 09:15:27 +00:00
|
|
|
return *fFormatter;
|
|
|
|
}
|
2010-01-16 19:59:59 +00:00
|
|
|
|
2018-03-13 09:25:41 +00:00
|
|
|
/** Rebuilds the formatter object from the property bag. */
|
|
|
|
void DecimalFormat::refreshFormatter(UErrorCode& status) {
|
2018-03-14 09:15:27 +00:00
|
|
|
if (fExportedProperties == nullptr) {
|
|
|
|
// fExportedProperties is null only when the formatter is not ready yet.
|
2018-03-13 09:25:41 +00:00
|
|
|
// The only time when this happens is during legacy deserialization.
|
|
|
|
return;
|
|
|
|
}
|
2018-03-17 01:31:52 +00:00
|
|
|
|
|
|
|
// In C++, fSymbols is the source of truth for the locale.
|
|
|
|
Locale locale = fSymbols->getLocale();
|
2018-03-13 09:25:41 +00:00
|
|
|
|
2018-03-14 09:15:27 +00:00
|
|
|
fFormatter.adoptInsteadAndCheckErrorCode(
|
|
|
|
new LocalizedNumberFormatter(
|
|
|
|
NumberPropertyMapper::create(
|
2018-03-15 10:08:26 +00:00
|
|
|
*fProperties, *fSymbols, *fWarehouse, *fExportedProperties, status).locale(
|
2018-03-14 09:15:27 +00:00
|
|
|
locale)), status);
|
2018-03-15 10:08:26 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
fParser.adoptInsteadAndCheckErrorCode(
|
|
|
|
NumberParserImpl::createParserFromProperties(
|
|
|
|
*fProperties, *fSymbols, false, status), status);
|
2018-03-15 10:08:26 +00:00
|
|
|
|
2018-03-21 09:48:55 +00:00
|
|
|
fParserWithCurrency.adoptInsteadAndCheckErrorCode(
|
|
|
|
NumberParserImpl::createParserFromProperties(
|
|
|
|
*fProperties, *fSymbols, true, status), status);
|
2018-03-31 03:10:44 +00:00
|
|
|
|
|
|
|
// In order for the getters to work, we need to populate some fields in NumberFormat.
|
2018-04-11 05:52:58 +00:00
|
|
|
NumberFormat::setCurrency(fExportedProperties->currency.get(status).getISOCurrency(), status);
|
2018-03-31 03:10:44 +00:00
|
|
|
NumberFormat::setMaximumIntegerDigits(fExportedProperties->maximumIntegerDigits);
|
|
|
|
NumberFormat::setMinimumIntegerDigits(fExportedProperties->minimumIntegerDigits);
|
|
|
|
NumberFormat::setMaximumFractionDigits(fExportedProperties->maximumFractionDigits);
|
|
|
|
NumberFormat::setMinimumFractionDigits(fExportedProperties->minimumFractionDigits);
|
2018-04-14 07:15:19 +00:00
|
|
|
// fProperties, not fExportedProperties, since this information comes from the pattern:
|
|
|
|
NumberFormat::setGroupingUsed(fProperties->groupingUsed);
|
2018-03-13 09:25:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 10:11:36 +00:00
|
|
|
void DecimalFormat::refreshFormatterNoError() {
|
|
|
|
ErrorCode localStatus;
|
|
|
|
refreshFormatter(localStatus);
|
|
|
|
}
|
|
|
|
|
2018-03-13 09:25:41 +00:00
|
|
|
void DecimalFormat::setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
|
|
|
|
UErrorCode& status) {
|
|
|
|
// Cast workaround to get around putting the enum in the public header file
|
|
|
|
auto actualIgnoreRounding = static_cast<IgnoreRounding>(ignoreRounding);
|
2018-03-14 09:15:27 +00:00
|
|
|
PatternParser::parseToExistingProperties(pattern, *fProperties, actualIgnoreRounding, status);
|
2018-03-13 09:25:41 +00:00
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|