-
July 28th, 2004, 02:59 PM
#1
Formatting number strings?
I have a number as a string
string aaa;
//assign string number
aaa = "333333333";
And I would like to reformat the number to make it more readable...
//reformat
aaa = "333,333,333"
Is there an easy way to do this?
-
July 28th, 2004, 03:45 PM
#2
Maybe something like this?
PHP Code:
void MakeNiceString(CString *nStr,int nSpace,CString nChar)
{
int y = nStr->GetLength();
int iCharCount = 0;
while (y > 0)
{
if(iCharCount == nSpace)
{
iCharCount = 0;
nStr->Insert(y,nChar);
}
iCharCount++;
y--;
}
}
CString aaa = "3333333333";
MakeNiceString(&aaa,3,",");
AfxTrace(aaa); // 3,333,333,333
/* Regards, David Smulders */
-
July 29th, 2004, 12:46 AM
#3
Just wondering if there is any C++ way where we can use std::locale and std::facet to achieve the same thing. Unfortunately, so far my google search has not find any good example.
-
July 29th, 2004, 02:59 AM
#4
Okay, just manage to get this code to work on VC++.Net. However, on VC++6, dvalue is not being displayed properly. Guess that is because VC++6 is not as standard complaint as compared to VC++.Net.
Code:
#include <locale>
#include <iostream>
using namespace std;
int main()
{
int ivalue = 10000;
double dvalue = 20000.202;
cout.imbue( locale("enu") );
cout << ivalue << endl;
cout << dvalue << endl;
cout.imbue( locale::classic() ); // Restore to classic C locale.
return 0;
}
Last edited by Kheun; July 29th, 2004 at 03:09 AM.
-
July 29th, 2004, 05:21 AM
#5
Here is a function for VC++ 6.0
This will insert commas on exact locations
void FormatNumber(CString& vol)
{
vol.TrimLeft();
vol.TrimRight();
int len = vol.GetLength();
for(int i = len - 3 ; i > 0 ; i -= 3)
{
vol.Insert(i,",");
}
}
-
July 29th, 2004, 06:20 AM
#6
will these people please STOP offering solutions using MFC! This is a non-MFC forum and the original poster did not specify MFC.
Now, how to insert a comma every 3rd character from the right? (I like Kheun's idea but we know we have started with a string and we'll assume we know it's numeric and want a comma and not any other symbol).
Now let's go for an old-fashioned for-loop:
inserting from the front...
Code:
std::string s;
len = s.length();
for ( int i = ((len-1)%3) + 1; i < len; i+=4, ++len )
{
s.insert( i, 1, ',' );
}
from the back I think this will work:
Code:
std::string s;
len = s.length();
for ( int i= len - 3; i > 0; i -= 3 )
{
s.insert( i, 1, ',' );
}
Anyone who can come up with wonderful insert_iterator and transform or whatever to do something like this will get prizes for elegance.
Last edited by NMTop40; July 29th, 2004 at 06:23 AM.
-
July 29th, 2004, 10:19 AM
#7
Thanks for all the help, guys.
-
July 29th, 2004, 11:50 AM
#8
Well, there is a new wrinkle in this. I was not told this, the numbers are dollar amounts with cents.
so 100000.00 needs to look like 100,000.00
-
July 29th, 2004, 12:00 PM
#9
Originally Posted by Kheun
Code:
#include <locale>
#include <iostream>
using namespace std;
int main()
{
int ivalue = 10000;
double dvalue = 20000.202;
cout.imbue( locale("enu") );
cout << ivalue << endl;
cout << dvalue << endl;
cout.imbue( locale::classic() ); // Restore to classic C locale.
return 0;
}
You use a hardcoded "enu" and you restore to ::classic which might be wrong.
Better:
Code:
#include <iostream>
#include <locale>
#include <iomanip>
using namespace std;
int main()
{
locale loc("");
locale loc_previous = cout.imbue(loc);
cout.precision(2);
cout << std::fixed;
double d = 2002.256530;
cout << d << std::endl;
cout.imbue(loc_previous);
cout << d << std::endl;
return 0;
}
-
July 29th, 2004, 12:26 PM
#10
Marc, I need to toss the formatted number into a string. It is going to be later written out to a file.
-
July 29th, 2004, 12:42 PM
#11
Then use a stringstream.
Code:
stringstream ss;
locale loc("");
locale loc_previous = ss.imbue(loc);
ss.precision(2);
ss << std::fixed;
double d = 2002.256530;
ss << d << std::endl;
ss.imbue(loc_previous);
// Get the string from the stringstream:
ss.str();
-
July 29th, 2004, 01:10 PM
#12
I am having trouble using streams in this project. I get linker errors in libcpd.lib. The C++ streams have a conflict with the MySql lib. Is there another solution?
-
July 29th, 2004, 01:31 PM
#13
Could you post some of the errors?
-
July 29th, 2004, 03:54 PM
#14
To link in mysqlclient.lib, I had to tell the linker to ignore LIBCD.lib or I get link redef errors.
So if I put this in
ifstream a("lala.txt");
Here are the link errors...
ibcpd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
libcpd.lib(locale.obj) : error LNK2001: unresolved external symbol __malloc_dbg
libcpd.lib(_tolower.obj) : error LNK2001: unresolved external symbol __malloc_dbg
libcpd.lib(xwcsxfrm.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function __Wcsxfrm
libcpd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
libcpd.lib(xwcsxfrm.obj) : error LNK2001: unresolved external symbol __free_dbg
libcpd.lib(locale.obj) : error LNK2019: unresolved external symbol __realloc_dbg referenced in function "private: void __thiscall std::locale::_Locimp::_Addfac(class std::locale::facet *,unsigned int)" (?_Addfac@_Locimp@locale@std@@AAEXPAVfacet@23@I@Z)
libcpd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol _errno
libcpd.lib(xwcscoll.obj) : error LNK2001: unresolved external symbol _errno
libcpd.lib(xstrcoll.obj) : error LNK2001: unresolved external symbol _errno
libcpd.lib(locale.obj) : error LNK2001: unresolved external symbol _errno
libcpd.lib(xwctomb.obj) : error LNK2001: unresolved external symbol _errno
libcpd.lib(wlocale.obj) : error LNK2001: unresolved external symbol _errno
libcpd.lib(xlocale.obj) : error LNK2001: unresolved external symbol _errno
libcpd.lib(_tolower.obj) : error LNK2019: unresolved external symbol __chvalidator referenced in function __Tolower
libcpd.lib(_toupper.obj) : error LNK2019: unresolved external symbol __chvalidator referenced in function __Toupper
libcpd.lib(xmbtowc.obj) : error LNK2019: unresolved external symbol __CrtDbgReport referenced in function __Mbrtowc
Debug/EasyCheckup.exe : fatal error LNK1120: 6 unresolved externals
Build log was saved at "file://c:\EZTax Projects\EasyCheckup\Debug\BuildLog.htm"
EasyCheckup - 18 error(s), 3
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|