Currency
Currency conversions are trickier than number conversions, mainly because there is no standard way to represent monetary values in a computer. We have adopted the convention that such values represent an integral number of the smallest unit of currency in use. For example, to represent a balance of $10.00 in the United States, you could say:
double sawbuck = 1000.;
This representation has the advantages of wide range, exactness, and portability. Wide range means you can exactly represent values from $0.00 up to and beyond $10,000,000,000,000.00 larger than any likely budget. Exactness means that, representing monetary values without fractional parts, you can perform arithmetic on them and compare the results for equality:
double price = 999.; // $9.99
double penny = 1.; // $.01
assert(price + penny == sawbuck);
This would not be possible if the values were naively represented, as in price = 9.99;.
Portability means simply that
double is a standard type, unlike common 64-bit integer or BCD representations. Of course, you can perform financial calculations on such other representations, but because you can always convert between them and
double, they are all supported. In the future,
RWLocale may directly support some other common representations as well.
Consider the following examples of currency conversions:
const RWLocale& here = RWLocale::global();
double sawbuck = 1000.;
RWCString tenNone = here.moneyAsString(sawbuck, RWLocale::NONE);
RWCString tenLocal = here.moneyAsString(sawbuck,RWLocale::LOCAL);
RWCString tenIntl = here.moneyAsString(sawbuck, RWLocale::INTL);
if (here.stringToMoney(tenNone, &sawbuck) &&
here.stringToMoney(tenLocal, &sawbuck) &&
here.stringToMoney(tenIntl, &sawbuck)) // verify conversion
cout << sawbuck << " " << tenNone << " "
<< tenLocal << " " << tenIntl << " " << endl;
In a United States locale, the code would display as:
1000.00000 10.00 $10.00 USD 10.00