[C++]数值与字符串转换问题

时间:2023-03-10 01:46:26
[C++]数值与字符串转换问题
#include <stdio.h>//sprintf
#include <stdlib.h>//atof,atol,strtod,strtol
using namespace std; int main(){
//字符串转数值
printf("%f\n", atof("23"));//23.000000
printf("%d\n", atol("23"));//23
printf("%f\n", strtod("23.00", NULL));//23
printf("%d\n", strtol("23.00$$$$", NULL, 10));//23 //数值转字符串
char tmp[10];
int i = 67;
sprintf(tmp, "%d", 89);//将数值转字符串
printf("%s\n", tmp);//89
sscanf( tmp, "%d", &i ); // 将字符串转换成整数 i = 15
printf("%s-%d\n", tmp, i);//89-89 }