sprintf 中如何在打印数字时加入逗号,像银行的那种如123,456,789.65

SpeedChina 2006-04-11 01:39:31
sprintf 中如何在打印数字是加入逗号,像银行的那种如123,456,789.65

char sz[128];
int a = 123456789;
sprintf (sz, "????????????", a)

问号中该如何填写啊??????
...全文
1241 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
huguyue6670 2006-04-12
  • 打赏
  • 举报
回复
那数字自己谢函数吧 否则sprintf();没见过由这个功能。
居易锦风 2006-04-12
  • 打赏
  • 举报
回复
用format吧
http://www.cublog.cn/u/15807/?u=http://www.cublog.cn/u/15807/showart.php?id=86991
nkwesley 2006-04-12
  • 打赏
  • 举报
回复
这些方法都不错,其实只要实现就好,效率不是很重要
Juchiyufei 2006-04-11
  • 打赏
  • 举报
回复
关住一下。
noneone 2006-04-11
  • 打赏
  • 举报
回复
学习
SpeedChina 2006-04-11
  • 打赏
  • 举报
回复
模糊中记得此前是用过此格式的,要不怎么Windows显示文件字节的时候都加了逗号,当时的印象大概是什么print("%###,###,###d",nNumber)之类的东西,但是十几年了,已经无从查考当时是不是写了类似的函数。
感谢各位的关注。
此帖稍留数日,分数还是要派发出去的。
handsomerun 2006-04-11
  • 打赏
  • 举报
回复
没有现成的函数可以用
得自己写
要么像鹦鹉说得那样,或者
你先把这个整数转成一个字符窜,然后对这个字符篡进行操作,
快乐鹦鹉 2006-04-11
  • 打赏
  • 举报
回复
自己封装一个函数好了。对需要转换的整数,每次除以1000进行循环,直到商为0为止。每次将余数转换为字符串,前面增加逗号就可以了。当然,最后一个数前不需要加逗号。
蒋晟 2006-04-11
  • 打赏
  • 举报
回复
There is not a function within the standard library to handle this.
Formatting currency strings is a rather complicated thing to do correctly
due to the format differing wildly from one locale to another. Some
operating systems provide functions to format currencies based on locale
but that is a little OT. If you are doing a platform specific thing then
ask some of the gurus in the newsgroup for your platform. Here is a general
function that formats unsigned numbers based on grouping and a separator.
It could be extended to handle currencies pretty easily; however, if you
have to handle localized currencies, then read your documentation on
setlocale() and localeconv(). These standard functions will provide you
with enough information to format currencies, numbers, dates, and times
with respect to a specific locale.

Anyway, here is a function that will format numbers into a buffer for
you.


void
number_to_buffer (unsigned long n, char *buffer, int digits_per_segment,
char separator)
{
unsigned long multiple; /* the multiple of 10 to use */
long int cursegment; /* largest power of multiple < n */
char *p; /* insertion pointer into buffer */
long int digits; /* current segment */
int nsegments; /* number of three digit segments */
char formatstr[32]; /* the format string for each segment */


/* Set up our parameters. */
p = buffer;
*p = '\0';
multiple = pow(10, digits_per_segment);


/* Set up a printf-style format string to handle zero padding
* each segment (i.e., formatstr = "%03d").
*/
sprintf(formatstr, "%%0%dd", digits_per_segment);


/* Figure out how many segments we have in n. */
nsegments = ((int)log10((double)n)) / digits_per_segment;


if (nsegments > 0) {
/*
* `n'' requires more than one segment, so handle each
* segment separately within a loop. We have to handle the
* first segment separately since it doesn't require zero
* padding. We also prime ``cursegment'' as the highest
* power of ``multiple'' that is less than or equal to
* ``n'' here.
*/
cursegment = pow(multiple, nsegments);
digits = n / cursegment;
p += sprintf(p, "%ld", digits);
*p++ = separator;
n -= (digits * cursegment);
cursegment /= multiple;
--nsegments;


/* Spin for the remaining powers of 1000 within n. */
while (nsegments != 0) {
digits = n / cursegment;
p += sprintf(p, formatstr, digits);
*p++ = separator;
n -= (digits * cursegment);
cursegment /= multiple;
--nsegments;
}


/* Place the final trailing digits on. */
sprintf(p, formatstr, n);
} else {
/* n only requires one segment so no leading zeros are
* necessary.
*/
sprintf(p, "%ld", n);
}



}


It isn't exactly the safest function in the world since it will gladly
clobber buffers that are too short, but it does the job. Here is a little
example of using it:

number_to_buffer(12012012UL, buffer, 3, ',');
printf("%s\n", buffer);
/* prints 12,012,012 */


You'll have to extend it to handle negative numbers (if necessary). Also
note that -1.23 dollars is usually displayed as ``($1.23)'' as opposed
to ``-$1.23''. I guess the handling will depend on how technically correct
you have to be.


On Windows, you can also use GetNumberFormat to format numbers

TCHAR szBuff[20];
TCHAR szNum[] = "10000";
NUMBERFMT nf;
nf.NumDigits = 0; // no decimal
nf.LeadingZero = 0;
nf.Grouping = 3;
nf.lpDecimalSep = ".";
nf.lpThousandSep = ","; // Thousand separator
nf.NegativeOrder = 0; // This will display as (10,000) if it is a -ve
value. See LCTYPE constants for more info.


int iRet = GetNumberFormat(NULL, 0, szNum, &nf, szBuff, sizeof(szBuff) );
TRACE("Return Value : %d Buffer: %s\n", iRet, szBuff);


You can use the GetLocaleInfo API to determine the thousand separator.
TCHAR szThousandSep[10];
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, szThousandSep,
sizeof(szThousandSep));
zhucde 2006-04-11
  • 打赏
  • 举报
回复
一个号称是垃圾的方法:


char *Addd(char *sz)
{
int i=0;
while(sz[i]!='\0')
i++;
int y=i%3;
int x=1,k=0;

char dz[256];

bool b=0;
for(int j=0;j<i;j++)
{



if(y==0)
b=1;
else
if(j==y&&j>0)
{
k++;
dz[j]=',';
b=1;
}

dz[j+k]=sz[j];
if(b)
{

if(x==3)
{


x=0;
k++;
dz[j+k]=',';
}
x++;
}


}
char* c=strrchr(dz,',');
strcpy(c,"");

return dz;
}



调用 :
char sz[128];
int a = 23458901;
sprintf (sz, "%d", a);


char *db=Addd(sz);

MessageBox(db);
yuanyou 2006-04-11
  • 打赏
  • 举报
回复
mark
kugou123 2006-04-11
  • 打赏
  • 举报
回复
转换成字符串,然后根据位数,加“,”号。
xqk 2006-04-11
  • 打赏
  • 举报
回复
自已写函数吧

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