CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Converting int to std::string using istringstream inserts commas

    Hi,

    So I have a following function that converts int to string however it inserts the commas for thousands, millions, and so on. e.g. 65432 -> "65,432".
    How do I make the conversion but have a string without commas?

    I know it has to with locale but I cannot find it how to prevent it. Also I know about std::stoi() and atoi() but I do not want them.

    Here is the code and thank you.

    Code:
    template <typename T>
    std::string NumberToString ( T Number )
    {
    	std::stringstream ss;
    	ss << Number;
    	return ss.str();
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Converting int to std::string using istringstream inserts commas

    Quote Originally Posted by vincegata
    Also I know about std::stoi() and atoi() but I do not want them.
    Why not?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Converting int to std::string using istringstream inserts commas

    Quote Originally Posted by vincegata View Post
    Here is the code and thank you.

    Code:
    template <typename T>
    std::string NumberToString ( T Number )
    {
    	std::stringstream ss;
    	ss << Number;
    	return ss.str();
    }
    Exactly what code is this? There is no way this function as it stands inserts commas into the numbers.

    To place commas, you have to do a little more work than just streaming a number to a stringstream object (a call to imbue and the locale to use needs to be done).

    Regards,

    Paul McKenzie

    Regards,

    Paul McKenzie

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Converting int to std::string using istringstream inserts commas

    This program using your template function
    Code:
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    template <typename T>
    std::string NumberToString ( T Number )
    {
    	std::stringstream ss;
    	ss << Number;
    	return ss.str();
    }
    
    int main()
    {
    int n = 1234567;
    double d = 34567.8;
    
    	cout << NumberToString<int>(n) << endl;
    	cout << NumberToString<double>(d) << endl;
    	return 0;
    }
    produces the output
    Code:
    1234567
    34567.8
    without any commas. So I'm confused as to how you think the function does insert commas?
    Last edited by 2kaud; January 24th, 2014 at 06:00 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Converting int to std::string using istringstream inserts commas

    Quote Originally Posted by Paul McKenzie
    There is no way this function as it stands inserts commas into the numbers.
    Quote Originally Posted by 2kaud
    So I'm confused as to how you think the function does insert commas?
    I have never seen it done myself, but there is locale support for numeric formatting with thousands place separators in the C++ standard library, so it may be the case that vincegata's default locale has this enabled and it is somehow actually in effect for stringstreams.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

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

Featured