Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > function for printing formatted number

Reply
Thread Tools

function for printing formatted number

 
 
wang
Guest
Posts: n/a
 
      08-22-2010
Hi all
is there a function for converting a number such as 1234567.89 into a
string presentation as "1,234,567.89" for printing? Thanks for any
hints!
kwwang
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-22-2010
wang <>, on 21/08/2010 18:37:18, wrote:

> Hi all
> is there a function for converting a number such as 1234567.89 into a
> string presentation as "1,234,567.89" for printing? Thanks for any
> hints!
> kwwang


Nothing straight out of the standard C++ box.

Make a search in boost, there could be an appropriate "format" function,
although such a simple function can be created in almost no time. Why
don't you give it a shot and post your code here? You might find that
instructive.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      08-22-2010
On 22 aug, 04:37, wang <king_ww...@yahoo.de> wrote:
> is there a function for converting a number such as 1234567.89 into a
> string presentation as "1,234,567.89" for printing? Thanks for any
> hints!


Such things are in C++ standard library. Hints are such:

class std::locale, template class std::numpunct, template class
std::basic_ios, especially the function imbue().

If you google for these you probably find tons of example code
snippets. I am not sure if they work with MSVC 6.0 however, with 2005
they did work.
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      08-22-2010
Francesco S. Carta wrote:

> > Hi all
> > is there a function for converting a number such as 1234567.89 into a
> > string presentation as "1,234,567.89" for printing? Thanks for any
> > hints!
> > kwwang

>
> Nothing straight out of the standard C++ box.


This is one of those perennial FAQs that everyone needs and nobody
covers. The locale/facets system in iostreams is apparently a kit for
you to assemble.

The ultimate problem appears to be some undiscovered culture somewhere
might separate hundreds, or ten-thousands, so a locale cannot simply
assume thousands and then ask what delimiter goes there!

http://www.velocityreviews.com/forum...le-output.html

#include <sstream>
#include <iostream>
#include <locale>
#include <string>

// custom numeric punctuation facet
struct Punct: std::numpunct<char>
{
char do_thousands_sep () const
{
return ',';
}
std::string do_grouping () const
{
return "\3";
}
};

std::string toStr(int x,std::locale const& l)
{
std::stringstream stream;
stream.imbue(l);

stream << x;
return stream.str();
}


int main()
{
// construct a custom punctuation facet
std::numpunct<char>* punct = new Punct;

// construct a locale containing the custom facet
const std::locale locale(std::cout.getloc(),punct);

std::cout.imbue(locale);
std::cout << "Val: " << 1234556 << std::endl;
std::cout << "From String: " << toStr(123456789,locale) <<
std::endl;
}

That thread then gets the James Kanze treatment, so read his prose to
learn what horrors I just pasted in, untested!

--
Phlip
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-22-2010
Phlip <>, on 21/08/2010 21:14:31, wrote:

> Francesco S. Carta wrote:
>
>>> Hi all
>>> is there a function for converting a number such as 1234567.89 into a
>>> string presentation as "1,234,567.89" for printing? Thanks for any
>>> hints!
>>> kwwang

>>
>> Nothing straight out of the standard C++ box.

>
> This is one of those perennial FAQs that everyone needs and nobody
> covers. The locale/facets system in iostreams is apparently a kit for
> you to assemble.
>
> The ultimate problem appears to be some undiscovered culture somewhere
> might separate hundreds, or ten-thousands, so a locale cannot simply
> assume thousands and then ask what delimiter goes there!
>
> http://www.velocityreviews.com/forum...le-output.html
>
> #include<sstream>
> #include<iostream>
> #include<locale>
> #include<string>
>
> // custom numeric punctuation facet
> struct Punct: std::numpunct<char>
> {
> char do_thousands_sep () const
> {
> return ',';
> }
> std::string do_grouping () const
> {
> return "\3";
> }
> };
>
> std::string toStr(int x,std::locale const& l)
> {
> std::stringstream stream;
> stream.imbue(l);
>
> stream<< x;
> return stream.str();
> }
>
>
> int main()
> {
> // construct a custom punctuation facet
> std::numpunct<char>* punct = new Punct;
>
> // construct a locale containing the custom facet
> const std::locale locale(std::cout.getloc(),punct);
>
> std::cout.imbue(locale);
> std::cout<< "Val: "<< 1234556<< std::endl;
> std::cout<< "From String: "<< toStr(123456789,locale)<<
> std::endl;
> }
>
> That thread then gets the James Kanze treatment, so read his prose to
> learn what horrors I just pasted in, untested!


