1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Handling Ints and Floats

By David Bolton, About.com

6 of 8

About Locale and Moneypunct

The example used a locale object from the PC in the line
locale loc("") ;

The line

const moneypunct <char, true> &mpunct = use_facet <moneypunct <char, true > >(loc) ;
creates an object mpunct which is a reference to a moneypunct template class. This has information about the specified locale - in our case the thousands_sep() method returns the character used for thousands separator.

Without the line

cout.imbue( loc ) ;
There would be no thousand's separators. Try commenting it out and rerunning the program.

Note There seem to be discrepancies between different compilers as to how cout.imbue behaves. Under Visual C++ 2005 Express Edition, this included separators. But the same code with Microsoft Visual C++ 6.0 did not!

Decimal Points

The example on the previous page used showpoint to show trailing zeros after the decimal points. It output numbers in what is called standard mode. Other modes include
  • Fixed Mode - Show numbers like 567.8
  • Scientific Mode - Show numbers like 1.23450e+009
If you use either of these two formatting modes through the cout.setf then precision() sets the number of decimal places after the decimal point (not the overall number of digits) but you lose the thousands formatting. Also trailing zeroes (as were enabled by ios_base::showpoint ) become automatically enabled without needing showpoint.

On the Next Page - Some Things to Watch out for

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. About Locale and Moneypunct

©2009 About.com, a part of The New York Times Company.

All rights reserved.