本文整理汇总了C++中DigitList类的典型用法代码示例。如果您正苦于以下问题:C++ DigitList类的具体用法?C++ DigitList怎么用?C++ DigitList使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DigitList类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: initVisibleDigits
VisibleDigits &
FixedPrecision::initVisibleDigits(
int64_t value,
VisibleDigits &digits,
UErrorCode &status) const {
if (U_FAILURE(status)) {
return digits;
}
if (!fRoundingIncrement.isZero()) {
// If we have round increment, use digit list.
DigitList digitList;
digitList.set(value);
return initVisibleDigits(digitList, digits, status);
}
// Try fast path
if (initVisibleDigits(value, 0, digits, status)) {
digits.fAbsDoubleValue = fabs((double) value);
digits.fAbsDoubleValueSet = U_SUCCESS(status) && !digits.isOverMaxDigits();
return digits;
}
// Oops have to use digit list
DigitList digitList;
digitList.set(value);
return initVisibleDigits(digitList, digits, status);
}
示例2: dispose
// ---------------------------------------
void
Formattable::setDecimalNumber(StringPiece numberString, UErrorCode &status) {
if (U_FAILURE(status)) {
return;
}
dispose();
// Copy the input string and nul-terminate it.
// The decNumber library requires nul-terminated input. StringPiece input
// is not guaranteed nul-terminated. Too bad.
// CharString automatically adds the nul.
DigitList *dnum = new DigitList(); // TODO: use getInternalDigitList
if (dnum == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
dnum->set(CharString(numberString, status).toStringPiece(), status);
if (U_FAILURE(status)) {
delete dnum;
return; // String didn't contain a decimal number.
}
adoptDigitList(dnum);
// Note that we do not hang on to the caller's input string.
// If we are asked for the string, we will regenerate one from fDecimalNum.
}
示例3: if
/**
* Currently, getDouble() depends on strtod() to do its conversion.
*
* WARNING!!
* This is an extremely costly function. ~1/2 of the conversion time
* can be linked to this function.
*/
double
DigitList::getDouble() const
{
{
Mutex mutex;
if (fHave == kDouble) {
return fUnion.fDouble;
}
}
double tDouble = 0.0;
if (isZero()) {
tDouble = 0.0;
if (decNumberIsNegative(fDecNumber)) {
tDouble /= -1;
}
} else if (isInfinite()) {
if (std::numeric_limits<double>::has_infinity) {
tDouble = std::numeric_limits<double>::infinity();
} else {
tDouble = std::numeric_limits<double>::max();
}
if (!isPositive()) {
tDouble = -tDouble; //this was incorrectly "-fDouble" originally.
}
} else {
MaybeStackArray<char, MAX_DBL_DIGITS+18> s;
// Note: 14 is a magic constant from the decNumber library documentation,
// the max number of extra characters beyond the number of digits
// needed to represent the number in string form. Add a few more
// for the additional digits we retain.
// Round down to appx. double precision, if the number is longer than that.
// Copy the number first, so that we don't modify the original.
if (getCount() > MAX_DBL_DIGITS + 3) {
DigitList numToConvert(*this);
numToConvert.reduce(); // Removes any trailing zeros, so that digit count is good.
numToConvert.round(MAX_DBL_DIGITS+3);
uprv_decNumberToString(numToConvert.fDecNumber, s.getAlias());
// TODO: how many extra digits should be included for an accurate conversion?
} else {
uprv_decNumberToString(this->fDecNumber, s.getAlias());
}
U_ASSERT(uprv_strlen(&s[0]) < MAX_DBL_DIGITS+18);
char *end = NULL;
tDouble = decimalStrToDouble(s.getAlias(), &end);
}
{
Mutex mutex;
DigitList *nonConstThis = const_cast<DigitList *>(this);
nonConstThis->internalSetDouble(tDouble);
}
return tDouble;
}
示例4: reduce
void
DigitList::mult(const DigitList &other, UErrorCode &status) {
fContext.status = 0;
int32_t requiredDigits = this->digits() + other.digits();
if (requiredDigits > fContext.digits) {
reduce(); // Remove any trailing zeros
int32_t requiredDigits = this->digits() + other.digits();
ensureCapacity(requiredDigits, status);
}
uprv_decNumberMultiply(fDecNumber, fDecNumber, other.fDecNumber, &fContext);
fHaveDouble = FALSE;
}
示例5: initVisibleDigitsWithExponent
VisibleDigitsWithExponent &
ScientificPrecision::initVisibleDigitsWithExponent(
int64_t value,
VisibleDigitsWithExponent &digits,
UErrorCode &status) const {
if (U_FAILURE(status)) {
return digits;
}
DigitList digitList;
digitList.set(value);
return initVisibleDigitsWithExponent(digitList, digits, status);
}
示例6: errln
void PluralRulesTest::checkSelect(const LocalPointer<PluralRules> &rules, UErrorCode &status,
int32_t line, const char *keyword, ...) {
// The varargs parameters are a const char* strings, each being a decimal number.
// The formatting of the numbers as strings is significant, e.g.
// the difference between "2" and "2.0" can affect which rule matches (which keyword is selected).
// Note: rules parameter is a LocalPointer reference rather than a PluralRules * to avoid having
// to write getAlias() at every (numerous) call site.
if (U_FAILURE(status)) {
errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
status = U_ZERO_ERROR;
return;
}
if (rules == NULL) {
errln("file %s, line %d: rules pointer is NULL", __FILE__, line);
return;
}
va_list ap;
va_start(ap, keyword);
for (;;) {
const char *num = va_arg(ap, const char *);
if (strcmp(num, END_MARK) == 0) {
break;
}
// DigitList is a convenient way to parse the decimal number string and get a double.
DigitList dl;
dl.set(StringPiece(num), status);
if (U_FAILURE(status)) {
errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
status = U_ZERO_ERROR;
continue;
}
double numDbl = dl.getDouble();
const char *decimalPoint = strchr(num, '.');
int fractionDigitCount = decimalPoint == NULL ? 0 : (num + strlen(num) - 1) - decimalPoint;
int fractionDigits = fractionDigitCount == 0 ? 0 : atoi(decimalPoint + 1);
FixedDecimal ni(numDbl, fractionDigitCount, fractionDigits);
UnicodeString actualKeyword = rules->select(ni);
if (actualKeyword != UnicodeString(keyword)) {
errln("file %s, line %d, select(%s) returned incorrect keyword. Expected %s, got %s",
__FILE__, line, num, keyword, US(actualKeyword).cstr());
}
}
va_end(ap);
}
示例7: reduce
/*
* Multiply
* The number will be expanded if need be to retain full precision.
* In practice, for formatting, multiply is by 10, 100 or 1000, so more digits
* will not be required for this use.
*/
void
DigitList::mult(const DigitList &other, UErrorCode &status) {
if (U_FAILURE(status)) {
return;
}
fContext.status = 0;
int32_t requiredDigits = this->digits() + other.digits();
if (requiredDigits > fContext.digits) {
reduce(); // Remove any trailing zeros
int32_t requiredDigits = this->digits() + other.digits();
ensureCapacity(requiredDigits, status);
}
uprv_decNumberMultiply(fDecNumber, fDecNumber, other.fDecNumber, &fContext);
internalClear();
}
示例8:
UBool
FixedPrecision::handleNonNumeric(DigitList &value, VisibleDigits &digits) {
if (value.isNaN()) {
digits.setNaN();
return TRUE;
}
if (value.isInfinite()) {
digits.setInfinite();
if (!value.isPositive()) {
digits.setNegative();
}
return TRUE;
}
return FALSE;
}
示例9: getPos
/**
* If in "by digits" mode, fills in the substitution one decimal digit
* at a time using the rule set containing this substitution.
* Otherwise, uses the superclass function.
* @param number The number being formatted
* @param toInsertInto The string to insert the result of formatting
* the substitution into
* @param pos The position of the owning rule's rule text in
* toInsertInto
*/
void
FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const
{
// if we're not in "byDigits" mode, just use the inherited
// doSubstitution() routine
if (!byDigits) {
NFSubstitution::doSubstitution(number, toInsertInto, _pos);
// if we're in "byDigits" mode, transform the value into an integer
// by moving the decimal point eight places to the right and
// pulling digits off the right one at a time, formatting each digit
// as an integer using this substitution's owning rule set
// (this is slower, but more accurate, than doing it from the
// other end)
} else {
// int32_t numberToFormat = (int32_t)uprv_round(transformNumber(number) * uprv_pow(10, kMaxDecimalDigits));
// // this flag keeps us from formatting trailing zeros. It starts
// // out false because we're pulling from the right, and switches
// // to true the first time we encounter a non-zero digit
// UBool doZeros = FALSE;
// for (int32_t i = 0; i < kMaxDecimalDigits; i++) {
// int64_t digit = numberToFormat % 10;
// if (digit != 0 || doZeros) {
// if (doZeros && useSpaces) {
// toInsertInto.insert(_pos + getPos(), gSpace);
// }
// doZeros = TRUE;
// getRuleSet()->format(digit, toInsertInto, _pos + getPos());
// }
// numberToFormat /= 10;
// }
DigitList dl;
dl.set(number);
dl.roundFixedPoint(20); // round to 20 fraction digits.
dl.reduce(); // Removes any trailing zeros.
UBool pad = FALSE;
for (int32_t didx = dl.getCount()-1; didx>=dl.getDecimalAt(); didx--) {
// Loop iterates over fraction digits, starting with the LSD.
// include both real digits from the number, and zeros
// to the left of the MSD but to the right of the decimal point.
if (pad && useSpaces) {
toInsertInto.insert(_pos + getPos(), gSpace);
} else {
pad = TRUE;
}
int64_t digit = didx>=0 ? dl.getDigit(didx) - '0' : 0;
getRuleSet()->format(digit, toInsertInto, _pos + getPos());
}
if (!pad) {
// hack around lack of precision in digitlist. if we would end up with
// "foo point" make sure we add a " zero" to the end.
getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos());
}
}
}
示例10: DecimalFormatPattern
void
DecimalFormatPatternParser::applyPatternWithoutExpandAffix(
const UnicodeString& pattern,
DecimalFormatPattern& out,
UParseError& parseError,
UErrorCode& status) {
if (U_FAILURE(status))
{
return;
}
out = DecimalFormatPattern();
// Clear error struct
parseError.offset = -1;
parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
// TODO: Travis Keep: This won't always work.
UChar nineDigit = (UChar)(fZeroDigit + 9);
int32_t digitLen = fDigit.length();
int32_t groupSepLen = fGroupingSeparator.length();
int32_t decimalSepLen = fDecimalSeparator.length();
int32_t pos = 0;
int32_t patLen = pattern.length();
// Part 0 is the positive pattern. Part 1, if present, is the negative
// pattern.
for (int32_t part=0; part<2 && pos<patLen; ++part) {
// The subpart ranges from 0 to 4: 0=pattern proper, 1=prefix,
// 2=suffix, 3=prefix in quote, 4=suffix in quote. Subpart 0 is
// between the prefix and suffix, and consists of pattern
// characters. In the prefix and suffix, percent, perMill, and
// currency symbols are recognized and translated.
int32_t subpart = 1, sub0Start = 0, sub0Limit = 0, sub2Limit = 0;
// It's important that we don't change any fields of this object
// prematurely. We set the following variables for the multiplier,
// grouping, etc., and then only change the actual object fields if
// everything parses correctly. This also lets us register
// the data from part 0 and ignore the part 1, except for the
// prefix and suffix.
UnicodeString prefix;
UnicodeString suffix;
int32_t decimalPos = -1;
int32_t multiplier = 1;
int32_t digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0, sigDigitCount = 0;
int8_t groupingCount = -1;
int8_t groupingCount2 = -1;
int32_t padPos = -1;
UChar32 padChar = 0;
int32_t roundingPos = -1;
DigitList roundingInc;
int8_t expDigits = -1;
UBool expSignAlways = FALSE;
// The affix is either the prefix or the suffix.
UnicodeString* affix = &prefix;
int32_t start = pos;
UBool isPartDone = FALSE;
UChar32 ch;
for (; !isPartDone && pos < patLen; ) {
// Todo: account for surrogate pairs
ch = pattern.char32At(pos);
switch (subpart) {
case 0: // Pattern proper subpart (between prefix & suffix)
// Process the digits, decimal, and grouping characters. We
// record five pieces of information. We expect the digits
// to occur in the pattern ####00.00####, and we record the
// number of left digits, zero (central) digits, and right
// digits. The position of the last grouping character is
// recorded (should be somewhere within the first two blocks
// of characters), as is the position of the decimal point,
// if any (should be in the zero digits). If there is no
// decimal point, then there should be no right digits.
if (pattern.compare(pos, digitLen, fDigit) == 0) {
if (zeroDigitCount > 0 || sigDigitCount > 0) {
++digitRightCount;
} else {
++digitLeftCount;
}
if (groupingCount >= 0 && decimalPos < 0) {
++groupingCount;
}
pos += digitLen;
} else if ((ch >= fZeroDigit && ch <= nineDigit) ||
ch == fSigDigit) {
if (digitRightCount > 0) {
// Unexpected '0'
debug("Unexpected '0'")
status = U_UNEXPECTED_TOKEN;
syntaxError(pattern,pos,parseError);
return;
}
if (ch == fSigDigit) {
++sigDigitCount;
} else {
if (ch != fZeroDigit && roundingPos < 0) {
roundingPos = digitLeftCount + zeroDigitCount;
}
//.........这里部分代码省略.........
示例11: if
/**
* Currently, getDouble() depends on strtod() to do its conversion.
*
* WARNING!!
* This is an extremely costly function. ~1/2 of the conversion time
* can be linked to this function.
*/
double
DigitList::getDouble() const
{
static char gDecimal = 0;
char decimalSeparator;
{
Mutex mutex;
if (fHave == kDouble) {
return fUnion.fDouble;
} else if(fHave == kInt64) {
return (double)fUnion.fInt64;
}
decimalSeparator = gDecimal;
}
if (decimalSeparator == 0) {
// We need to know the decimal separator character that will be used with strtod().
// Depends on the C runtime global locale.
// Most commonly is '.'
// TODO: caching could fail if the global locale is changed on the fly.
char rep[MAX_DIGITS];
sprintf(rep, "%+1.1f", 1.0);
decimalSeparator = rep[2];
}
double tDouble = 0.0;
if (isZero()) {
tDouble = 0.0;
if (decNumberIsNegative(fDecNumber)) {
tDouble /= -1;
}
} else if (isInfinite()) {
if (std::numeric_limits<double>::has_infinity) {
tDouble = std::numeric_limits<double>::infinity();
} else {
tDouble = std::numeric_limits<double>::max();
}
if (!isPositive()) {
tDouble = -tDouble; //this was incorrectly "-fDouble" originally.
}
} else {
MaybeStackArray<char, MAX_DBL_DIGITS+18> s;
// Note: 14 is a magic constant from the decNumber library documentation,
// the max number of extra characters beyond the number of digits
// needed to represent the number in string form. Add a few more
// for the additional digits we retain.
// Round down to appx. double precision, if the number is longer than that.
// Copy the number first, so that we don't modify the original.
if (getCount() > MAX_DBL_DIGITS + 3) {
DigitList numToConvert(*this);
numToConvert.reduce(); // Removes any trailing zeros, so that digit count is good.
numToConvert.round(MAX_DBL_DIGITS+3);
uprv_decNumberToString(numToConvert.fDecNumber, s);
// TODO: how many extra digits should be included for an accurate conversion?
} else {
uprv_decNumberToString(this->fDecNumber, s);
}
U_ASSERT(uprv_strlen(&s[0]) < MAX_DBL_DIGITS+18);
if (decimalSeparator != '.') {
char *decimalPt = strchr(s, '.');
if (decimalPt != NULL) {
*decimalPt = decimalSeparator;
}
}
char *end = NULL;
tDouble = uprv_strtod(s, &end);
}
{
Mutex mutex;
DigitList *nonConstThis = const_cast<DigitList *>(this);
nonConstThis->internalSetDouble(tDouble);
gDecimal = decimalSeparator;
}
return tDouble;
}
示例12: getMultiplier
int32_t
ScientificPrecision::toScientific(DigitList &value) const {
return value.toScientific(
fMantissa.fMin.getIntDigitCount(), getMultiplier());
}
示例13: workText
UBool
FractionalPartSubstitution::doParse(const UnicodeString& text,
ParsePosition& parsePosition,
double baseValue,
double /*upperBound*/,
UBool lenientParse,
Formattable& resVal) const
{
// if we're not in byDigits mode, we can just use the inherited
// doParse()
if (!byDigits) {
return NFSubstitution::doParse(text, parsePosition, baseValue, 0, lenientParse, resVal);
// if we ARE in byDigits mode, parse the text one digit at a time
// using this substitution's owning rule set (we do this by setting
// upperBound to 10 when calling doParse() ) until we reach
// nonmatching text
} else {
UnicodeString workText(text);
ParsePosition workPos(1);
double result = 0;
int32_t digit;
// double p10 = 0.1;
DigitList dl;
NumberFormat* fmt = NULL;
while (workText.length() > 0 && workPos.getIndex() != 0) {
workPos.setIndex(0);
Formattable temp;
getRuleSet()->parse(workText, workPos, 10, temp);
UErrorCode status = U_ZERO_ERROR;
digit = temp.getLong(status);
// digit = temp.getType() == Formattable::kLong ?
// temp.getLong() :
// (int32_t)temp.getDouble();
if (lenientParse && workPos.getIndex() == 0) {
if (!fmt) {
status = U_ZERO_ERROR;
fmt = NumberFormat::createInstance(status);
if (U_FAILURE(status)) {
delete fmt;
fmt = NULL;
}
}
if (fmt) {
fmt->parse(workText, temp, workPos);
digit = temp.getLong(status);
}
}
if (workPos.getIndex() != 0) {
dl.append((char)('0' + digit));
// result += digit * p10;
// p10 /= 10;
parsePosition.setIndex(parsePosition.getIndex() + workPos.getIndex());
workText.removeBetween(0, workPos.getIndex());
while (workText.length() > 0 && workText.charAt(0) == gSpace) {
workText.removeBetween(0, 1);
parsePosition.setIndex(parsePosition.getIndex() + 1);
}
}
}
delete fmt;
result = dl.fCount == 0 ? 0 : dl.getDouble();
result = composeRuleValue(result, baseValue);
resVal.setDouble(result);
return TRUE;
}
}
示例14: while
/**
* If in "by digits" mode, fills in the substitution one decimal digit
* at a time using the rule set containing this substitution.
* Otherwise, uses the superclass function.
* @param number The number being formatted
* @param toInsertInto The string to insert the result of formatting
* the substitution into
* @param pos The position of the owning rule's rule text in
* toInsertInto
*/
void
FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const
{
// if we're not in "byDigits" mode, just use the inherited
// doSubstitution() routine
if (!byDigits) {
NFSubstitution::doSubstitution(number, toInsertInto, _pos);
// if we're in "byDigits" mode, transform the value into an integer
// by moving the decimal point eight places to the right and
// pulling digits off the right one at a time, formatting each digit
// as an integer using this substitution's owning rule set
// (this is slower, but more accurate, than doing it from the
// other end)
} else {
// int32_t numberToFormat = (int32_t)uprv_round(transformNumber(number) * uprv_pow(10, kMaxDecimalDigits));
// // this flag keeps us from formatting trailing zeros. It starts
// // out false because we're pulling from the right, and switches
// // to true the first time we encounter a non-zero digit
// UBool doZeros = FALSE;
// for (int32_t i = 0; i < kMaxDecimalDigits; i++) {
// int64_t digit = numberToFormat % 10;
// if (digit != 0 || doZeros) {
// if (doZeros && useSpaces) {
// toInsertInto.insert(_pos + getPos(), gSpace);
// }
// doZeros = TRUE;
// getRuleSet()->format(digit, toInsertInto, _pos + getPos());
// }
// numberToFormat /= 10;
// }
DigitList dl;
dl.set(number, 20, TRUE);
UBool pad = FALSE;
while (dl.fCount > (dl.fDecimalAt <= 0 ? 0 : dl.fDecimalAt)) {
if (pad && useSpaces) {
toInsertInto.insert(_pos + getPos(), gSpace);
} else {
pad = TRUE;
}
getRuleSet()->format((int64_t)(dl.fDigits[--dl.fCount] - '0'), toInsertInto, _pos + getPos());
}
while (dl.fDecimalAt < 0) {
if (pad && useSpaces) {
toInsertInto.insert(_pos + getPos(), gSpace);
} else {
pad = TRUE;
}
getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos());
++dl.fDecimalAt;
}
if (!pad) {
// hack around lack of precision in digitlist. if we would end up with
// "foo point" make sure we add a " zero" to the end.
getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos());
}
}
}