All of what you have written was somewhere in the background of my
"Nothing straight out of the standard C++ box" reply.

It's far easier to lookup an ad-hoc function or to write a simple one
instead of delving into the complexity of assembling your own locale,
for such a simple task.

Granted, if one is accustomed to deal with them, one can come up with
something like the above straight from the top of one's head.

As I see it, it just feels like shooting flies with a cannon, with the
further downside that I need to assemble it.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      08-22-2010
"Öö Tiib" wrote:

> On 22 aug, 04:37, wang <king_ww...@yahoo.de> wrote:
>> is there a function for converting a number such as 1234567.89 into a
>> string presentation as "1,234,567.89" for printing? Thanks for any
>> hints!

>
> Such things are in C++ standard library. Hints are such:
>
> class std::locale, template class std::numpunct, template class
> std::basic_ios, especially the function imbue().


That also answers the question, "Can I learn C++ in 21 days?"


 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      08-22-2010
On 22 aug, 13:41, "Francesco S. Carta" <entul...@gmail.com> wrote:
> Phlip <phlip2...@gmail.com>, on 21/08/2010 21:14:31, wrote:
>
>
>
> > Francesco S. Carta wrote:

>
> >>> Hi all
> >>> is there a function for converting a number such as 1234567.89 into a
> >>> string presentation as "1,234,567.89" for printing? Thanks for any
> >>> hints!
> >>> kwwang

>
> >> Nothing straight out of the standard C++ box.

>
> > This is one of those perennial FAQs that everyone needs and nobody
> > covers. The locale/facets system in iostreams is apparently a kit for
> > you to assemble.

>
> > The ultimate problem appears to be some undiscovered culture somewhere
> > might separate hundreds, or ten-thousands, so a locale cannot simply
> > assume thousands and then ask what delimiter goes there!

>
> >http://www.velocityreviews.com/forum...commas-to-int-...

>
> > #include<sstream>
> > #include<iostream>
> > #include<locale>
> > #include<string>

>
> > // custom numeric punctuation facet
> > struct Punct: std::numpunct<char>
> > {
> > char do_thousands_sep () const
> > {
> > return ',';
> > }
> > std::string do_grouping () const
> > {
> > return "\3";
> > }
> > };

>
> > std::string toStr(int x,std::locale const& *l)
> > {
> > std::stringstream stream;
> > stream.imbue(l);

>
> > stream<< *x;
> > return stream.str();
> > }

>
> > int main()
> > {
> > // construct a custom punctuation facet
> > std::numpunct<char>* punct = new Punct;

>
> > // construct a locale containing the custom facet
> > const std::locale locale(std::cout.getloc(),punct);

>
> > std::cout.imbue(locale);
> > std::cout<< *"Val: "<< *1234556<< *std::endl;
> > std::cout<< *"From String: "<< *toStr(123456789,locale)<<
> > std::endl;
> > }

>
> > That thread then gets the James Kanze treatment, so read his prose to
> > learn what horrors I just pasted in, untested!

>
> All of what you have written was somewhere in the background of my
> "Nothing straight out of the standard C++ box" reply.
>
> It's far easier to lookup an ad-hoc function or to write a simple one
> instead of delving into the complexity of assembling your own locale,
> for such a simple task.
>
> Granted, if one is accustomed to deal with them, one can come up with
> something like the above straight from the top of one's head.
>
> As I see it, it just feels like shooting flies with a cannon, with the
> further downside that I need to assemble it.


Usually you have that cannon constructed already if you work for shop
that writes C++ for international market. The problem is heavily
localization related. For you "1,234,567.89" is may be natural so you
think it is a "fly" to achieve it. German-speaking nations often like
it other way around, like "1.234.567,89". Some Hindi-speaking guy does
not understand thousand separator, for them there is hundreds
separators. So the solution to have same number readable for all of
them is to have locales, or what else you suggest to do? Hack it ad-
hoc until it is unreadable blob of crap?
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-22-2010
Öö Tiib <>, on 22/08/2010 09:09:15, wrote:

> On 22 aug, 13:41, "Francesco S. Carta"<entul...@gmail.com> wrote:
>> Phlip<phlip2...@gmail.com>, on 21/08/2010 21:14:31, wrote:
>>
>>
>>
>>> Francesco S. Carta wrote:

>>
>>>>> Hi all
>>>>> is there a function for converting a number such as 1234567.89 into a
>>>>> string presentation as "1,234,567.89" for printing? Thanks for any
>>>>> hints!
>>>>> kwwang

>>
>>>> Nothing straight out of the standard C++ box.

>>
>>> This is one of those perennial FAQs that everyone needs and nobody
>>> covers. The locale/facets system in iostreams is apparently a kit for
>>> you to assemble.

>>
>>> The ultimate problem appears to be some undiscovered culture somewhere
>>> might separate hundreds, or ten-thousands, so a locale cannot simply
>>> assume thousands and then ask what delimiter goes there!

>>
>>> http://www.velocityreviews.com/forum...commas-to-int-...

>>
>>> #include<sstream>
>>> #include<iostream>
>>> #include<locale>
>>> #include<string>

>>
>>> // custom numeric punctuation facet
>>> struct Punct: std::numpunct<char>
>>> {
>>> char do_thousands_sep () const
>>> {
>>> return ',';
>>> }
>>> std::string do_grouping () const
>>> {
>>> return "\3";
>>> }
>>> };

>>
>>> std::string toStr(int x,std::locale const& l)
>>> {
>>> std::stringstream stream;
>>> stream.imbue(l);

>>
>>> stream<< x;
>>> return stream.str();
>>> }

>>
>>> int main()
>>> {
>>> // construct a custom punctuation facet
>>> std::numpunct<char>* punct = new Punct;

>>
>>> // construct a locale containing the custom facet
>>> const std::locale locale(std::cout.getloc(),punct);

>>
>>> std::cout.imbue(locale);
>>> std::cout<< "Val: "<< 1234556<< std::endl;
>>> std::cout<< "From String: "<< toStr(123456789,locale)<<
>>> std::endl;
>>> }

>>
>>> That thread then gets the James Kanze treatment, so read his prose to
>>> learn what horrors I just pasted in, untested!

>>
>> All of what you have written was somewhere in the background of my
>> "Nothing straight out of the standard C++ box" reply.
>>
>> It's far easier to lookup an ad-hoc function or to write a simple one
>> instead of delving into the complexity of assembling your own locale,
>> for such a simple task.
>>
>> Granted, if one is accustomed to deal with them, one can come up with
>> something like the above straight from the top of one's head.
>>
>> As I see it, it just feels like shooting flies with a cannon, with the
>> further downside that I need to assemble it.

>
> Usually you have that cannon constructed already if you work for shop
> that writes C++ for international market. The problem is heavily
> localization related. For you "1,234,567.89" is may be natural so you
> think it is a "fly" to achieve it. German-speaking nations often like
> it other way around, like "1.234.567,89". Some Hindi-speaking guy does
> not understand thousand separator, for them there is hundreds
> separators. So the solution to have same number readable for all of
> them is to have locales, or what else you suggest to do? Hack it ad-
> hoc until it is unreadable blob of crap?


Of course not. Using std::locale or an existing in-house version of them
is the way to go in such cases.

It all depends on the target one wants to achieve.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      08-22-2010
On 22 aug, 19:16, "Francesco S. Carta" <entul...@gmail.com> wrote:
> Öö Tiib <oot...@hot.ee>, on 22/08/2010 09:09:15, wrote:
>
>
>
> > On 22 aug, 13:41, "Francesco S. Carta"<entul...@gmail.com> *wrote:
> >> Phlip<phlip2...@gmail.com>, on 21/08/2010 21:14:31, wrote:

>
> >>> Francesco S. Carta wrote:

>
> >>>>> Hi all
> >>>>> is there a function for converting a number such as 1234567.89 into a
> >>>>> string presentation as "1,234,567.89" for printing? Thanks for any
> >>>>> hints!
> >>>>> kwwang

>
> >>>> Nothing straight out of the standard C++ box.

>
> >>> This is one of those perennial FAQs that everyone needs and nobody
> >>> covers. The locale/facets system in iostreams is apparently a kit for
> >>> you to assemble.

>
> >>> The ultimate problem appears to be some undiscovered culture somewhere
> >>> might separate hundreds, or ten-thousands, so a locale cannot simply
> >>> assume thousands and then ask what delimiter goes there!

>
> >>>http://www.velocityreviews.com/forum...commas-to-int-....

>
> >>> #include<sstream>
> >>> #include<iostream>
> >>> #include<locale>
> >>> #include<string>

>
> >>> // custom numeric punctuation facet
> >>> struct Punct: std::numpunct<char>
> >>> {
> >>> char do_thousands_sep () const
> >>> {
> >>> return ',';
> >>> }
> >>> std::string do_grouping () const
> >>> {
> >>> return "\3";
> >>> }
> >>> };

>
> >>> std::string toStr(int x,std::locale const& * *l)
> >>> {
> >>> std::stringstream stream;
> >>> stream.imbue(l);

>
> >>> stream<< * *x;
> >>> return stream.str();
> >>> }

>
> >>> int main()
> >>> {
> >>> // construct a custom punctuation facet
> >>> std::numpunct<char>* punct = new Punct;

>
> >>> // construct a locale containing the custom facet
> >>> const std::locale locale(std::cout.getloc(),punct);

>
> >>> std::cout.imbue(locale);
> >>> std::cout<< * *"Val: "<< * *1234556<< * *std::endl;
> >>> std::cout<< * *"From String: "<< * *toStr(123456789,locale)<<
> >>> std::endl;
> >>> }

>
> >>> That thread then gets the James Kanze treatment, so read his prose to
> >>> learn what horrors I just pasted in, untested!

>
> >> All of what you have written was somewhere in the background of my
> >> "Nothing straight out of the standard C++ box" reply.

>
> >> It's far easier to lookup an ad-hoc function or to write a simple one
> >> instead of delving into the complexity of assembling your own locale,
> >> for such a simple task.

>
> >> Granted, if one is accustomed to deal with them, one can come up with
> >> something like the above straight from the top of one's head.

>
> >> As I see it, it just feels like shooting flies with a cannon, with the
> >> further downside that I need to assemble it.

>
> > Usually you have that cannon constructed already if you work for shop
> > that writes C++ for international market. The problem is heavily
> > localization related. For you "1,234,567.89" is may be natural so you
> > think it is a "fly" to achieve it. German-speaking nations often like
> > it other way around, like "1.234.567,89". Some Hindi-speaking guy does
> > not understand thousand separator, for them there is hundreds
> > separators. So the solution to have same number readable for all of
> > them is to have locales, or what else you suggest to do? Hack it ad-
> > hoc until it is unreadable blob of crap?

>
> Of course not. Using std::locale or an existing in-house version of them
> is the way to go in such cases.
>
> It all depends on the target one wants to achieve.


Yes and target is to format the output *exactly* with all
peculiarities like expected. And these peculiarities are: notation?,
decimal point?, precision?, padding?, thousands or hundreds
separation?, with what separator?, plus sign for positives?,
parentheses around negatives? and so on just name it. Been a bit
around in industry you notice that these damn peculiarities will
change immediately when your bosses sell it not only to neighbour of
yours but few blocks farther too. And then you see how sweet is to
just use locale as a configuration dedicated for next set of poor
fatsos who bought it. There are lot more interesting things to hack
than numeric formats, really.
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-22-2010
Öö Tiib <>, on 22/08/2010 10:06:08, wrote:

> On 22 aug, 19:16, "Francesco S. Carta"<entul...@gmail.com> wrote:
>> Öö Tiib<oot...@hot.ee>, on 22/08/2010 09:09:15, wrote:
>>
>>
>>
>>> On 22 aug, 13:41, "Francesco S. Carta"<entul...@gmail.com> wrote:
>>>> Phlip<phlip2...@gmail.com>, on 21/08/2010 21:14:31, wrote:

>>
>>>>> Francesco S. Carta wrote:

>>
>>>>>>> Hi all
>>>>>>> is there a function for converting a number such as 1234567.89 into a
>>>>>>> string presentation as "1,234,567.89" for printing? Thanks for any
>>>>>>> hints!
>>>>>>> kwwang

>>
>>>>>> Nothing straight out of the standard C++ box.

