Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

6.7 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.

6.7.1 Conversion from Integers and Strings

Conversions to RWDecimal<T> objects are provided by the constructors

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.

6.7.2 Conversion from Built-In Floating Point

Money.h++ does not automatically provide conversion from a built-in floating point number to an RWDecimal<T> because such conversions often cause round-off error. This situation occurs frequently with constants. For example, the following code

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:

or

If you do want to convert from a floating point number, use the global function

This will return a decimal object approximately equal to the parameter.

6.7.3 Conversion to Built-In Types

Conversions from RWDecimal objects are provided by the functions:

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.



Previous fileTop of DocumentContentsIndexNext file

©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.