Apr 15, 2021 at 8:30am UTC
How do I format a number to be displayed with commas to separate every three zeroes like this 1,000,000.00 in c++ for double and 1,000,0000 for integer?
I ultimately want to have this information stored in a string so I can display it in OpenGL. I already have my OpenGL code to print the string. But want an efficient method to add in the commas into the string.
Last edited on Apr 15, 2021 at 8:31am UTC
Apr 15, 2021 at 9:09am UTC
Last edited on Apr 15, 2021 at 12:01pm UTC
Apr 15, 2021 at 9:34am UTC
Is that the same solution in this video?
https://www.youtube.com/watch?v=9SubQGhfgJQ&t=699s
I copy pasted the code but there were still errors: c:\users\neo\downloads\workspace\c++vs6\2d\spacecannonsource\textout.h(65) : error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'
Last edited on Apr 15, 2021 at 9:50am UTC
Apr 15, 2021 at 11:11am UTC
Please post the code that you are having trouble with. Enough to run if the compile time errors were fixed.
Apr 15, 2021 at 11:38am UTC
Does this work?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <locale>
using namespace std;
struct separated : numpunct<char >
{
char do_thousands_sep() const { return ',' ; }
string do_grouping() const { return "\03" ; }
};
int main()
{
locale our_local( cout.getloc(), new separated );
double d = 1000000;
int i = d + 0.5;
cout.imbue( our_local );
cout << fixed << setprecision(2) << d << '\n' ;
cout << i << '\n' ;
stringstream ss;
ss.imbue( our_local );
ss << fixed << setprecision(2) << d << " and " << i;
string S = ss.str();
cout << "As a string: " << S;
}
1,000,000.00
1,000,000
As a string: 1,000,000.00 and 1,000,000
Last edited on Apr 15, 2021 at 1:33pm UTC
Apr 15, 2021 at 3:47pm UTC
error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'
im using vs6
Last edited on Apr 15, 2021 at 3:48pm UTC
Apr 15, 2021 at 3:48pm UTC
recklessasiandriver wrote:error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'
Is that:
- the error when running my code?
- the error when running your code?
Please post your CODE, not the error message.
Last edited on Apr 15, 2021 at 3:49pm UTC
Apr 15, 2021 at 4:00pm UTC
locale our_local( cout.getloc(), new separated );
c:\users\david\onedrive\workspace\c++vs6\2d\spacecannonsource\textout.cpp(155) : error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'
I also shifted the code to a different part of my code and got the above error.
Apr 15, 2021 at 4:04pm UTC
Show the WHOLE of your code, not one line.
We need to be able to compile it.
How old is your compiler? Run my code on this site (Click on "Edit & Run").
VS6 is, err, ancient.
Last edited on Apr 15, 2021 at 4:09pm UTC
Apr 15, 2021 at 10:13pm UTC
What's the link to it? I will try and upgrade my code to it.
Apr 16, 2021 at 9:18am UTC
Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <string>
#include <locale>
struct comma_facet : public std::numpunct<char > {
explicit comma_facet(unsigned char group) : g(group) {}
char do_thousands_sep() const override { return ',' ; }
std::string do_grouping() const override { return std::string(1, g); }
unsigned char g {};
};
auto set000s(std::ostream& os, unsigned char group = 3)
{
return os.imbue(std::locale(os.getloc(), new comma_facet(group)));
}
int main()
{
const auto num {9876543};
set000s(std::cout);
std::cout << num << '\n' ;
set000s(std::cout, 4);
std::cout << num << '\n' ;
}
Last edited on Apr 16, 2021 at 9:28am UTC
Apr 16, 2021 at 1:20pm UTC
c:\users\david\onedrive\workspace\c++vs6\2d\spacecannonsource\main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'override'
Adding a semi colon then creates this:
c:\users\david\onedrive\workspace\c++vs6\2d\spacecannonsource\main.cpp(7) : error C2059: syntax error : 'return'
Last edited on Apr 16, 2021 at 1:24pm UTC
Apr 16, 2021 at 1:21pm UTC
What's the link to it?
Do you have Google? Consider using it sometime.
Government has forbidden it's use in our country.
Apr 16, 2021 at 4:38pm UTC
Compile as C++11! Or try removing override.
Last edited on Apr 16, 2021 at 4:41pm UTC