|
-
Dec 27th, 2002, 12:35 PM
#1
Thread Starter
Frenzied Member
Commas
Anyone got some code to put commas into a string of numbers?
ie "1234" = "1,230" "1234567890" = "1,234,567,890"
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Dec 27th, 2002, 06:33 PM
#2
C++
Code:
template <typename _C, typename _Traits, typename _Alloc>
inline std::basic_string<_C, _Traits, _Alloc> & add_commas(
const std::basic_string<_C, _Traits, _Alloc> & in,
std::basic_string<_C, _Traits, _Alloc> & out)
{
out.reserve(in.length()+(in.length()/3));
std::basic_string<_C, _Traits, _Alloc> temp = in;
std::reverse(temp.begin(), temp.end());
for(std::basic_string<_C, _Traits, _Alloc>::const_iterator i=temp.begin();
i!=temp.end(); ++i)
{
std::iterator_traits<std::basic_string<_C, _Traits, _Alloc>::const_iterator>
::difference_type diff = i-temp.begin();
if(diff>0 && diff%3==0)
out.push_back(std::use_facet<std::ctype<_C> >(std::locale()).widen(','));
out.push_back(*i);
}
std::reverse(out.begin(), out.end());
return out;
}
100% standards compliant, but not really locale sensitive (some locales want a . inserted instead of a ,)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 27th, 2002, 06:46 PM
#3
Locale definition returns a NUL-char for the thousands seperator for me...
You'll have to do with this one.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 27th, 2002, 06:46 PM
#4
Thread Starter
Frenzied Member
Ok thanks this at least gives me something to work with.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Dec 27th, 2002, 06:48 PM
#5
Thread Starter
Frenzied Member
Hmm I keep getting
error C2039: 'reverse' : is not a member of 'std'
Any ideas?
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Dec 27th, 2002, 07:00 PM
#6
Sorry, you need these headers:
<string>
<locale>
<algorithm>
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 27th, 2002, 07:03 PM
#7
Thread Starter
Frenzied Member
Ok thanks up and running now
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Dec 28th, 2002, 10:13 AM
#8
np, if you want anything else (like an explanation ) just ask.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 28th, 2002, 01:07 PM
#9
Thread Starter
Frenzied Member
Actually I am only familure with the basics of templates, yet this seem pretty self exlanitory if you just think about it.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

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
|