beautypg.com

Numbers, Currency – HP Integrity NonStop J-Series User Manual

Page 243

background image

the hour and once to extract the minute. This is an expensive operation. If you expect to work often
with the components of a time or date, you may be better off disassembling the time only once:

RWTime now = RWTime::now();
struct tm tmbuf;
now.extract(&tmbuf);
const RWLocale& here = RWLocale::global(); // the default
// global locale
cout << here.asString(&tmbuf, 'H') << ":"
<< here.asString(&tmbuf, 'M'); << endl;

Please note that if you work with years before 1901 or after 2037, you can't use

RWTime

because it

does not have the required range.

[30]

You can use

RWLocale

to perform conversions for any time or

date because struct tm operations are not so restricted.

Numbers

RWLocale

also provides you with an interface for conversions between strings and numbers, both

integers and floating point values.

RWLocaleSnapshot

implements this interface, providing the full

range of capabilities defined by the Standard C Library type struct lconv. The capabilities include
using appropriate digit group separators, decimal "point", and currency notation. When converting
from strings, RWLocaleSnapshot allows and checks the same digit group separators.

Unfortunately, stream operations of this class are clumsier than we might like, since the standard
iostream library provides definitions for number insertion and extraction operators which cannot be
overridden. Instead, we can use

RWCString

functions directly:

RWLocaleSnapshot french("fr");
double f = 1234567.89;
long i = 987654;
RWCString fs = french.asString(f, 2);
RWCString is = french.asString(i);
if (french.stringToNum(fs, &f) &&
french.stringToNum(is, &i)) // verify conversion
cout << "C:\t" << f << "\t" << i << endl
<< "French:\t" << fs << "\t" << is << endl;

Since the French use periods for digit group separators, and commas to separate the integer from the
fraction part of a number, this code might display as:

C: 1.234567e+07 987654
French: 1.234.567,89 987.654

You will notice that numbers with digit group separators are easier to read.

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

This manual is related to the following products: