how to convert string to double?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kathy

    #1

    how to convert string to double?

    I can not find something like atof() which can convert string to
    double.

    Can anyone help me?

  • xuatla

    #2
    Re: how to convert string to double?

    kathy wrote:[color=blue]
    > I can not find something like atof() which can convert string to
    > double.
    >
    > Can anyone help me?
    >[/color]

    How about just using atof(s.c_str()) , given s is a string in your case?

    X

    Comment

    • Thomas Tutone

      #3
      Re: how to convert string to double?

      kathy wrote:
      [color=blue]
      > I can not find something like atof() which can convert string to
      > double.
      >
      > Can anyone help me?[/color]

      It's in the FAQ:



      You may want to read the rest of the FAQ too - I bet you'll find it
      very helpful.

      Best regards,

      Tom

      Comment

      • dakka

        #4
        Re: how to convert string to double?

        kathy wrote:[color=blue]
        > I can not find something like atof() which can convert string to
        > double.
        >
        > Can anyone help me?
        >[/color]

        #include <iostream>
        #include <sstream>

        using namespace std;

        double strtodouble(con st string& what)
        {
        istringstream instr(what);
        double val;
        instr >> val;
        return val;
        }

        int main()
        {
        string dbl("3.1415");
        cout << strtodouble(dbl ) << endl;
        return 0;
        }

        --
        --dakka

        Dykstra's Observation:
        If debugging is the process of removing bugs, then programming must be
        the process of putting them in.

        Comment

        • kathy

          #5
          Re: how to convert string to double?

          The following failed:

          istringstream instr;
          double val[2];
          instr.str(str1) ;
          instr >> val[0];
          instr.str(str2) ;
          instr >> val[2];

          why?

          Comment

          • kathy

            #6
            Re: how to convert string to double?

            wrong typing:

            instr >> val[2];

            should be

            instr >> val[1];

            Comment

            • kathy

              #7
              Re: how to convert string to double?

              wrong typing:

              instr >> val[2];

              should be

              instr >> val[1];

              Comment

              • mlimber

                #8
                Re: how to convert string to double?

                kathy wrote:[color=blue]
                > The following failed:
                >
                > istringstream instr;
                > double val[2];
                > instr.str(str1) ;
                > instr >> val[0];[/color]

                Because the stream state is currently set to fail, you need to clear
                it:

                instr.clear();
                [color=blue]
                > instr.str(str2) ;
                > instr >> val[1]; // Fixed as in your followup
                >
                > why?[/color]

                In the future, you should tell us how it failed. Fortunately, this one
                was fairly easy to spot.

                Cheers! --M

                Comment

                • kathy

                  #9
                  Re: how to convert string to double?

                  why it set to fail state?

                  Comment

                  Working...