C语言 数与串之间转换的方法
3388 点击·0 回帖
![]() | ![]() | |
![]() | C语言 数与串之间转换的方法,需要的朋友可以参考一下 整数转换为字符串:char *itoa( int value, char *string,int radix); 小数转换为字符串:sprintf(串, 格式控制符列, 数据); 字符串转小数:double atof(const char *nptr); 字符串转整数:int atoi(const char *nptr); 测试代码:
#include<stdio.h> #include<stdlib.h> int main() { int a=2013420; float b=2.054f; double c=5.24; char sa[20],sb[20],sc[20]; //将整数a转换为字符串 itoa(a,sa,10); puts(sa); //将浮点型数据转换为字符串 sprintf(sb,"%g",b); puts(sb); //将double型数据转换为字符串 sprintf(sc,"%lg",c); puts(sc); printf("========以下是串转换为数值=========n"); char *s1="123",*s2="1.23"; printf("%dn",atoi(s1)); printf("%gn",atof(s2)); getchar(); return 0; } | |
![]() | ![]() |