number_decnum.h (2250B)
1 // © 2017 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 4 #include "unicode/utypes.h" 5 6 #if !UCONFIG_NO_FORMATTING 7 #ifndef __NUMBER_DECNUM_H__ 8 #define __NUMBER_DECNUM_H__ 9 10 #include "decNumber.h" 11 #include "charstr.h" 12 #include "bytesinkutil.h" 13 14 U_NAMESPACE_BEGIN 15 16 #define DECNUM_INITIAL_CAPACITY 34 17 18 namespace number::impl { 19 20 /** A very thin C++ wrapper around decNumber.h */ 21 // Exported as U_I18N_API_CLASS for tests 22 class U_I18N_API_CLASS DecNum : public UMemory { 23 public: 24 U_I18N_API DecNum(); // leaves object in valid but undefined state 25 26 // Copy-like constructor; use the default move operators. 27 DecNum(const DecNum& other, UErrorCode& status); 28 29 /** Sets the decNumber to the StringPiece. */ 30 void setTo(StringPiece str, UErrorCode& status); 31 32 /** Sets the decNumber to the NUL-terminated char string. */ 33 void setTo(const char* str, UErrorCode& status); 34 35 /** Uses double_conversion to set this decNumber to the given double. */ 36 void setTo(double d, UErrorCode& status); 37 38 /** Sets the decNumber to the BCD representation. */ 39 void setTo(const uint8_t* bcd, int32_t length, int32_t scale, bool isNegative, UErrorCode& status); 40 41 void normalize(); 42 43 void multiplyBy(const DecNum& rhs, UErrorCode& status); 44 45 void divideBy(const DecNum& rhs, UErrorCode& status); 46 47 bool isNegative() const; 48 49 bool isZero() const; 50 51 /** Is infinity or NaN */ 52 bool isSpecial() const; 53 54 bool isInfinity() const; 55 56 bool isNaN() const; 57 58 void toString(ByteSink& output, UErrorCode& status) const; 59 60 inline CharString toCharString(UErrorCode& status) const { 61 CharString cstr; 62 CharStringByteSink sink(&cstr); 63 toString(sink, status); 64 return cstr; 65 } 66 67 inline const decNumber* getRawDecNumber() const { 68 return fData.getAlias(); 69 } 70 71 private: 72 static constexpr int32_t kDefaultDigits = DECNUM_INITIAL_CAPACITY; 73 MaybeStackHeaderAndArray<decNumber, char, kDefaultDigits> fData; 74 decContext fContext; 75 76 void _setTo(const char* str, int32_t maxDigits, UErrorCode& status); 77 }; 78 79 } // namespace number::impl 80 81 U_NAMESPACE_END 82 83 #endif // __NUMBER_DECNUM_H__ 84 85 #endif /* #if !UCONFIG_NO_FORMATTING */