c++ 字符串与数字相互转换

日常编码过程中,字符串与数字进行相互转换是常见的需求,下面我们总结一下在c++中,字符串与数字的转换都是如何来进行操作的。

1.字符串转数字c语言风格

首先看看,在c语言的风格中,我们怎么做到把字符串转为数字。

void func1() {
    string s1 = "123";
    string s2 = "123.1";
    int i = atoi(s1.c_str());
    double d = atof(s2.c_str());
    cout<<i<<endl;
    cout<<d<<endl;
}

注意上述代码是在namespace为std的环境中运行。
atoi, atof函数,均在stdlib.h中进行了声明,在头文件中,其声明的函数原型为

double	 atof(const char *);
int	 atoi(const char *);
long	 atol(const char *);

不难看出,上面这些方法的输入均为const char *,即字符串,得到的输出为转化以后的各种数值类型。

123
123.1

2.字符串转数字c++风格

void func2() {
    string s1 = "456";
    string s2 = "456.2";
    int i = stoi(s1);
    long l = stol(s1);
    float f = stof(s2);
    double d = stod(s2);
    cout<<i<<endl;
    cout<<l<<endl;
    cout<<f<<endl;
    cout<<d<<endl;
}

上面代码也是在namespace为std的环境下运行

这些函数均位于string类中,函数签名原型为

_LIBCPP_FUNC_VIS int                stoi  (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS long               stol  (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS unsigned long      stoul (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS long long          stoll (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);

_LIBCPP_FUNC_VIS float       stof (const string& __str, size_t* __idx = 0);
_LIBCPP_FUNC_VIS double      stod (const string& __str, size_t* __idx = 0);
_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);

最后代码运行结果为

456
456
456.2
456.2

3.数字转字符串

上面是字符串转数字,接下来再看看数字如何转字符串。

void func3() {
    int i = 123;
    float f = 1.234;
    double d = 2.012;
    cout<<to_string(i)<<endl;
    cout<<to_string(f)<<endl;
    cout<<to_string(d)<<endl;
}

c++11以后,string类中提供了to_string方法,可以将各种类型数字转成字符串。
上面的代码输出结果为:

123
1.234000
2.012000

输出的结果貌似与我们预期有差异,很明显是与浮点数精度相关。

为了控制浮点数的精度,可以使用ostringstream来控制精度。

#include<sstream>
#include <iomanip>
using namespace std;

void func4() {
    double d = 3.14159265358979;
    cout << d << endl;
    stringstream ss;
    ss.precision(10);
    ss << d;
    cout << ss.str() << endl;
}

上面的代码运行,结果为

3.14159
3.141592654

主要是通过precison控制浮点数精度

  • 74
    点赞
  • 413
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,可以使用以下方法将字符串转换数字: 1. 使用库函数atoi():这个函数可以将字符串转换为整数。例如: ```c char str[] = "123"; int num = atoi(str); ``` 这样就将字符串"123"转换为整数123。 2. 使用库函数atof():这个函数可以将字符串转换为浮点数。例如: ```c char str[] = "3.14"; float num = atof(str); ``` 这样就将字符串"3.14"转换为浮点数3.14。 3. 使用库函数sscanf():这个函数可以根据指定的格式将字符串转换为特定类型的数据。例如: ```c char str[] = "456"; int num; sscanf(str, "%d", &num); ``` 这样就将字符串"456"转换为整数456,并存储在变量num中。 4. 使用库函数strtol():这个函数可以将字符串转换为长整数。例如: ```c char str[] = "789"; long num = strtol(str, NULL, 10); ``` 这样就将字符串"789"转换为长整数789。 以上是C语言中将字符串转换数字的几种常用方法。你可以根据需要选择合适的方法进行转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C++ 字符串数字之间的相互转换](https://blog.csdn.net/weixin_43390123/article/details/116094291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C++中将字符串转换数字](https://blog.csdn.net/qq_46906413/article/details/122824849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值