Results 1 to 9 of 9

Thread: Commas

  1. #1

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024

    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


  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  4. #4

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  5. #5

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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


  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  9. #9

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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
  •  



Click Here to Expand Forum to Full Width