Custom Numeric Formatting

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Class CNumericFormat was developed to provide user customizable numeric formatting to display any numeric data in your application.

Format information for displaying numbers is described by format string. Format string can contain following information:

  • Decimal point position and number of digits after decimal point
  • Rules for displaying significant and insignificant digits
  • Scaling a number by a multiple of one or more thousands
  • Any prefixes and postfixes
  • Color of number
  • Conditional selection of format information form the set of format groups

Formatted number can be displayed everywhere in your application where you have prepared device context. Also you are able to prepare formatted number without displaying.

Class CNumericFormat and sample application were created using Microsoft Visual C++ v5.0 and tested with and without _UNICODE.

How to integrate CNumericFormat in your application

  1. Include the following files into your application:

    NumFrm.h

    NumFrm.cpp

  2. Somewhere in your project (for example in document class) declare, or dynamically create instance of CNumericFormat class, and provide access to it form drawing part of your code.

  3. Set format string by using SetFormat member function.

  4. Call printing member functions form drawing stuff.

CNumericFormat members

CNumericFormat()

Default constructor for CNumericFormat class.

CNumericFormat(const CString& sFormat)

Constructor for CNumericFormat class with format string initialization.

BOOL LoadString(UINT nRes)

Loads format string given as resource.

BOOL SetFormat(const CString& sFormat)

Sets format string.

CString GetFormat() const

Retrieves format string

LPCTSTR PrintNumber(double nVal, CDC* pDC, LPRECT lpRect, UINT nFormat = DT_LEFT, int nForceDec = -1) const

Prints formatted number using ::DrawText API call.

Parameters:

  • nVal Specifies the number to be printed.
  • pDC Points to device context class.
  • lpRect Points to a RECT structure or CRect object that contains the rectangle (in logical coordinates) in which the number is to be formatted.
  • nFormat Specifies the method of formatting the text for ::DrawText API function.
  • nForceDec Specifies the number of digits after decimal point, or -1 if the number of digits after decimal point should be defined from format string.

Return value: Pointer to buffer with formatted number if successful, NULL otherwise.

LPCTSTR PrintNumberETO(double nVal, CDC* pDC, int x, int y, UINT nOptions, LPCRECT lpRect, int nForceDec = -1) const

Prints formatted number using ::ExtTextOut API call.

Parameters:

  • nVal Specifies the number to be printed.
  • pDC Points to device context class.
  • x Specifies the logical x-coordinate of the character cell for the first character in the specified string.
  • y Specifies the logical y-coordinate of the top of the character cell for the first character in the specified string.
  • nOptions Specifies the rectangle type for ::ExtTextOut API function.
  • lpRect Points to a RECT structure that determines the dimensions of the rectangle. This parameter can be NULL. You can also pass a CRect object for this parameter.
  • nForceDec Specifies the number of digits after decimal point, or -1 if the number of digits after decimal point should be defined from format string.

Return value: Pointer to buffer with formatted number if successful, NULL otherwise.

LPCTSTR SPrintNumber(double nVal, BOOL bFull = FALSE) const

Makes formatted number and stores its it the internal buffer.

Parameters:

  • nVal Specifies the number to be processed.
  • bFull If this parameter is FALSE, that resulting formatted number will be formed without thousands scaling and without prefixes and postfixes.

Return value: Pointer to buffer with formatted number if successful, NULL otherwise.

LPCTSTR SPrintNumberSimple(double nVal, int nForceDec = -1) const

Makes formatted number like usual sprintf function call with specified precision.

Parameters:

  • nVal Specifies the number to be processed.
  • nForceDec Specifies the number of digits after decimal point, or -1 if the number of digits after decimal point should be defined from format string.

Return value: Pointer to buffer with formatted number if successful, NULL otherwise.

friend CArchive& operator<<(CArchive& ar, const CNumericFormat& frmt)

Stores CNumericFormat object to archive.

friend CArchive& operator>>(CArchive& ar, CNumericFormat& frmt)

Loads CNumericFormat from archive.

Format string description

Format string consists of one or more format groups. Format groups within format string are delimited by semicolon sign (;). Different format groups are used for conditional numeric formatting. Each format group can include one or more conditions, enclosed into square brackets. Selection of particular format group is made from left to right until satisfied group for printed number will be found.
If the count of groups more than one and all groups do not contain conditions, than first group automatically accepts [>=0] condition in case of count of groups is two and [>0] otherwise, and second group automatically accepts [<0] condition.

Format group can contain information about color. The colors are defined via its names enclosed into square brackets. The following set of colors is available:

  • Green
  • Red
  • Magenta
  • Cyan
  • Yellow
  • White
  • Black

If format group does not contain color tag, current text color for device context will be used.

Table below describes worker format string characters:

# Displays only significant digit, does not display insignificant zeros.
0 If stays on right side of the decimal point, than displays insignificant zeros if a number has fewer significant digits. If stays on left side of the decimal point, displays zero before decimal point if absolute value of number is less than 1.
? Adds spaces for insignificant zeros on right side of the decimal point.
~ Purges negative sign for negative values.
% Multiplies number on 100 and appends % sign.
. Shows decimal point position.
, Indicates separation of thousands if stays before last digit holder character (#, 0 and ?). Scale a number by a multiple of one or more thousands (according to the count) if stays after last digit holder character.

Thousand and decimal characters in formatted numbers always come form locale information.

Download demo project – 25 KB

Download source – 6 KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read