Currency Module User’s Guide : Chapter 6 Decimal Classes : Conversions
Conversions
Constructors and conversion functions provide conversion from and to various types. The most common conversions are from an int, a string, or a double to an RWDecimal<T>, or from an RWDecimal<T> to an int, a double, or a string. Conversions between RWDecimal<T> types are also provided.
Conversion from Integers and Strings
Conversions to RWDecimal<T> objects are provided by the constructors
 
RWDecimal<T>(long int);
RWDecimal<T>(const char*);
These functions produce an exact representation of the argument. The string constructor accepts strings with embedded commas and a leading dollar sign. Negative numbers may be indicated either with a minus sign or by parentheses. A technical description of the exact grammar allowed is given in the "Technical Notes" section of this manual.
Conversion from Built-In Floating Point
The Currency Module does not automatically provide conversion from a built-in floating point number to an RWDecimal<T> because such conversions often cause round-off errors. This situation occurs frequently with constants. For example, the following code
 
RWDecimal<RWMP2Int> x = 1.01;
will not produce an exact representation of 1.01, since the compiler interprets the constant as a floating point literal. To set x to an exact representation of 1.01, you can use a string or the form of the constructor that takes an integer and a number of decimal places. For example:
 
RWDecimal<RWMP2Int> x = "1.01"
or
 
RWDecimal<RWMP2Int> x(101,2);
If you do want to convert from a floating point number, use the global function.
 
RWDecimal<T> from(long double)
This will return a decimal object approximately equal to the parameter.
Conversion to Built-In Types
Conversions from RWDecimal objects are provided by the functions:
 
RWCString toString(const RWDecimal<T>&)
long double toDouble(const RWDecimal<T>&)
long int toInt(const RWDecimal<T>&)
Conversion to a string is exact but, conversion to a long double will likely involve rounding. Conversion to a long int will only produce a meaningful result if the number will fit in an integer.