Custom Numeric Formatting

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



Comments

  • Colors.h header

    Posted by Legacy on 02/15/1999 12:00am

    Originally posted by: Eugeny Berezkin

    I have forgotten to include very simple header file to source and demo archives - colors.h. This header
    consists of just few lines:
    
    

    #define Black RGB( 0, 0, 0)
    #define Blue RGB( 0, 0, 255)
    #define Cyan RGB( 0, 255, 255)
    #define Green RGB( 0, 255, 0)
    #define Magenta RGB(255, 0, 255)
    #define Red RGB(255, 0, 0)
    #define Yellow RGB(255, 255, 0)
    #define White RGB(255, 255, 255)

    Excuse me for my disappointing fault.

    Eugeny

    Reply
  • Missing Header

    Posted by Legacy on 02/13/1999 12:00am

    Originally posted by: ISIDRO PADILLA

    Hi,

    Thank for this interesting work. Please, could you include colors.h

    Thank you very much.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds