From: Younies Date: Tue, 11 Feb 2020 13:52:36 +0000 (+0100) Subject: add important functionalities to DecNum X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d3a48f8679441d75a3c469b5fbcad27557d75ede;p=icu add important functionalities to DecNum --- diff --git a/icu4c/source/i18n/number_decnum.h b/icu4c/source/i18n/number_decnum.h index 3a3e92b055b..548f00eafdd 100644 --- a/icu4c/source/i18n/number_decnum.h +++ b/icu4c/source/i18n/number_decnum.h @@ -53,14 +53,20 @@ class U_I18N_API DecNum : public UMemory { void normalize(); + void multiplyBy(double rhs, UErrorCode& status); void multiplyBy(const DecNum& rhs, UErrorCode& status); + void divideBy(double rhs, UErrorCode& status); void divideBy(const DecNum& rhs, UErrorCode& status); + void add(double rhs, UErrorCode& status); void add(const DecNum& rhs, UErrorCode& status); + void subtract(double rhs, UErrorCode& status); void subtract(const DecNum& rhs, UErrorCode& status); + void power(int32_t power); + bool isNegative() const; bool isZero() const; @@ -71,6 +77,8 @@ class U_I18N_API DecNum : public UMemory { bool equalTo(const DecNum& rhs, UErrorCode& status) const; + StringPiece toString(UErrorCode& status) const; + void toString(ByteSink& output, UErrorCode& status) const; inline const decNumber* getRawDecNumber() const { diff --git a/icu4c/source/i18n/number_utils.cpp b/icu4c/source/i18n/number_utils.cpp index b5df94c9436..60836c195a8 100644 --- a/icu4c/source/i18n/number_utils.cpp +++ b/icu4c/source/i18n/number_utils.cpp @@ -254,6 +254,12 @@ void DecNum::normalize() { uprv_decNumberReduce(fData, fData, &fContext); } +void DecNum::multiplyBy(double rhs, UErrorCode& status){ + DecNum rhsDecNum; + rhsDecNum.setTo(rhs, status); + multiplyBy(rhsDecNum, status); +} + void DecNum::multiplyBy(const DecNum& rhs, UErrorCode& status) { uprv_decNumberMultiply(fData, fData, rhs.fData, &fContext); if (fContext.status != 0) { @@ -261,6 +267,12 @@ void DecNum::multiplyBy(const DecNum& rhs, UErrorCode& status) { } } +void DecNum::divideBy(double rhs, UErrorCode& status) { + DecNum rhsDecNum; + rhsDecNum.setTo(rhs, status); + divideBy(rhsDecNum, status); +} + void DecNum::divideBy(const DecNum& rhs, UErrorCode& status) { uprv_decNumberDivide(fData, fData, rhs.fData, &fContext); if ((fContext.status & DEC_Inexact) != 0) { @@ -270,6 +282,12 @@ void DecNum::divideBy(const DecNum& rhs, UErrorCode& status) { } } +void DecNum::add(double rhs, UErrorCode& status) { + DecNum rhsDecNum; + rhsDecNum.setTo(rhs, status); + add(rhsDecNum, status); +} + void DecNum::add(const DecNum& rhs, UErrorCode& status) { uprv_decNumberAdd_66(fData, fData, rhs.fData, &fContext); if (fContext.status != 0) { @@ -277,6 +295,12 @@ void DecNum::add(const DecNum& rhs, UErrorCode& status) { } } +void DecNum::subtract(double rhs, UErrorCode& status) { + DecNum rhsDecNum; + rhsDecNum.setTo(rhs, status); + subtract(rhsDecNum, status); +} + void DecNum::subtract(const DecNum& rhs, UErrorCode& status) { uprv_decNumberSubtract_66(fData, fData, rhs.fData, &fContext); if (fContext.status != 0) { @@ -310,6 +334,14 @@ bool DecNum::equalTo(const DecNum& rhs, UErrorCode& status) const { return false; // TODO(younies): implement this function } +StringPiece DecNum::toString(UErrorCode& status) const { + StringPiece result; + + // TODO(younies): implement + + return result; +} + void DecNum::toString(ByteSink& output, UErrorCode& status) const { if (U_FAILURE(status)) { return;