Module: Essential Tools Module Group: Traditional Collection Classes
RWDecimalPortable RWDecimalBase
asString() binaryStoreSize() enum RoundingMethod |
hash() isNumber() restoreFrom() |
RWDecimalPortable() saveOn() trimZeros() |
operator!=() operator>>() operator>() operator>=() |
operator<<() operator<() operator<=() operator*() |
operator+() operator-() operator==() round() |
toDouble() |
#include <rw/tools/decport.h> RWDecimalPortable dec; // construct a decimal == 0 dec = "1.23"; // construct a decimal == 1.23
RWDecimalPortable represents an arbitrary precision decimal fraction.
When constructing a RWDecimalPortable from a string, the constructor parses the initial characters of the string until the first non-digit, behaving similarly to C's strtod)( function. For example, a string "99E44"
If an RWDecimalPortable is provided an invalid string, either in the constructor or through assignment, it does not throw an exception. Use isNumber() to determine if the object is valid.
RWDecimalPortable a("555AAA"); a.isValid(); // returns true RWDecimalPortable b; b = "A555AAA"; b.isValid(); // returns false (NAN)
Note that:
The validity or non-validity of the provided string is locale-dependent, as discussed below in the description for the constructor that takes a string.
In the first example above, the likely value of the RWDecimalPortable would be 555. That is, the string parser parses until it encounters an invalid character, and constructs the number from the characters up to that point.
RWDecimalBase contains the following enum, used by the round() method:
enum RoundingMethod { PLAIN = 0, UP = 1, DOWN = 2,
BANKERS = 3, TRUNCATE = 2 };
Rounding methods are defined as follows:
PLAIN: Round up on a tie. For example, 1.25 rounds to 1.3, 1.35 rounds to 1.4, 1.251 becomes 1.3.
UP: Always round up. For example, 1.25 rounds to 1.3, 1.35 rounds to 1.4, and 1.251 becomes 1.3.
DOWN: Always round down. For example, 1.25 rounds to 1.2, 1.35 rounds to 1.3, and 1.251 rounds to 1.2.
BANKERS: On a tie, round so that last digit is even. For example, 1.25 rounds to 1.2, 1.35 rounds to 1.4, and 1.251 (which is not a tie) rounds to 1.3.
TRUNCATE: Same as DOWN.
RWDecimalPortable();
The default constructor creates a value initialized to zero (0).
RWDecimalPortable(const char* input);
Parses the initial portion of input formatted as a decimal number up to the end of the string, or the first non-decimal character, whichever comes first. The parse behavior is locale-dependent (for decimal points, thousands separator, grouping, etc). If no locale is specified, the default locale is used, and parsing behavior will be defined by that locale, creating possibly unexpected results. To ensure predictable and expected behavior, the program should explicitly specify the locale before using this constructor, and input strings should be formatted according the numeric string facets for that locale.
For more information on conversions between strings and numbers, see Section 8.5.4, "Numbers," in the Essential Tools Module User's Guide.
RWDecimalPortable(long intPart);
Constructs a value with intPart as the part before the decimal point.
virtual RWspace binaryStoreSize() const;
Returns the number of bytes required to store self using the saveOn() methods.
friend unsigned hash() const;
Returns a value suitable for hashing.
bool isNumber() const;
Returns false if self represents a NaN, a missing value, or a NULL; otherwise returns true.
NOTE -- Note: In this instance, the mathematical rather than the RDBMS meaning of NULL is intended. Please see the entry for the RWDBNullIndicator class in the SourcePro DB Reference Guide.
RWCString asString(const RWLocale&) const;
Returns self as an RWCString using the provided RWLocale instance for numeric formatting. For example, an English locale could return strings such as -23.21, 0.32, or 432.
RWCString asString() const;
Returns self as an RWCString using the global locale as determined by the static member function RWLocal::global() for numeric formatting. For example, an English locale could return strings such as -23.21, 0.32, or 432.
friend void restoreFrom(RWFile& file);
Reads file, replacing the contents of self.
friend void restoreFrom(RWvistream& vis);
Reads vis, replacing the contents of self.
friend void saveOn(RWFile& file) const;
Writes the contents of self to file.
friend void saveOn(RWvostream& vos) const;
Writes the contents of self to vos.
void trimZeros();
Removes leading zeros before self's decimal point and trailing zeros after.
RWDecimalPortable round(const RWDecimalPortable&, int digits, RoundingMethod = RWDecimalBase::PLAIN);
Returns an RWDecimalPortable which is the result of rounding self according to the applied specifications.
double toDouble(const RWDecimalPortable&);
Returns a double which approximates the value of self.
RWDecimalPortable operator-(const RWDecimalPortable&); RWDecimalPortable operator+(const RWDecimalPortable&, const RWDecimalPortable&); RWDecimalPortable operator-(const RWDecimalPortable&, const RWDecimalPortable&); RWDecimalPortable operator*(const RWDecimalPortable&, const RWDecimalPortable&);
Rudimentary math functions. These are not very efficient. For efficiency, use one of the decimal classes of the Rogue Wave Analysis Product Line's Currency Module.
bool operator==(const RWDecimalPortable& x, const RWDecimalPortable& y); bool operator>=(const RWDecimalPortable& x, const RWDecimalPortable& y); bool operator!=(const RWDecimalPortable& x, const RWDecimalPortable& y); bool operator<(const RWDecimalPortable& x, const RWDecimalPortable& y); bool operator>(const RWDecimalPortable& x, const RWDecimalPortable& y); bool operator<=(const RWDecimalPortable& x, const RWDecimalPortable& y);
Relational operators.
ostream& operator<<(ostream&, const RWDecimalPortable&);
Writes an RWDecimalPortable to an output stream.
ostream& operator>>(istream&, RWDecimalPortable&);
Reads an RWDecimalPortable from an input stream.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.