Format string into scientific notation

14,784

Solution 1

Something like this:

#include<iostream>
using namespace std;
int main()
{
        char *s = "0.4794255386042030002732879352156";
        double d;
        sscanf(s,"%lf",&d);
        printf("%.12e\n",d);
        return EXIT_SUCCESS;
}

Output:

# g++ a.cpp  && ./a.out
4.794255386042e-01

Solution 2

Are you looking for something like this?

Here is a sample:

 // modify basefield
#include <iostream>
#include <sstream>
using namespace std;
int main () {
    std::string numbers("0.4794255386042030002732879352156");
    std::stringstream stream;
    stream << numbers;
    double number_fmt;
    stream >> number_fmt;
    cout.precision(30);
    cout << number_fmt << endl;
    cout.precision(5);
    cout << scientific << number_fmt << endl;
  return 0;
}

Output:

0.479425538604203005377257795772

4.79426e-01

Solution 3

In highly portable C the working example below outputs:

result is 4.794255386042E-01
#include <stdio.h>
int main()
{
    char *str = "0.4794255386042030002732879352156";
    double f;
    char   newstr [50];
    // convert string in `str` to float
    sscanf (str, "%le", &f);
    sprintf (newstr, "%.12E", f);
    printf ("result is %s\n", newstr);
    return 0;
}

Solution 4

Looking at the strings in your question, it would seem you are using base-10 logarithms. In that case wouldn't it be relatively easy to just count the leading or trailing zeros and put that in an exponent, by scanning the strings directly?

Maybe i'm missing something..

Solution 5

Convert to long double using sscanf(), then use sprintf() to print it back out as a string, using the scientific formatter:

long double x;
char num[64];
if(sscanf(string, "%Lf", &x) == 1)
  sprintf(num, "%.12Le", x);

Not sure if even long double actually gives you 12 significant digits, though. On my system, this generates "4.79425538604e-01".

Share:
14,784

Related videos on Youtube

John Scipione
Author by

John Scipione

I am a electrical engineering student at Rochester Institute of Technology and part time programmer for hire.

Updated on June 04, 2022

Comments

  • John Scipione
    John Scipione about 1 year

    I have a string that looks like this:

    "0.4794255386042030002732879352156"
    

    which is approximately the sin(0.5). I would like to format the string to look a much nicer

    "4.794255386042e-1"
    

    How can I achieve this? Remember I am dealing with strings and not numbers (float, double). Also I need to round to keep the number as accurate as possible, I can't just truncate. If I need to convert to a different data type I would prefer a long double because a regular double doesn't have enough precision. I'd like at least 12 decimal digits before rounding. Perhaps there is a simple sprintf() conversion I could do.

  • dcp
    dcp over 13 years
    No, that outputs 0.4794255386042030002732879352156, which isn't what he asked for.
  • Clifford
    Clifford over 13 years
    Agreed. In this case he could simply find the position of the point, move it, and append the exponent as a count of how far and in which direction the point was moved.
  • John Scipione
    John Scipione over 13 years
    did you mean if(sscanf(string, "%Lf", &x) == 1) because %Ld gives a long integer?
  • John Scipione
    John Scipione over 13 years
    you are right a double is good enough once I added in a proper format string I even got 17 decimal digits out of a double, I am sure I could get more but that is plenty.
  • Clifford
    Clifford over 13 years
    Not with a double you couldn't! In fact not even 17 is possible regardless of what you thing you got. The mantissa is 52 bits, 2^52 is a 16 digit decimal number, so the limit is 15 digits (mathematically 15.65 digits) because 52 bits is sufficient to represent all possible 15 digit numbers. Note that I am talking about significant figures not decimal places.
  • Sanjaya R
    Sanjaya R over 13 years
    No, it outputs 4.794255e-01 ( the default precision is 6 ). 'cout.precision(12)' would set it to twelve.

Related