C++ String转Double详解教程

发布时间:2023-05-16

一、C++ String转Double并保留两位小数

C++中可以使用atof()函数将字符串转为double类型,同时调用流输入输出库函数,可以精简代码,实现字符串转double。以下是代码示例:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
double string_to_double(string str) {
   double d;
   stringstream ss;
   ss << str;
   ss >> d;
   return d;
}
int main() {
   string s = "123.456";
   double d = string_to_double(s);
   cout << fixed << setprecision(2) << d << endl; //保留两位小数
   return 0;
}

二、C++ String转Double(C#)

在使用C#中,字符串转Double可以使用Parse方法,可以看作是C#方法的一种基础方法。以下是代码示例:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main(void){
   string s="123.456";
   double d=stod(s);
   system("pause");
   return 0;
}

三、C++ String转Double

在C++中,通过atof函数,可以实现字符串转Double类型。以下是代码示例:

#include <iostream>
#include <cstdlib>
using namespace std;
int main(void){
   char str[] ="123.456";
   double d = atof(str);
   cout << "转换后的数为:"  <<d<<endl; 
   return 0;
}

四、C++ String转Double小数点

如果要将字符串转换成double类型的变量,需要先判断字符串中是否有小数点,如果有则进行转换,如果没有,则字符串需要补充小数点再进行转换。

#include <iostream>
#include <cstdlib>
using namespace std;
int main(void){
   char str[] ="123456";
   int flag = 0;
   for(int i=0;i<strlen(str);i++){
      if(str[i] == '.') //如果有小数点,flag设置为1
         flag = 1;
   }
   double d;
   if(flag == 1){
      d = atof(str);
   }else{
      //如果没有小数点,需要在字符串补充小数点再转换
      char str_c[50];
      sprintf(str_c,"%.2f",(double)atof(str)/100);
      d = atof(str_c);
   }
   cout<< "转换后的数为:" << d<<endl; 
   return 0;
}

五、C++ String转Int

C++中可以使用stoi函数将字符串转为int类型。以下是代码示例:

#include <iostream>
#include <string>
using namespace std;
int main() {
   string s = "123";
   int i = stoi(s);
   cout << i << endl;
   return 0;
}

六、String转Date

C++中没有自带的日期转换函数,可以使用第三方库date.h来实现,安装方法:pip install date。以下是代码示例:

#include <iostream>
#include <date/date.h>
using namespace std;
int main() {
   date::sys_seconds s {1234567890s};
   auto const tp = date::floor<date::days>(s);
   return static_cast<int>(date::day_of_week(tp)); //返回日期的星期数
}

七、String转Long

在C++中,将String转换为long类型时,可以使用stol函数。以下是代码示例:

#include <iostream>
#include <string>
using namespace std;
int main(){
   string s="123456789";
   long l=stol(s);
   cout<<"转换后的数为: "<<l<<endl; 
   return 0;
}

八、C++ String转数组

在C++中,将String转换为数组时,可以使用split函数,将String对象的部分进行分离。以下是代码示例:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> split(string str, char delimiter) {
   vector<string> v;
   string s = "";
   for (int i = 0; i < str.length(); i++) {
      if (str[i] == delimiter) {
         v.push_back(s);
         s = "";
      } else {
         s += str[i];
      }
   }
   v.push_back(s);
   return v;
}
int main() {
   string s = "A,B,C,D,E";
   vector<string> v = split(s, ',');
   for (int i = 0; i < v.size(); i++) {
      cout << v[i] << endl;
   }
   return 0;
}

九、Cint转String

在C++中,将int转换为String时,可以使用to_string函数。以下是代码示例:

#include <iostream>
#include <string>
using namespace std;
int main() {
   int i = 123456;
   string s = to_string(i);
   cout << s << endl;
   return 0;
}

总之,在C++中,有多种方法可以将String转换为Double类型、Int类型或其他类型,根据实际需求,选择最适合的方法可以简化代码,提高效率。