Computing.Net > Forums > Programming > Inserting commas into numbers

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Inserting commas into numbers

Reply to Message Icon

Name: leif
Date: July 23, 2003 at 13:41:58 Pacific
OS: na
CPU/Ram: na
Comment:

What's a good way to insert commas into numeric output in c++? For instance long l = 12300 would be output as 12,300.



Sponsored Link
Ads by Google

Response Number 1
Name: igork
Date: July 24, 2003 at 07:11:45 Pacific
Reply:

As far as I know, there is no standart way to do so. However you may try to implement a function that will do it for you.
I wonder,though, what will be its contribution compared to the effort you will
make to write it...


0

Response Number 2
Name: leif
Date: July 24, 2003 at 07:16:24 Pacific
Reply:

I have an assignment to write three output manipulators that take parameters. One of them is supposed to do the comma thing. The book I have for this class says parameterized manipulators are beyond it's scope, so now my greater problem is finding a reference for doing those.


0

Response Number 3
Name: igork
Date: July 24, 2003 at 07:45:00 Pacific
Reply:

So what is your actual problem?
Is it inserting commas, or writing parameterized manipulators ?


0

Response Number 4
Name: Khash
Date: July 24, 2003 at 11:07:43 Pacific
Reply:

I'm not sure if there is a ready to use function that would do the job for you, and if you get marks for this part of the program they don't want you to use such a function (if there is one).
So just try to write a simple function that uses divisions by 1000 to cut your number and add commas into it.


0

Response Number 5
Name: igork
Date: July 24, 2003 at 14:55:15 Pacific
Reply:

If you need more information about Manipulators just look in "C++ programming laguage, 3rd edition" by B.Stroustrup, pages 631 to 636.
Note: in other editions, the pages may differ.


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: July 24, 2003 at 15:43:14 Pacific
Reply:

I don't know c++ programming but I know it supports Regular Expressions. So, doing a substitution on a var might solve the comma insertion problem.

Here's 2 options using Perl syntax:

$x = 1234567890;

option 1,
$x =~ s/(?=\d)(?=(\d\d\d)+$)/,/g;

option 2
while ($x =~ s/(\d)((\d\d\d)+\b)/$1,$2/g) {};

In either case, $x is now 1,234,567,890

I borrowed these regexs from O’Reilly’s Mastering Regular Expressions by Jeffrey Friedl.

However, they only work with integers. If you need to work with real numbers, we’ll need to make some changes.


0

Response Number 7
Name: igork
Date: July 27, 2003 at 00:43:33 Pacific
Reply:

Sorry to disappoint you pal, but C++ does not support regular expressions...


0

Response Number 8
Name: iDadalex
Date: July 28, 2003 at 17:44:33 Pacific
Reply:

Best bet is to write a function which will insert a comma after every third integer space. Shouldnt be to hard. just some math.



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Inserting commas into numbers

Insert NULL into Pocket Access www.computing.net/answers/programming/insert-null-into-pocket-access/4197.html

cmd.exe alternative from JPSoft? www.computing.net/answers/programming/cmdexe-alternative-from-jpsoft/17245.html

htbml help www.computing.net/answers/programming/htbml-help/4019.html