c语⾔字符串转化
基础
1. 这些函数都在表头⽂件 #include<stdlib.h>中
⽅法
atof(将字符串转换成浮点型数)
double atof(const char *nptr) 将字符串转换为浮点数
strtod(将字符串转换成浮点数)
定义函数 double strtod(const char *nptr,char **endptr);
#include<stdlib.h>
#include <stdio.h>
main()
{
char*a =" 100.23";
float b =atof(a);
printf("a = %.2f\n", b);
system("pause");
}
atoi将字符串转换成整型数
定义函数 int atoi(const char *nptr);
c++中string的用法#include<stdlib.h>
#include <stdio.h>
main()
{
char a[]=" - 100";
int c =atoi(a);
printf("c = %d\n", c);
system("pause");
}
atol(将字符串转换成长整型数)
定义函数 long atol(const char *nptr);
strtol(将字符串转换成长整型数)
定义函数 long int strtol(const char *nptr,char **endptr,int base);
strtoul(将字符串转换成⽆符号长整型数)
定义函数 unsigned long int strtoul(const char *nptr,char **endptr,int base);
ltoa 把⼀长整形转换为字符串
⽤ 法:char *ltoa(long value, char *string, int radix);
ultoa:把⼀⽆符号长整形转换为字符串
⽤ 法:char *ultoa(unsigned long value, char *string, int radix);
gcvt(将浮点型数转换为字符串,取四舍五⼊)
表头⽂件 #include<stdlib.h>
ecvt将双精度浮点型数转换为字符串,转换结果中不包括⼗进制⼩数点
char *ecvt(double value, int ndigit, int *decpt, int *sign);
fcvt指定位数为转换精度,其余同ecvt
⽤ 法:char *fcvt(double value, int ndigit, int *decpt, int *sign);
toascii(将整型数转换成合法的ASCII 码字符)定义函数 int toascii(int c)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论