Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format w/thousands seperator in C

Status
Not open for further replies.

Sandman83

Programmer
Sep 11, 2001
122
US
Hi,

I'm really new to C and am wondering how to format a number with thousands seperators and a decimal.
EX. #,###.##

P.S. any tutorial sites will be greatly appriciated as well.

Thanks.
 
Here's a little function I wrote originally in MFC to add commas for the 1,000s - this may help get you off on a start if you can adapt it (unless of course you are using MFC anyway).

[tt]CString Delimit(int i)
{
CString str;
str.Format("%d",i);
if (i < 1000) return str;

for (auto int x=str.GetLength()-3;x>=1;x-=3)
{
try {
str.Insert(x,',');
}
catch (auto CMemoryException* e) {
e->ReportError();
e->Delete();
}
}
return (str);
}
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
and if you just want to use #'s, use a floating point number, and just use the setprecision method of cout, and set it to the number of decimals you want The weevil of doooooooooom
-The eagle may soar, but the weasel never gets sucked up by a jet engine (Anonymous)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top