标签:src printf oid 字符串 位长 har for dbi 转换
/*输入值src:数字字符串,以分为单位
*输出值des:数字字符查,以元为单位,并用千分位分隔
*/
void amountofthousandBits(char src[], int srclen, char des[])
{
int iLoop=0;
int k=0;
int reallen = -1;
char tmpsrc[16];
memset(tmpsrc, 0x00, sizeof(tmpsrc));
k=srclen;
/* 计算有效长度 */
for(iLoop=0; iLoop<srclen; iLoop++)
{
if(src[iLoop] != ‘0‘)
{
k =iLoop;
break;
}
}
/* 去掉0的长度 */
reallen = srclen-k;
int m=0;
for(m=0;m<reallen;m++)
{
tmpsrc[m] = src[k+m];
}
if(reallen == 0)
{
strcpy(des, "0.00");
}
if(reallen == 1)
{
strcpy(des, "0.0");
strncat(des, &tmpsrc[reallen-1],1);
}
if(reallen == 2)
{
strcpy(des,"0.");
strncat(des, &tmpsrc[reallen-2],1);
strncat(des, &tmpsrc[reallen-1],1);
}
if(reallen>=3 && reallen<=5)
{
for(k=0;k<reallen;k++){
if(k==0)
{
strncpy(des,&tmpsrc[k],1);
}else
{
if(k==reallen-2)
{
strcat(des,".");
strncat(des,&tmpsrc[k],1);
}else
{
strncat(des,&tmpsrc[k],1);
}
}
}
}
if(reallen>5)
{
/* 需要加千分位整数位长度 */
int tmpData=0;
tmpData = reallen-2;
/* 判断与3整除余数 */
for(k=0;k<tmpData;k++)
{
if(k==0)
{
strncpy(des, &tmpsrc[k], 1);
}
else
{
if((tmpData-k)%3 == 0)
{
strncat(des,",",1);
strncat(des,&tmpsrc[k],1);
}
else
{
strncat(des,&tmpsrc[k],1);
}
}
}
strncat(des,".",1);
strncat(des,&tmpsrc[reallen-2],1);
strncat(des,&tmpsrc[reallen-1],1);
}
return ;
}
例子:
int main()
{
char src[20]="000100000000001";
char des[1024];
memset(des, 0x00, sizeof(des));
amountofthousandBits(src, strlen(src), des);
printf("src=%s, des=%s\n", src,des);
return 0;
}
标签:src printf oid 字符串 位长 har for dbi 转换
原文地址:https://www.cnblogs.com/Yloon/p/13828658.html