question: std:string and float values??

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

    question: std:string and float values??

    I am starting to use templates and have managed to figure out how to use
    std::string, std::map and make_pair successfully so far (Yeah I know -
    not much of a big step but I'm getting there)

    Previously I was using something like:

    char *buffer[50];
    double value = 10.5;

    sprintf(buffer, "text %.2f text", value);

    How should I do this using an std:string in the place of the buffer
    variable?

    Thanks in advance

    Sean Hannan
  • Senthilvel Samatharman

    #2
    Re: question: std:string and float values??

    Woodster wrote:
    [color=blue]
    > I am starting to use templates and have managed to figure out how to use
    > std::string, std::map and make_pair successfully so far (Yeah I know -
    > not much of a big step but I'm getting there)
    >
    > Previously I was using something like:
    >
    > char *buffer[50];
    > double value = 10.5;
    >
    > sprintf(buffer, "text %.2f text", value);
    >
    > How should I do this using an std:string in the place of the buffer
    > variable?[/color]

    Use stringstream

    #include <iostream>
    #include <sstream>

    int main()
    {
    std:: stringstream str;
    double value = 10.5;

    str<<"text "<<value<<" text";

    string buffer(str.str( ));

    std::cout<<buff er;

    return 0;
    }

    This is how i'd do .. mabe experts here can show you a better way..

    [color=blue]
    >
    >
    > Thanks in advance
    >
    > Sean Hannan[/color]

    Comment

    • Senthilvel Samatharman

      #3
      Re: question: std:string and float values??

      >

      Sorry a small correction in the program
      [color=blue]
      >
      > Use stringstream
      >
      > #include <iostream>
      > #include <sstream>[/color]

      #include <string> // I missed it last time...
      [color=blue]
      >
      >
      > int main()
      > {
      > std:: stringstream str;
      > double value = 10.5;
      >
      > str<<"text "<<value<<" text";
      >
      > std::string buffer(str.str( )); //In my last post it was just string...not
      > std::string.
      >
      > std::cout<<buff er;
      >
      > return 0;
      > }
      >
      > This is how i'd do .. mabe experts here can show you a better way..
      >[color=green]
      > >
      > >
      > > Thanks in advance
      > >
      > > Sean Hannan[/color][/color]

      Comment

      • Woodster

        #4
        Re: question: std:string and float values??

        In article <3FB32FD0.3ED03 605@adcc.alcate l.be>,
        Samatharman.Sen thilvel@adcc.al catel.be says...[color=blue]
        > Use stringstream
        >
        > #include <iostream>
        > #include <sstream>
        >
        > int main()
        > {
        > std:: stringstream str;
        > double value = 10.5;
        >
        > str<<"text "<<value<<" text";
        >
        > string buffer(str.str( ));[/color]

        Only problem is that this does not allow formatting of the variable (ie:
        %.2f in this case). Is there a way I can still format the variable
        'value'

        Sean Hannan

        Comment

        • Frank Schmitt

          #5
          Re: question: std:string and float values??

          Woodster <mirror@127.0.0 .1> writes:
          [color=blue]
          > In article <3FB32FD0.3ED03 605@adcc.alcate l.be>,
          > Samatharman.Sen thilvel@adcc.al catel.be says...[color=green]
          > > Use stringstream
          > >
          > > #include <iostream>
          > > #include <sstream>
          > >
          > > int main()
          > > {
          > > std:: stringstream str;
          > > double value = 10.5;
          > >
          > > str<<"text "<<value<<" text";
          > >
          > > string buffer(str.str( ));[/color]
          >
          > Only problem is that this does not allow formatting of the variable (ie:
          > %.2f in this case). Is there a way I can still format the variable
          > 'value'[/color]

          use std::setprecisi on:

          #include <iomanip>
          #include <sstream>

          int main() {
          std:: stringstream str;
          double value = 10.5;

          str << std::setprecisi on(2) << "text " << value << " text";
          string buffer(str.str( ));
          ....

          HTH & kind regards
          frank

          --
          Frank Schmitt
          4SC AG phone: +49 89 700763-0
          e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

          Comment

          • Senthilvel Samatharman

            #6
            Re: question: std:string and float values??

            Woodster wrote:
            [color=blue]
            > In article <3FB32FD0.3ED03 605@adcc.alcate l.be>,
            > Samatharman.Sen thilvel@adcc.al catel.be says...[color=green]
            > > Use stringstream
            > >
            > > #include <iostream>
            > > #include <sstream>
            > >
            > > int main()
            > > {
            > > std:: stringstream str;
            > > double value = 10.5;
            > >
            > > str<<"text "<<value<<" text";
            > >
            > > string buffer(str.str( ));[/color]
            >
            > Only problem is that this does not allow formatting of the variable (ie:
            > %.2f in this case). Is there a way I can still format the variable
            > 'value'[/color]

            Use setprecision of <iomanip>
            Think this is what you ask for

            #include <iostream>
            #include <sstream>
            #include <string>
            #include <iomanip>

            int main()
            {
            std:: stringstream str;
            double value = 10.590878;

            str<<"text "<<std::setprec ision(5)<<value <<" text"; //--> value will have
            exactly five digits( before decimal point + after decimal point)

            std::string buffer(str.str( ));

            std::cout<<buff er;

            return 0;
            }
            [color=blue]
            >
            >
            > Sean Hannan[/color]

            Comment

            • Jon Bell

              #7
              Re: question: std:string and float values??

              In article <MPG.1a1dbefcde df150498968a@ne ws.westnet.com. au>,
              Woodster <mirror@127.0.0 .1> wrote:[color=blue]
              >In article <3FB32FD0.3ED03 605@adcc.alcate l.be>,
              >Samatharman.Se nthilvel@adcc.a lcatel.be says...[color=green]
              >> Use stringstream
              >>
              >> #include <iostream>
              >> #include <sstream>
              >>
              >> int main()
              >> {
              >> std:: stringstream str;
              >> double value = 10.5;
              >>
              >> str<<"text "<<value<<" text";
              >>
              >> string buffer(str.str( ));[/color]
              >
              >Only problem is that this does not allow formatting of the variable (ie:
              >%.2f in this case). Is there a way I can still format the variable
              >'value'[/color]

              Sure, use stream manipulators just like you would if you were writing to
              cout or a file. I'm not completely "up" on C-style formatting so I'm not
              sure exactly what format you want, but this might come close:

              str << "text " << fixed << setprecision(2) << value << " text";

              --
              Jon Bell <jtbellap8@pres by.edu> Presbyterian College
              Dept. of Physics and Computer Science Clinton, South Carolina USA

              Comment

              • Woodster

                #8
                Re: question: std:string and float values??

                Thanks for the prompt response from all who replied. This will get me
                going for what I am trying to do.

                Regards

                Sean Hannan

                In article <bp02ep$aj8$2@j tbell.presby.ed u>, jtbellq2f@presb y.edu
                says...[color=blue]
                > In article <MPG.1a1dbefcde df150498968a@ne ws.westnet.com. au>,
                > Woodster <mirror@127.0.0 .1> wrote:[color=green]
                > >In article <3FB32FD0.3ED03 605@adcc.alcate l.be>,
                > >Samatharman.Se nthilvel@adcc.a lcatel.be says...[color=darkred]
                > >> Use stringstream
                > >>
                > >> #include <iostream>
                > >> #include <sstream>
                > >>
                > >> int main()
                > >> {
                > >> std:: stringstream str;
                > >> double value = 10.5;
                > >>
                > >> str<<"text "<<value<<" text";
                > >>
                > >> string buffer(str.str( ));[/color]
                > >
                > >Only problem is that this does not allow formatting of the variable (ie:
                > >%.2f in this case). Is there a way I can still format the variable
                > >'value'[/color]
                >
                > Sure, use stream manipulators just like you would if you were writing to
                > cout or a file. I'm not completely "up" on C-style formatting so I'm not
                > sure exactly what format you want, but this might come close:
                >
                > str << "text " << fixed << setprecision(2) << value << " text";
                >
                >[/color]

                Comment

                • Jon Bell

                  #9
                  Re: question: std:string and float values??

                  In article <MPG.1a1da53f84 43d74989689@new s.westnet.com.a u>,
                  Woodster <mirror@127.0.0 .1> wrote:[color=blue]
                  >
                  >char *buffer[50];
                  >double value = 10.5;
                  >
                  >sprintf(buffer , "text %.2f text", value);
                  >
                  >How should I do this using an std:string in the place of the buffer
                  >variable?[/color]

                  #include <cstdio>
                  #include <iostream>
                  #include <iomanip>
                  #include <string>
                  #include <sstream>

                  using namespace std;

                  int main ()
                  {
                  // C-style method

                  double value = 10.5;
                  char buf[80];
                  sprintf (buf, "text %.2f text\n", value);
                  cout << buf;

                  // C++-style method

                  ostringstream bufstream;
                  bufstream << "text "
                  << fixed << setprecision(2) << value
                  << " text\n";
                  cout << bufstream.str() ;

                  return 0;
                  }


                  --
                  Jon Bell <jtbellap8@pres by.edu> Presbyterian College
                  Dept. of Physics and Computer Science Clinton, South Carolina USA

                  Comment

                  Working...