String formatter

 

Download C++ source code - 3 Kb

 

The provided source code solves the recurrent issue of formatting strings, that is repurpose string content so they match a particular data type and culture. Formatting is often used to display percentage signs on numeric values, and is also used to restrict or enforce a given amount of decimals in floating numbers.

Well I won't give here an introduction to trivia, I am only trying to show a C++ class that I personally am going to reuse in more than one scenarios in the coming weeks. And since the code is simple and useful, I thought to share it. Use it in your convenience.

The class itself only relies on stdio. That would have been pretty much useless to have a dependency on MFC code or the like. Of note is the fact that .NET has its own formatting schema so it's up to you to stick with the one I have implemented, or just go the .NET way of formatting strings.

The formatting semantics supported by the class is the one from MS Excel. Here is an example : ### ###.00 which allows to make sure that numbers passed in are repurposed to show a thousand separator, and have 2 decimals on their right even if the passed numbers are integers. Simple rules that apply are as follows :

  • # is a placeholder for any number. If there is no number, no 0 is inserted
  • 0 is same than #, except that if there is no number, a 0 is inserted
  • <space char> is used as a thousand separator
  • . is used as a decimal separator
  • Any other character is inserted. For instance [Red]## ##.00 is valid, and allows Excel to output the number and switch the cell to red. (this makes sense in Excel only of course).

Of course, since separators are culture-specific, the class has a simple API to allow to specify custom separators, or let the class find the separators from the user locale (as displayed in the control panel / regional settings).

In short, here is the API exposed by the class :


// SetSeparators //////////////////////////////////////////////////////
//
// set custom separators
//
// param : cThousandSep : used to represent the thousand separators. For instance ' '
//         cDecimalSep : used to represent the decimal separator. For instance ','
//
void SetSeparators(/*in*/char cThousandSep,/*in*/char cDecimalSep);


// Format /////////////////////////////////////////////////////////////
//
// does the actual string formatting, taking into account custom separators or not
//
// param : szInput is the input string, for instance "123"
//         szFormat is the format, for instance "### ###.00"
//         szOutputBufer is a pointer to the output buffer (no memory allocation is performed)
//         nOutputBufferLen is the length of the buffer passed in szOutputBuffer
void Format(/*in*/LPSTR szInput, 
			/*in*/LPSTR szFormat, 
			/*out*/LPSTR szOutputBuffer, /*in*/long nOutputBufferLen);

And here are two samples :

User locale separators
  strformatter s;
  char sztmp[MAX_PATH];
  s.Format("4321653","### ##0,00F",sztmp,MAX_PATH);

  ==> produces 4321 653,00F
Custom separators
  strformatter s;
  char sztmp[MAX_PATH];
  s.SetSeparators(' ',',');
  s.Format("4321653,123","[Red]( ### ##0,00%)",sztmp,MAX_PATH);

  ==> produces [Red](4 321 653,12%)

 

 

Enjoy!
Stephane Rodriguez- September 21, 2003.

Home
Blog