C/C++中字符串與數(shù)字相互轉(zhuǎn)換
悅辰
首先我們找到Visual C++ 6.0,鼠標(biāo)雙擊,點(diǎn)開(kāi)
接著,輸入頭文件:
# include<stdlib.h>
# include<stdio.h>
我們調(diào)用itoa()函數(shù)把把整數(shù)num_int轉(zhuǎn)成字符串str_int
調(diào)用gcvt()函數(shù)把浮點(diǎn)數(shù)num_dounle轉(zhuǎn)化成字符串str_double
程序如下:
int main(void)
{int num_int=200;
double num_double=425.10f;
char str_int[30];
char str_double[30];
itoa(num_int,str_int,10);
gcvt(num_double,8,str_double);
最后我們輸出字符串str_int和字符串str_double,程序運(yùn)行,如圖所示
printf("str_int:%sn",str_int);
printf("str_double:%sn",str_double);
return 0;
}