0

I have a text file with numbers in scientific notation stored as a string of characters, some of which have up to 20 digits. For example, 2.3456789e-015. I need to convert this into a long double in order to perform some mathematical operations on it.

How can I accomplish this?

long double number = 2.3456789e-015;

I know I can declare a long double like so, but the trouble comes when I have the number as a string and need to specify the base as well as the exponent separately. The pow() function only works on doubles and cpowl() has no way to be formatted in printf. It is important that I keep the precision!!

oneb1got
  • 153
  • 1
  • 4
  • 8
  • [`strtold`](http://en.cppreference.com/w/c/string/byte/strtof) would seem a pretty good candidate for the job. – WhozCraig Feb 12 '15 at 06:31

3 Answers3

2

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

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

Mine
  • 4,123
  • 1
  • 25
  • 46
1

Try double atof(const char*).

CiaPan
  • 9,381
  • 2
  • 21
  • 35
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);
    }
VolAnd
  • 6,367
  • 3
  • 25
  • 43