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 
13 U_NAMESPACE_BEGIN
14 
15 #define DECNUM_INITIAL_CAPACITY 34
16 
17 // Export an explicit template instantiation of the MaybeStackHeaderAndArray that is used as a data member of DecNum.
18 // When building DLLs for Windows this is required even though no direct access to the MaybeStackHeaderAndArray leaks out of the i18n library.
19 // (See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.)
20 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
21 template class U_I18N_API MaybeStackHeaderAndArray<decNumber, char, DECNUM_INITIAL_CAPACITY>;
22 #endif
23 
24 namespace number {
25 namespace impl {
26 
27 /** A very thin C++ wrapper around decNumber.h */
28 // Exported as U_I18N_API for tests
29 class U_I18N_API DecNum : public UMemory {
30   public:
31     DecNum();  // leaves object in valid but undefined state
32 
33     // Copy-like constructor; use the default move operators.
34     DecNum(const DecNum& other, UErrorCode& status);
35 
36     /** Sets the decNumber to the StringPiece. */
37     void setTo(StringPiece str, UErrorCode& status);
38 
39     /** Sets the decNumber to the NUL-terminated char string. */
40     void setTo(const char* str, UErrorCode& status);
41 
42     /** Uses double_conversion to set this decNumber to the given double. */
43     void setTo(double d, UErrorCode& status);
44 
45     /** Sets the decNumber to the BCD representation. */
46     void setTo(const uint8_t* bcd, int32_t length, int32_t scale, bool isNegative, UErrorCode& status);
47 
48     void normalize();
49 
50     void multiplyBy(const DecNum& rhs, UErrorCode& status);
51 
52     void divideBy(const DecNum& rhs, UErrorCode& status);
53 
54     bool isNegative() const;
55 
56     bool isZero() const;
57 
58     void toString(ByteSink& output, UErrorCode& status) const;
59 
getRawDecNumber()60     inline const decNumber* getRawDecNumber() const {
61         return fData.getAlias();
62     }
63 
64   private:
65     static constexpr int32_t kDefaultDigits = DECNUM_INITIAL_CAPACITY;
66     MaybeStackHeaderAndArray<decNumber, char, kDefaultDigits> fData;
67     decContext fContext;
68 
69     void _setTo(const char* str, int32_t maxDigits, UErrorCode& status);
70 };
71 
72 } // namespace impl
73 } // namespace number
74 
75 U_NAMESPACE_END
76 
77 #endif // __NUMBER_DECNUM_H__
78 
79 #endif /* #if !UCONFIG_NO_FORMATTING */
80