0

我有一个文本文件,其中的科学计数法数字存储为一串字符,其中一些字符最多为20数字。例如,2.3456789e-015。我需要将其转换为 long double 以便对其执行一些数学运算。

我怎样才能做到这一点?

long double number = 2.3456789e-015;

我知道我可以像这样声明一个long double,但是当我将数字作为字符串并且需要分别指定基数和指数时,麻烦就来了。该pow()函数仅适用于双打cpowl()无法格式化为printf. 保持精度很重要!!

4

3 回答 3

2

If you want to convert string to long double, use [stold][1]

If you want to printf long double, use %Lf.

于 2015-02-12T06:32:55.790 回答
1

sscanf(str, "%lf", &number) understands any formats. scanf and sscanf allow checking the result of conversion:

    if( sscanf(str, "%lf", &number) )
    {
        printf("number = %le\n", number);
    }
于 2015-02-12T06:32:52.560 回答
1

试试double atof(const char*)

于 2015-02-12T06:30:55.170 回答