如何在 C/C++ 中将字符串转换为双精度?
这里有一个将字符串转换为双精度的示例。
示例
#include <iostream>
using namespace std;
int main() {
char s[20] = "18.2894 is a number";
char *p;
double result;
result = strtod(s, &p);
cout<<"The number after conversion of string : "<<result;
return(0);
}输出
The number after conversion of string : 18.289400
在上面的程序中,声明了一个类型为 char 的数组 s[20],并将其初始化为字母数字字符。函数 strtod() 用于将该字符串转换为一个双精度数字。
char s[20] = "18.2894 is a number"; char *p; double result; result = strtod(s, &p);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP