Converting int to std::string using istringstream inserts commas
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

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

  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,285

    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,427

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,386

    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 05:00 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center