如何显示12345为12,345 ?? [英] how to display 12345 as 12,345 ??

查看:93
本文介绍了如何显示12345为12,345 ??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:


int price1 = 35000;

int price2 = 600;

int price3 = 8765;

int price4 = 120000;

int price5 = 3800000;


我想输出以屏蔽以下内容:


35,000

600

8,765

120,000

3,800,000

我该怎么办?有什么好的功能吗?

解决方案

U?ytkownik" news.hku.hk" <双****** @ hkusua.hku.hk> napisa3 w wiadomo?ci

news:40 ****** @ newsgate.hku.hk ...

假设我有:
int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

我想输出屏幕以下内容:

35,000
600
8,765
120,000
3,800,000

我应该怎么做做??有什么好的功能吗??




不在标准中。你必须自己做。我相信它很简单。


问候,

Marcin


" news.hku.hk" <双****** @ hkusua.hku.hk>在留言中写道

news:40 ****** @ newsgate.hku.hk ...

假设我有:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

和我想输出屏幕如下:

35,000
600
8,765
120,000
3,800,000

我该怎么办? ?有什么好的功能吗??




你可以查看方面,看看Dinkumware。




我以前做过这个。我已经针对这篇文章修改了它,但是

在这里(在你使用之前检查bug!):

char szBuffer [30]; //全局变量


char * GenerateString(unsigned long);


int main(void)

{

cout<< GenerateString(12345);

}

char * GenerateString(unsigned long Numbr)

{

szBuffer [29 ] =''\''';


char * pChar =& m_szBuffer [28];


unsigned long char_counter = 0 ;

unsigned long comma_counter = 0;

unsigned long nResult; //将被初始化

unsigned long nTempValue = Numbr;


for(;;)

{

nResult = 0;


//我曾经使用过以下方法,但它的速度太快了

/ *

while(nTempValue> = 10)

{

nTempValue - = 10;

nResult + = 1;

} * /


nResult = nTempValue / 10;

nTempValue%= 10;


* pChar =((''0'')+(nTempValue));

pChar - = 1;

char_counter + = 1;


nTempValue = nResult; //非常重要!


如果(!nTempValue)中断; //此行阻止,345生成

生成


char_counter + = 1;

if(comma_counter == 3)

{

* pChar ='','';

pChar - = 1;

char_counter + = 1;

comma_counter = 0;

}

} //结束For


返回pChar;


}

希望有所帮助。另外,你可以尝试将它合并到一个类中(我做了!)

并且还覆盖了ostream"<<<""运营商!那会很方便。我会发布

我为它写的课程,但它有一个非常具体的目的,而不仅仅是

生成这些字符串。

- JKop


suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??

解决方案

U?ytkownik "news.hku.hk" <bi******@hkusua.hku.hk> napisa3 w wiadomo?ci
news:40******@newsgate.hku.hk...

suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??



Not in the standard. You''ll have to do it yourself. I believe it is quite
simple.

regards,
Marcin


"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40******@newsgate.hku.hk...

suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??



You could look up facets, and check out Dinkumware.




I''ve done this before. I''ve adapted it for the purpose of this post, but
here goes (Check for bugs before you use it!):
char szBuffer[30]; //Global Variable

char* GenerateString(unsigned long);

int main(void)
{
cout << GenerateString(12345);
}
char* GenerateString(unsigned long Numbr)
{
szBuffer[29] = ''\0'';

char* pChar = &m_szBuffer[28];

unsigned long char_counter = 0;
unsigned long comma_counter = 0;
unsigned long nResult; //will be initialized
unsigned long nTempValue = Numbr;

for(;;)
{
nResult = 0;

//I used to use the method below, but it''s FAR too slow
/*
while ( nTempValue >= 10 )
{
nTempValue -= 10;
nResult += 1;
}*/

nResult = nTempValue / 10;
nTempValue %= 10;

*pChar = ( ( ''0'' ) + ( nTempValue ) );
pChar -= 1;
char_counter +=1;

nTempValue = nResult; //Very Important!

if (!nTempValue) break; //This line prevents ",345" being
generated

char_counter += 1;
if ( comma_counter == 3 )
{
*pChar = '','';
pChar -= 1;
char_counter += 1;
comma_counter = 0;
}
} //End of "For"

return pChar;

}
Hope that helps. Also, you may try incorporating it into a class ( I did! )
and also overide the ostream "<<" operator! That''d be dead handy. I''d post
the class I wrote for it but it had a very specific purpose other than just
genrating these strings.
-JKop


这篇关于如何显示12345为12,345 ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