Does anyone have a simple c++ function that will format a number i.e. 1000 = 1,000 / 5000000 = 5,000,000 etc... ? I am new to c++ so if I need to include anything or add header files please let me know.
Thanks in advance!
Jas
Does anyone have a simple c++ function that will format a number i.e. 1000 = 1,000 / 5000000 = 5,000,000 etc... ? I am new to c++ so if I need to include anything or add header files please let me know.
Thanks in advance!
Jas
Convert the number to a (preferably C++) string using stringstreams, or any of the numerous other methods that will get mentioned. Then go through the string backwards (aka starting at the end), and every three characters insert a comma.
well that is what i tried but i couldn't get my head around the function so i was wondering if anyone had some sample code?
If you tried it, show us your code.
http://cboard.cprogramming.com/showthread.php?t=61043
My post in the above thread, #19, shows how to use a feature of the language (somewhat complex for beginners) that takes care of this for you. It would however be far easier for most to simply manipulate a string containing the value by inserting commas where appropriate as has already been suggested.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens