Click here to Skip to main content
11,429,154 members (63,543 online)
Click here to Skip to main content

XFormatNumber - A function to format numbers with commas

, 25 Jun 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
An easy-to-use function that provides locale-correct comma-formatting of numbers

Introduction

Formatting of numeric values with commas looks nice, but sometimes commas are not the correct thousands separator - many countries use "." for thousands separator. To ensure the correct separator, the Win32 function GetNumberFormat() should be used. However, this API requires the setup of a decimal separator, thousands separator, digit grouping, etc., which can add many lines of code to do a simple thing.

After needing to use GetNumberFormat() one more time, I decide to make a simple function with defaults taken from the locale.

XFormatNumber Features

XFormatNumber takes as input a numeric string, which may or may not contain decimal point and fractional digits. XFormatNumber is defined as

CString XFormatNumber(LPCTSTR lpszNumber, int nFracDigits)
The parameter lpszNumber is numeric string, and the nFracDigits parameter specifies whether to include fractional digits in the  returned CString: 0 means no fractional digits, >0 means return nFracDigits fractional digits, and -1 means return however many fractional digits are contained in the numeric string.

XFormatNumber Demo

The demo app shows results for various formatting options:

m_List.AddString(XFormatNumber(_T("1"), -1));
m_List.AddString(XFormatNumber(_T("10"), -1));
m_List.AddString(XFormatNumber(_T("100"), -1));
m_List.AddString(XFormatNumber(_T("1000"), -1));
m_List.AddString(XFormatNumber(_T("10000"), -1));
m_List.AddString(XFormatNumber(_T("100000"), -1));
m_List.AddString(XFormatNumber(_T("1000000"), -1));
m_List.AddString(XFormatNumber(_T("10000000"), -1));
m_List.AddString(XFormatNumber(_T("100000000"), -1));
m_List.AddString(XFormatNumber(_T("1000000000"), -1));
m_List.AddString(XFormatNumber(_T("1000.0"), -1));
m_List.AddString(XFormatNumber(_T("1000.00"), -1));
m_List.AddString(XFormatNumber(_T("1000.000"), -1));
m_List.AddString(XFormatNumber(_T("1000.0000"), -1));
m_List.AddString(XFormatNumber(_T("1000.00000"), 1));

XFormatNumber Screenshot

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
GeneralPerformance Improvement Pin
Gautam Jain26-Oct-07 4:32
memberGautam Jain26-Oct-07 4:32 
AnswerRe: Performance Improvement Pin
Hans Dietrich20-Apr-11 1:00
mentorHans Dietrich20-Apr-11 1:00 
Generaladded default value Pin
breakpoint3-May-06 17:21
memberbreakpoint3-May-06 17:21 
AnswerRe: added default value Pin
Hans Dietrich20-Apr-11 0:59
mentorHans Dietrich20-Apr-11 0:59 
GeneralSome comments Pin
Victor Boctor5-Dec-02 2:01
memberVictor Boctor5-Dec-02 2:01 
Nice work. Following are some comments:

1. I would replace:
ASSERT(lpszNumber);
with
AfxIsValidString(lpszNumber)
2. In the code below:
if (dp == -1)
{
  // set number of fractional digits from input string
  CString strDecSep = szDecSep;
  dp = str.Find(szDecSep);
  dp = (dp == -1) ? 0 : str.GetLength() - dp - strDecSep.GetLength();
}
Shouldn't the decimal point be always a dot? The GetNumberFormat() document includes:
One decimal point (dot) if the number is a floating-point value
If you don't always use the dot, then the caller would have to use GetLocaleInfo() to prepare the string.

3. In the code below:
_TCHAR *pszFormattedNumber = new _TCHAR [nSize];
ASSERT(pszFormattedNumber);
If the allocation fails, an exception will be thrown and hence the ASSERTion adds no value.

4. It would be good to document your assumptions for calculating the expected output string size in the code below:
int nSize = (_tcslen(lpszNumber) * 3) + 100;
Regards,
Victor.

phpWebNotes is a page annotation system modelled after php.net.
http://webnotes.sourceforge.net/demo.php[^]
AnswerRe: Some comments Pin
Hans Dietrich20-Apr-11 0:59
mentorHans Dietrich20-Apr-11 0:59 
Generalfractional digits are limited to 9 Pin
C++ Hacker1-Jul-02 23:09
memberC++ Hacker1-Jul-02 23:09 
AnswerRe: fractional digits are limited to 9 Pin
Hans Dietrich20-Apr-11 0:58
mentorHans Dietrich20-Apr-11 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 26 Jun 2002
Article Copyright 2002 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid