Formatting decimal places

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

    Formatting decimal places

    I trying to format my output to display a set number of decimal
    places. I have been trying to use the <iomanip> setprecision(), but
    that will only display the total number of digits. Can someone please
    help me???? Thanks.
  • Marcin Kalicinski

    #2
    Re: Formatting decimal places

    Uzytkownik "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
    news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...[color=blue]
    > I trying to format my output to display a set number of decimal
    > places. I have been trying to use the <iomanip> setprecision(), but
    > that will only display the total number of digits. Can someone please
    > help me???? Thanks.[/color]

    Check out boost::format.

    Marcin


    Comment

    • John Harrison

      #3
      Re: Formatting decimal places


      "Andrew" <admckni@hqcnsg .navy.mil> wrote in message
      news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...[color=blue]
      > I trying to format my output to display a set number of decimal
      > places. I have been trying to use the <iomanip> setprecision(), but
      > that will only display the total number of digits. Can someone please
      > help me???? Thanks.[/color]

      You have to set the 'float mode' to fixed. E.g.

      #include <iostream>

      #include <iomanip>

      using namespace std;

      int main()

      {

      cout << fixed << setprecision(1) << 1.23456789 << '\n';

      cout << fixed << setprecision(2) << 1.23456789 << '\n';

      cout << fixed << setprecision(3) << 1.23456789 << '\n';

      cout << fixed << setprecision(4) << 1.23456789 << '\n';

      cout << fixed << setprecision(5) << 1.23456789 << '\n';

      }

      john




      Comment

      • Julie

        #4
        Re: Formatting decimal places

        Marcin Kalicinski wrote:[color=blue]
        >
        > Uzytkownik "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
        > news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...[color=green]
        > > I trying to format my output to display a set number of decimal
        > > places. I have been trying to use the <iomanip> setprecision(), but
        > > that will only display the total number of digits. Can someone please
        > > help me???? Thanks.[/color]
        >
        > Check out boost::format.
        >
        > Marcin[/color]

        Boost is OT in this forum.

        Comment

        • Mike Wahler

          #5
          Re: Formatting decimal places


          "Andrew" <admckni@hqcnsg .navy.mil> wrote in message
          news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...[color=blue]
          > I trying to format my output to display a set number of decimal
          > places. I have been trying to use the <iomanip> setprecision(), but
          > that will only display the total number of digits. Can someone please
          > help me???? Thanks.[/color]

          #include <ios>
          #include <iomanip>
          #include <iostream>

          int main()
          {
          double d(3.14);

          std::cout << std::fixed << std::setprecisi on(4)
          << d << '\n';

          return 0;
          }

          Output:

          3.1400


          -Mike



          Comment

          • Siemel Naran

            #6
            Re: Formatting decimal places

            "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            news:c63g5t$7da a2$1@ID-
            [color=blue]
            > You have to set the 'float mode' to fixed. E.g.[/color]
            [color=blue]
            > cout << fixed << setprecision(1) << 1.23456789 << '\n';
            >
            > cout << fixed << setprecision(2) << 1.23456789 << '\n';[/color]

            The float mode is sticky, so you don't have to set it in each line, though
            setting it is harmless and often a good idea for clarity if we're fixing
            fixed and scientific output in the same program.

            cout << fixed;
            cout << setprecision(1) << 1.23456789 << '\n';
            cout << setprecision(2) << 1.23456789 << '\n';


            Comment

            • bartek

              #7
              Re: Formatting decimal places

              Julie <julie@nospam.c om> wrote in news:408545AE.C 7FE7FDC@nospam. com:
              [color=blue]
              > Marcin Kalicinski wrote:[color=green]
              >>
              >> Uzytkownik "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
              >> news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...[color=darkred]
              >> > I trying to format my output to display a set number of decimal
              >> > places. I have been trying to use the <iomanip> setprecision(), but
              >> > that will only display the total number of digits. Can someone please
              >> > help me???? Thanks.[/color]
              >>
              >> Check out boost::format.
              >>
              >> Marcin[/color]
              >
              > Boost is OT in this forum.[/color]

              Why don't you like boost?

              Strange, but I've seen other people not using it *just because* (i'm not
              counting compiler issues that might arise with boost).
              Why - I wonder.

              cheers!
              b

              Comment

              • Julie

                #8
                Re: Formatting decimal places

                bartek wrote:[color=blue]
                >
                > Julie <julie@nospam.c om> wrote in news:408545AE.C 7FE7FDC@nospam. com:
                >[color=green]
                > > Marcin Kalicinski wrote:[color=darkred]
                > >>
                > >> Uzytkownik "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
                > >> news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...
                > >> > I trying to format my output to display a set number of decimal
                > >> > places. I have been trying to use the <iomanip> setprecision(), but
                > >> > that will only display the total number of digits. Can someone please
                > >> > help me???? Thanks.
                > >>
                > >> Check out boost::format.
                > >>
                > >> Marcin[/color]
                > >
                > > Boost is OT in this forum.[/color]
                >
                > Why don't you like boost?[/color]

                I did not express my preference for Boost in my response.

                I was expressing (rather tersely) that a reference to Boost is off-topic in
                this forum when there is an existing and preferred solution in C++ language.

                Comment

                • bartek

                  #9
                  Re: Formatting decimal places

                  Julie <julie@nospam.c om> wrote in news:4085574C.C E8A5E68@nospam. com:
                  [color=blue]
                  > bartek wrote:[color=green]
                  >>
                  >> Julie <julie@nospam.c om> wrote in news:408545AE.C 7FE7FDC@nospam. com:
                  >>[color=darkred]
                  >> > Marcin Kalicinski wrote:
                  >> >>
                  >> >> Uzytkownik "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
                  >> >> news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...
                  >> >> > I trying to format my output to display a set number of decimal
                  >> >> > places. I have been trying to use the <iomanip> setprecision(),
                  >> >> > but that will only display the total number of digits. Can
                  >> >> > someone please help me???? Thanks.
                  >> >>
                  >> >> Check out boost::format.
                  >> >>
                  >> >> Marcin
                  >> >
                  >> > Boost is OT in this forum.[/color]
                  >>
                  >> Why don't you like boost?[/color]
                  >
                  > I did not express my preference for Boost in my response.
                  >
                  > I was expressing (rather tersely) that a reference to Boost is
                  > off-topic in this forum when there is an existing and preferred
                  > solution in C++ language.
                  >[/color]

                  Sorry, I overreacted to your post.

                  Although, I don't think that mentioning boost is off topic here at all.
                  Just as it's not off topic to direct an (confused?) individual to other
                  (more appropriate?) newsgroups, isn't it?

                  cheers!
                  b

                  Comment

                  • Dave Moore

                    #10
                    Re: Formatting decimal places

                    admckni@hqcnsg. navy.mil (Andrew) wrote in message news:<8e7fb04b. 0404200701.78f6 38bb@posting.go ogle.com>...[color=blue]
                    > I trying to format my output to display a set number of decimal
                    > places. I have been trying to use the <iomanip> setprecision(), but
                    > that will only display the total number of digits. Can someone please
                    > help me???? Thanks.[/color]

                    I doubt you have stated things correctly, unless your version of the
                    STL is broken ... an example usually helps clear up such
                    misunderstandin gs. The problem is that you also need to use the
                    manipulator std::fixed to specify that the field has a fixed number of
                    decimal places. Thus the following code

                    #include <iostream>
                    #include <iomanip>

                    int main() {
                    std::cout << std::setprecisi on(3);
                    std::cout << 1.0 << ' ' << 12.47 << ' ' << 16.89567 << std::endl;
                    std::cout << std::fixed;
                    std::cout << 1.0 << ' ' << 12.47 << ' ' << 16.89567 << std::endl;
                    return 0;
                    }

                    should produce:
                    1 12.47 16.9 // think about it .. you'll figure out why
                    1.0000 12.4700 16.8957

                    HTH, Dave Moore

                    Comment

                    • David Harmon

                      #11
                      Re: Formatting decimal places

                      On Tue, 20 Apr 2004 08:45:50 -0700 in comp.lang.c++, Julie
                      <julie@nospam.c om> wrote,[color=blue]
                      >Marcin Kalicinski wrote:[color=green]
                      >>
                      >> Uzytkownik "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
                      >> news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...[color=darkred]
                      >> > I trying to format my output to display a set number of decimal
                      >> > places. I have been trying to use the <iomanip> setprecision(), but
                      >> > that will only display the total number of digits. Can someone please
                      >> > help me???? Thanks.[/color]
                      >>
                      >> Check out boost::format.
                      >>
                      >> Marcin[/color]
                      >
                      >Boost is OT in this forum.[/color]

                      Where OT = "On Topic"

                      Comment

                      • Julie

                        #12
                        Re: Formatting decimal places

                        David Harmon wrote:[color=blue]
                        >
                        > On Tue, 20 Apr 2004 08:45:50 -0700 in comp.lang.c++, Julie
                        > <julie@nospam.c om> wrote,[color=green]
                        > >Marcin Kalicinski wrote:[color=darkred]
                        > >>
                        > >> Uzytkownik "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
                        > >> news:8e7fb04b.0 404200701.78f63 8bb@posting.goo gle.com...
                        > >> > I trying to format my output to display a set number of decimal
                        > >> > places. I have been trying to use the <iomanip> setprecision(), but
                        > >> > that will only display the total number of digits. Can someone please
                        > >> > help me???? Thanks.
                        > >>
                        > >> Check out boost::format.
                        > >>
                        > >> Marcin[/color]
                        > >
                        > >Boost is OT in this forum.[/color]
                        >
                        > Where OT = "On Topic"[/color]

                        Nope. OT is the accepted "Off Topic".

                        From one of my other responses:
                        [color=blue]
                        > I was expressing (rather tersely) that a reference to Boost is off-topic in
                        > this forum when there is an existing and preferred solution in C++ language.[/color]

                        Comment

                        • Dave Moore

                          #13
                          Re: Formatting decimal places

                          admckni@hqcnsg. navy.mil (Andrew) wrote in message news:<8e7fb04b. 0404200701.78f6 38bb@posting.go ogle.com>...[color=blue]
                          > I trying to format my output to display a set number of decimal
                          > places. I have been trying to use the <iomanip> setprecision(), but
                          > that will only display the total number of digits. Can someone please
                          > help me???? Thanks.[/color]

                          Sorry .. after submitting I realized that the line

                          std::cout << std::setprecisi on(3);

                          in my previous post should have been

                          std::cout << std::setprecisi on(4);

                          I guess that is why they give you that "review before posting" option
                          at google 8*) .. sorry for any confusion.

                          Comment

                          • Jeff Schwab

                            #14
                            Re: Formatting decimal places

                            Julie wrote:[color=blue]
                            > David Harmon wrote:
                            >[color=green]
                            >>On Tue, 20 Apr 2004 08:45:50 -0700 in comp.lang.c++, Julie
                            >><julie@nospam .com> wrote,
                            >>[color=darkred]
                            >>>Marcin Kalicinski wrote:
                            >>>
                            >>>>Uzytkowni k "Andrew" <admckni@hqcnsg .navy.mil> napisal w wiadomosci
                            >>>>news:8e7fb0 4b.0404200701.7 8f638bb@posting .google.com...
                            >>>>
                            >>>>>I trying to format my output to display a set number of decimal
                            >>>>>places. I have been trying to use the <iomanip> setprecision(), but
                            >>>>>that will only display the total number of digits. Can someone please
                            >>>>>help me???? Thanks.
                            >>>>
                            >>>>Check out boost::format.
                            >>>>
                            >>>>Marcin
                            >>>
                            >>>Boost is OT in this forum.[/color]
                            >>
                            >>Where OT = "On Topic"[/color]
                            >
                            >
                            > Nope. OT is the accepted "Off Topic".
                            >
                            > From one of my other responses:
                            >
                            >[color=green]
                            >>I was expressing (rather tersely) that a reference to Boost is off-topic in
                            >>this forum when there is an existing and preferred solution in C++ language.[/color][/color]

                            Not a fan of irony, I see.

                            Why is it OT even to mention boost? Also, if you know of a "preferred
                            solution," why was it worth your time to criticize someone else, but not
                            worth your time to present the solution?

                            Comment

                            • David Harmon

                              #15
                              Re: Formatting decimal places

                              On Tue, 20 Apr 2004 10:41:14 -0700 in comp.lang.c++, Julie
                              <julie@nospam.c om> wrote,[color=blue]
                              >[color=green]
                              >> I was expressing (rather tersely) that a reference to Boost is off-topic in
                              >> this forum when there is an existing and preferred solution in C++ language.[/color][/color]

                              Boost is a collection of solutions in the C++ language. Widely accepted
                              and frequently preferred. Not in any way off-topic that I can see.
                              It does happen to be the wrong answer to the question of this thread,
                              but that's a different matter entirely.

                              Comment

                              Working...