>>
>>>>> This is one of those perennial FAQs that everyone needs and nobody
>>>>> covers. The locale/facets system in iostreams is apparently a kit for
>>>>> you to assemble.

>>
>>>>> The ultimate problem appears to be some undiscovered culture somewhere
>>>>> might separate hundreds, or ten-thousands, so a locale cannot simply
>>>>> assume thousands and then ask what delimiter goes there!

>>
>>>>> http://www.velocityreviews.com/forum...commas-to-int-...

>>
>>>>> #include<sstream>
>>>>> #include<iostream>
>>>>> #include<locale>
>>>>> #include<string>

>>
>>>>> // custom numeric punctuation facet
>>>>> struct Punct: std::numpunct<char>
>>>>> {
>>>>> char do_thousands_sep () const
>>>>> {
>>>>> return ',';
>>>>> }
>>>>> std::string do_grouping () const
>>>>> {
>>>>> return "\3";
>>>>> }
>>>>> };

>>
>>>>> std::string toStr(int x,std::locale const& l)
>>>>> {
>>>>> std::stringstream stream;
>>>>> stream.imbue(l);

>>
>>>>> stream<< x;
>>>>> return stream.str();
>>>>> }

>>
>>>>> int main()
>>>>> {
>>>>> // construct a custom punctuation facet
>>>>> std::numpunct<char>* punct = new Punct;

>>
>>>>> // construct a locale containing the custom facet
>>>>> const std::locale locale(std::cout.getloc(),punct);

>>
>>>>> std::cout.imbue(locale);
>>>>> std::cout<< "Val: "<< 1234556<< std::endl;
>>>>> std::cout<< "From String: "<< toStr(123456789,locale)<<
>>>>> std::endl;
>>>>> }

>>
>>>>> That thread then gets the James Kanze treatment, so read his prose to
>>>>> learn what horrors I just pasted in, untested!

>>
>>>> All of what you have written was somewhere in the background of my
>>>> "Nothing straight out of the standard C++ box" reply.

>>
>>>> It's far easier to lookup an ad-hoc function or to write a simple one
>>>> instead of delving into the complexity of assembling your own locale,
>>>> for such a simple task.

>>
>>>> Granted, if one is accustomed to deal with them, one can come up with
>>>> something like the above straight from the top of one's head.

>>
>>>> As I see it, it just feels like shooting flies with a cannon, with the
>>>> further downside that I need to assemble it.

>>
>>> Usually you have that cannon constructed already if you work for shop
>>> that writes C++ for international market. The problem is heavily
>>> localization related. For you "1,234,567.89" is may be natural so you
>>> think it is a "fly" to achieve it. German-speaking nations often like
>>> it other way around, like "1.234.567,89". Some Hindi-speaking guy does
>>> not understand thousand separator, for them there is hundreds
>>> separators. So the solution to have same number readable for all of
>>> them is to have locales, or what else you suggest to do? Hack it ad-
>>> hoc until it is unreadable blob of crap?

>>
>> Of course not. Using std::locale or an existing in-house version of them
>> is the way to go in such cases.
>>
>> It all depends on the target one wants to achieve.

>
> Yes and target is to format the output *exactly* with all
> peculiarities like expected.


No, the target was just to format a number following the example given
by the OP. Don't take it for pedantry or for playing word games, all you
are trying to do with me amounts to breaking through an open door: I'll
repeat it, if one needs to take care of all those details, then
std::locale is the way to go, I fully agree with you.

The next time I'll take care of mentioning locales in my replies just to
avoid this kind of discussions, although I'm very well accustomed to see
a question replied by several people from different points of view, to
the advantage of the OP.

> And these peculiarities are: notation?,
> decimal point?, precision?, padding?, thousands or hundreds
> separation?, with what separator?, plus sign for positives?,
> parentheses around negatives? and so on just name it. Been a bit
> around in industry you notice that these damn peculiarities will
> change immediately when your bosses sell it not only to neighbour of
> yours but few blocks farther too. And then you see how sweet is to
> just use locale as a configuration dedicated for next set of poor
> fatsos who bought it. There are lot more interesting things to hack
> than numeric formats, really.


Nothing new under my sky, and the same surely stands under yours. But
what about the OP? If one comes here asking for a formatting function,
chances are that one might learn something by hacking numeric formats,
as you pose it.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments
 



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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57