}
// Set the value into the Union of simple type values.
- // Cannot use the set() functions because they would delete the fDecimalNum value,
- // TODO: fDecimalQuantity->fitsInInt() to kLong type.
- /*
- if (fDecimalQuantity->fitsInInt()) {
- fType = kLong;
- fValue.fInt64 = fDecimalNum->getLong();
- } else
- */
+ // Cannot use the set() functions because they would delete the fDecimalNum value.
if (fDecimalQuantity->fitsInLong()) {
- fType = kInt64;
fValue.fInt64 = fDecimalQuantity->toLong();
+ if (fValue.fInt64 <= INT32_MAX && fValue.fInt64 >= INT32_MIN) {
+ fType = kLong;
+ } else {
+ fType = kInt64;
+ }
} else {
fType = kDouble;
fValue.fDouble = fDecimalQuantity->toDouble();
setToDouble(temp);
}
+void DecimalQuantity::negate() {
+ flags ^= NEGATIVE_FLAG;
+}
+
int32_t DecimalQuantity::getMagnitude() const {
U_ASSERT(precision != 0);
return scale + precision - 1;
*/
void multiplyBy(int32_t multiplicand);
+ /** Flips the sign from positive to negative and back. C++-only: not currently needed in Java. */
+ void negate();
+
/**
* Scales the number by a power of ten. For example, if the value is currently "1234.56", calling
* this method with delta=-3 will change the value to "1.23456".
// All other numbers
LocalPointer<DecimalQuantity> actualQuantity(new DecimalQuantity(quantity));
if (0 != (flags & FLAG_NEGATIVE)) {
- actualQuantity->multiplyBy(-1);
+ actualQuantity->negate();
}
output.adoptDecimalQuantity(actualQuantity.orphan());
}
}
void DecimalQuantityTest::testToDouble() {
+ IcuTestErrorCode status(*this, "testToDouble");
static const struct TestCase {
const char* input; // char* for the decNumber constructor
double expected;
{ "-3.142E-271", -3.142e-271 } };
for (auto& cas : cases) {
+ status.setScope(cas.input);
DecimalQuantity q;
- q.setToDecNumber({cas.input, -1});
+ q.setToDecNumber({cas.input, -1}, status);
double actual = q.toDouble();
assertEquals("Doubles should exactly equal", cas.expected, actual);
}
#endif
}
else {
- errln((UnicodeString)"FAIL: Non-numeric Formattable returned");
+ errln(UnicodeString("FAIL: Non-numeric Formattable returned: ") + pattern + " " + s);
continue;
}
if (pos.getIndex() == s.length())
(uprv_fabs(a - valParse[v+ival]) / a > (2*DBL_EPSILON))) ||
(!useEpsilon && a != valParse[v+ival]))
{
- errln((UnicodeString)"FAIL: Expected " + valParse[v+ival]);
+ errln((UnicodeString)"FAIL: Expected " + valParse[v+ival] + " but got " + a
+ + " on input " + s);
}
}
else {
{
logln((UnicodeString)" -parse-> " + a);
if (a != lvalParse[v+ilval])
- errln((UnicodeString)"FAIL: Expected " + lvalParse[v+ilval]);
+ errln((UnicodeString)"FAIL: Expected " + lvalParse[v+ilval] + " but got " + a);
}
else
errln((UnicodeString)"FAIL: Partial parse (" + pos.getIndex() + " chars) -> " + a);