题目
如题,假设数据为:63.2558的string类型,如何把它变为double型
处理办法
直接细节入手,前面先找到前置位,后面找到后置位,都是一些普通的办法,然后把它一起加起来,最后就得到了最后的结果
代码
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
double StringToDouble(string str){
double num_front = 0,num_end=0;
int temp;
for(int i = 0;i<str.length();i++){
if(str[i]=='.'){
temp = i;
break;
}
num_front*=10;
num_front+=(str[i]-'0');
}
int count = 1;
cout<<temp<<endl;
for(i = temp+1;i<str.length();i++){
num_end+=(str[i]-'0')/(pow(10,count));
count++;
}
num_front+=num_end;
return num_front;
}
int main(){
string a = "63.2558";
cout<<StringToDouble(a);
}
输出
最后输出结果为63.2558