-
January 24th, 2014, 12:06 AM
#1
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();
}
-
January 24th, 2014, 12:27 AM
#2
Re: Converting int to std::string using istringstream inserts commas
Originally Posted by vincegata
Also I know about std::stoi() and atoi() but I do not want them.
Why not?
-
January 24th, 2014, 01:01 AM
#3
Re: Converting int to std::string using istringstream inserts commas
Originally Posted by vincegata
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
-
January 24th, 2014, 05:55 AM
#4
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
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)
-
January 24th, 2014, 06:34 AM
#5
Re: Converting int to std::string using istringstream inserts commas
Originally Posted by Paul McKenzie
There is no way this function as it stands inserts commas into the numbers.
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|