如何将字符串转换为长双精度
How to convert a string into a long double?
首先,我已经彻底检查了关于这件事的其他问题,但没有任何帮助。我需要将字符串转换为长双精度类型,但没有什么对我有用。
string a="634.232";
long double x;
x=strtold(a.c_str(),0);
例如:这个"strtelled"将这个输入更改为这个输出。
检查错误:
#include <cerrno>
#include <cstdlib>
#include <iostream>
const char str[] = "634.232";
int main()
{
char * e;
errno = 0;
long double val = std::strtold(str, &e);
if (*e != ' ' || errno != 0) { /* error */ std::abort(); }
std::cout << "SUccessfully parsed: " << val;
}
请注意,字符串解析可能会以多种方式失败:字符串可能不代表数字,也可能不代表整个数字,或者它所代表的数字可能太大,无法适应数据类型。你必须检查所有这些可能性,这就是这个代码正在做的。结束指针e
检查我们是否已经使用了整个字符串,而errno
检查转换是否成功。
strtold
应该可以正常工作,正如KerrekSB所建议的那样。
但当你检查你的工作时,我建议你使用stold
:http://www.cplusplus.com/reference/string/stold/
string a{"634.232"};
long double x{stold(a)};
相关文章:
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 在 c++ 中从双精度转换为字符串
- 使用 sprintf 和 %g 将双精度转换为字符串的意外结果
- 从双精度转换为整数的显式类型是否始终检查整数溢出?
- C++,遇到将双精度转换为整数的问题
- 在C++中将双精度转换为intptr_t
- 将大双精度转换为科学记数法以用作字符串?
- 了解双精度转换与整数转换中的整数与截断关系
- 将 OLE 自动化日期 (OADate) 双精度转换为结构 tm,而不使用 VariantTime到SystemTime
- 无法在赋值中将"双精度 (*)[4]"转换为"双精度**"
- 将大双精度转换为无符号 int 期间堆栈损坏
- 将双精度转换为本地8位
- 警告从"双精度"转换为"int"
- 如何将双精度转换为浮点指针
- 将双精度转换为整数时出错
- 使用并集将双精度转换为十六进制再转换为双精度
- 是否有可以将每个双精度转换为唯一uint64_t,保持精度和顺序的功能?(为什么我找不到?
- 使用定点表示法将双精度转换为字符串,没有尾随零和机智的冲刺
- 在C++中将双精度转换为字符*/字符串
- 无法将双精度 [] [] 转换为双精度 **