![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2004
Posts: 50
Rep Power: 5
![]() |
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++? |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
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:
|
|
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#5 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Quote:
http://www.amazon.com/C++-Standard-L.../dp/0201379260 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |