Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

RWDecimalFormat



Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <rw/decio.h>
main(){cout << RWDecimalFormat("$_____.__")("2.243") << endl;}

Related Java Class

com.roguewave.money.currency.v1_0.MoneyFormat

Description

The RWDecimalFormat class encapsulates formatting information for converting a decimal number to a string. You can construct and modify the attributes of a formatted object in two ways:

  1. Specify a picture string. The string gives a textual representation of the look of the format. A sample picture is "$0___.__". Using this sample picture, the number 3.14159 is formatted as "$0003.14".

  2. Set attributes directly, using member functions. This allows maximum flexibility.

You can also use a hybrid approach by initially constructing an RWDecimalFormat object using a picture string and then "fine tuning" the formatting parameters using the member functions to change attributes directly.

Once a formatted object is constructed, you use the function call operator() to convert from decimal numbers to strings.

Example

Program output

Attributes

The attributes used by the formatting object are shown in the following table. Two sets of attributes are maintained by the RWDecimalFormat class; one for formatting positive numbers and one for formatting negative numbers. Member functions that set an attribute have names like setAttribute; member functions that obtain the current value of an attribute have names like attribute(). Each of these functions take a parameter indicating whether the attribute for formatting positive values, negative values, or both is to be operated on:

Table 3 -- Attributes

AttributeDescriptiondefault 
allDecimals
If FALSE, the number of decimal places printed is given by the decimalPlaces attribute, otherwise all digits are printed.
FALSE
decimalPlaces
If allDecimals is FALSE, this is the number of decimal places to print. Negative numbers are allowed.
0
decimalSeparator
The decimal point symbol.
"."
doRounding
If TRUE, round the number before display.
FALSE
farLeftText
Text on the extreme left.
""
farRightText
Text on the extreme right.
""
fixedWidth
If FALSE, the natural width of the number is used; if TRUE, the width is fixed to the value of the width attribute
FALSE
justification
How to justify the number in the width provided (LEFT, RIGHT, or CENTER are allowed).
LEFT
leftFillChar
Fill space to left of number with this character.
' '
leftDigitSeparator
String that separates digits left of decimal point into groups.
""
leftGroupSize
Number of digits per group left of the decimal point
3
nearLeftText
String which goes just to the left of the number. Most often used for printing a currency symbol.
""
nearRightText
String which goes just to the right of the number. Most often used for printing a currency symbol.
""
rightDigitSeparator
String which separates digits right of decimal point into groups.
""
rightFillChar
Fill space to right of number with this character.
' '
rightGroupSize
Number of digits per group right of the decimal point
3
roundMethod
The rounding mode used to truncate decimal places (PLAIN, LEFT, RIGHT, BANKERS, TRUNCATE are allowed).
PLAIN
roundPlaces
Number of decimal places to round, if doRounding attribute is TRUE. Negative numbers are allowed. If the allDecimals attribute is FALSE, and the decimalPlaces attribute is less than roundPlaces, then the number is rounded to the number of digits indicated by decimalPlaces.
0
showDecimalPoint
If TRUE, the decimal point is printed when exactly zero decimal places are displayed.
FALSE
width
If the fixedWidth attribute is TRUE, this determines the width of the formatted number.
0

Picture Strings

A picture string uses text characters and their relative positions to indicate how to set formatting attributes. The width attribute is set to the length of the picture string. Other attributes are changed depending on the characters in the picture. The table below lists valid formatting characters. Most formatting characters can be placed in one of four positions:

Table 4 -- Valid formatting characters

CharAffectIllustrationExample numberFormatted number 
@
Indicates start of picture format string. Everything before the @ is taken as leading text.
Num=@____
4.32
"Num=4.32"
_
Padding to make the width correct.
_________
123456789

12.346
" 12.346"
.
Indicates where the decimal point goes, and separates left from right formatting.
______.__
12.346
" 12.35"
#
Separates left from right formatting.
_(_#_)_
-12.3
" (12.3)"
(
Use '()' as the negative sign.
(____.__)
-12.3
"( 12.3)"
)
Use '()' as the negative sign.
_(___._)_
-12.3
" (12.3) "
-
Use '-' as the negative sign, with position as indicated.
-_____.__ _____.__-
-12.34
-12.34
"- 12.34"
" 12.34-"
+
Use '+' as the plus sign, with position as indicated.
_+____.__
12.34
" +12.34"
0
Set padding character to '0', rather than blank. Affects either before or after the decimal point.
+0___.__0
12.34
"+0012.340"
L
Left justify the number.
L_____.__
12.34
" 12.34 "
C
Center justify the number.
C_____.__
12.34
" 12.34 "
,
Separate characters before/after the decimal point into groups of three.
,_____.__
1234.98
" 1,234.98"
$
Add the currency symbol '$' in the position indicated.
$_____.__
12.34
"$ 12.34"

Formatting Wide Numbers

If the width attribute is smaller than needed for the number to be output, the output is marked with one or more width overflow characters (currently "*" is used). If the number would have to be truncated to the left of the decimal point in order to fit in the specified width, then the entire field is filled with width overflow characters. If the number can be made to fit by truncating at or to the right of the decimal place, then the number is output with a single trailing overflow character to indicate that truncation took place. For example, the following statements produce the strings shown:

Public Constructors

RWDecimalFormat();
RWDecimalFormat(const char *picture);

Public Member Functions

RWBoolean
allDecimals(Sign) const;
int
decimalPlaces(Sign) const;
RWCString
decimalSeparator(Sign) const;
RWBoolean
doRounding(Sign) const;
RWCString
farLeftText(Sign) const;
RWCString
farRightText(Sign) const;
RWBoolean
fixedWidth(Sign) const;
Justification
justification(Sign) const;
char
leftFillChar(Sign) const;
char
rightFillChar(Sign) const;
RWCString
leftDigitSeparator(Sign) const;
int
leftGroupSize(Sign) const;
RWCString
nearLeftText(Sign) const;
RWCString
nearRightText(Sign) const;
RWCString
rightDigitSeparator(Sign) const;
int
rightGroupSize(Sign) const;
RWDecimalBase::RoundingMethod
roundMethod(Sign) const;
int
roundPlaces(Sign) const;
RWBoolean
showDecimalPoint(Sign) const;
int
width(Sign) const;
void
setAllDecimals(RWBoolean, Sign=BOTH);
void
setDecimalPlaces(int, Sign=BOTH);
void
setDecimalSeparator(const RWCString&, Sign=BOTH);
void
setDoRounding(RWBoolean, Sign=BOTH);
void
setFarLeftText(const RWCString&, Sign=BOTH);
void
setFarRightText(const RWCString&, Sign=BOTH);
void
setFixedWidth(RWBoolean, Sign=BOTH);
void
setJustification(Justification, Sign=BOTH);
void
setLeftDigitSeparator(const RWCString&, Sign=BOTH);
void
setLeftFillChar(char, Sign=BOTH);
void
setLeftGroupSize(int, Sign=BOTH);
void
setNearLeftText(const RWCString&, Sign=BOTH);
void
setNearRightText(const RWCString&, Sign=BOTH);
void
setRightDigitSeparator(const RWCString&, Sign=BOTH);
void
setRightFillChar(char, Sign=BOTH);
void
setRightGroupSize(int, Sign=BOTH);
void
setRoundMethod(RWDecimalBase::RoundingMethod, Sign=BOTH);
void
setRoundPlaces(int, Sign=BOTH);
void
setShowDecimalPoint(RWBoolean, Sign=BOTH);
void
setWidth(unsigned int, Sign=BOTH);
void
setLocale(const RWLocaleSnapshot& newLocale);

Public Member Operators

RWCString
operator()(const char *number);
RWCString
operator()(const RWDecimal<T>&);
RWCString
operator()(const RWFixedDecimal<T>&);

Global Operators

RWCString 
operator<<(const RWDecimalFormat&, const RWDecimal<T>& x);
RWCString 
operator<<(const RWDecimalFormat&, const RWFixedDecimal<T>&);


Previous fileTop of DocumentContentsIndexNext file

©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.