Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 7th, 2006, 3:34 PM   #1
Robocop
Programmer
 
Join Date: Sep 2004
Posts: 50
Rep Power: 5 Robocop is on a distinguished road
Showing Comma

I want to get a float number obtained from an operation to show the comma for thousands, millions, etc. For example 12345.0 to 12,345.0

Is there a command like showcomma or something like that?

In VB you can just do Format(variable, "standard"), but what about C++?
Robocop is offline   Reply With Quote
Old Nov 7th, 2006, 3:40 PM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
for example if you were given the number 1500, and wanted to output 1500. i just did this on the fly so please do check over it.
if(num<=1000)
{
num=num / 1000;
out num;
out a comma;
num=num % 1000;
out num;
}

i think you need to something as similar as above..........you could use a switch statement instead of an if statement.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Nov 7th, 2006, 6:54 PM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>Is there a command like showcomma or something like that?
No, but it's easy to create your own facet that does precisely that:
#include <iostream>
#include <locale>

template <typename CharT>
class numfmt: public std::numpunct<CharT> {
  CharT sep_; // Character to separate groups
  int group_; // Number of characters in a group
public:
  numfmt ( CharT sep, int group ): sep_ ( sep ), group_ ( group ) { }
private:
  CharT do_thousands_sep() const { return sep_; }
  std::string do_grouping() const { return std::string ( 1, group_ ); }
};

int main()
{
  numfmt<char> *fmt = new numfmt<char> ( ',', 3 );

  std::cout.imbue ( std::locale ( std::locale(), fmt ) );

  for ( double n = 0, i = 1; n < 10; n++, i *= 10 )
    std::cout<< std::fixed << i <<'\n';
}
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Nov 8th, 2006, 1:00 PM   #4
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Wow that was a cool trick, that looks much easier to do than it would be in C. To bad its pretty obscure, I need a good resource for all these odd corners of the STL.
Game_Ender is offline   Reply With Quote
Old Nov 8th, 2006, 1:59 PM   #5
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Game_Ender View Post
Wow that was a cool trick, that looks much easier to do than it would be in C. To bad its pretty obscure, I need a good resource for all these odd corners of the STL.
The "odd corners " are explained in "The C++ Standard Library: A Tutorial and Reference". Specifically chapter 13.8 and the whole of chapter 14 in this case.

http://www.amazon.com/C++-Standard-L.../dp/0201379260
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML Tables -- Only One Border Showing Intimidat0r HTML / XHTML / CSS 10 Apr 5th, 2006 7:45 AM
Problem with showing error xavier ASP 4 Oct 15th, 2005 4:39 AM
Selection Not Showing In Menu B3TA_SCR1PT3R Other Scripting Languages 1 Aug 1st, 2005 10:53 PM
Showing more digits BebopFusion C 4 Jul 15th, 2005 4:57 PM
showing line numbers Chuiu C 5 May 7th, 2005 8:44 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC