博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
char*,string,float,int 转换
阅读量:6965 次
发布时间:2019-06-27

本文共 1888 字,大约阅读时间需要 6 分钟。

char* 转 float:

double atof (const char* str);
/* atof example: sine calculator */#include 
/* printf, fgets */#include
/* atof */#include
/* sin */int main (){ double n,m; double pi=3.1415926535; char buffer[256]; printf ("Enter degrees: "); fgets (buffer,256,stdin); n = atof (buffer); m = sin (n*pi/180); printf ("The sine of %f degrees is %f\n" , n, m); return 0;}

string 转 float;

float stof (const string&  str, size_t* idx = 0);float stof (const wstring& str, size_t* idx = 0);
#include 
// std::cout#include
// std::string, std::stofint main (){ std::string orbits ("686.97 365.24"); std::string::size_type sz; // alias of size_t float mars = std::stof (orbits,&sz); float earth = std::stof (orbits.substr(sz)); std::cout << "One martian year takes " << (mars/earth) << " Earth years.\n"; return 0;}

相关有stoi,stod,stold,以及strtod

由数值转向string:

函数原型:

string to_string (int val);string to_string (long val);string to_string (long long val);string to_string (unsigned val);string to_string (unsigned long val);string to_string (unsigned long long val);string to_string (float val);string to_string (double val);string to_string (long double val);

举例

// to_string example#include 
// std::cout#include
// std::string, std::to_stringint main (){ std::string pi = "pi is " + std::to_string(3.1415926); std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number"; std::cout << pi << '\n'; std::cout << perfect << '\n'; return 0;}

 

 但是在 某些编译器里 to_string 不被支持。http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-so-g

解决方法:

http://stackoverflow.com/questions/11063413/erroritoa-was-not-declared-in-this-scope

转载于:https://www.cnblogs.com/xaf-dfg/p/3699256.html

你可能感兴趣的文章
docker
查看>>
Date13
查看>>
HTML5新增了哪些标签
查看>>
网站DDOS***防护实战老男孩经验心得分享
查看>>
X86服务器虚拟化的资源划分和性能优化
查看>>
2015年11月 广州深圳 MVP 线下活动
查看>>
⑨①-成功者的路永远都是相通的
查看>>
一次DPM备份Exchange DAG的故障处理过程
查看>>
Docker视频发布
查看>>
利用Powershell查询AD中账号属性
查看>>
office2003/2007/2010版本降低宏安全设置方法
查看>>
高性能的MySQL(7)字符集和校对
查看>>
[Node] 重要外部模块
查看>>
设计先行,编码在后
查看>>
项目视频光盘项目中所学概览-html5+批处理+bat转exe
查看>>
javascript pattern
查看>>
将行政区域导入SQL SERVER
查看>>
好书记录
查看>>
Linux系统下查看目录大小
查看>>
对称加密DES和TripleDES
查看>>