Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. output format

output format

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wong1907
    wrote on last edited by
    #1

    hello if "i","j" are double i=1.2345678 j=2.345678 cout<

    J M M 3 Replies Last reply
    0
    • W wong1907

      hello if "i","j" are double i=1.2345678 j=2.345678 cout<

      J Offline
      J Offline
      Joel Holdsworth
      wrote on last edited by
      #2

      printf("%.2f\n", i*j);

      - I believe that should do the trick!**

      With time we live, with money we spend! Joel Holdsworth.

      **

      W 1 Reply Last reply
      0
      • J Joel Holdsworth

        printf("%.2f\n", i*j);

        - I believe that should do the trick!**

        With time we live, with money we spend! Joel Holdsworth.

        **

        W Offline
        W Offline
        wong1907
        wrote on last edited by
        #3

        If I prefer use cout rather than printf how can I do??? THX!!!:-D:-D

        D 1 Reply Last reply
        0
        • W wong1907

          If I prefer use cout rather than printf how can I do??? THX!!!:-D:-D

          D Offline
          D Offline
          Dominik Reichl
          wrote on last edited by
          #4

          char szTemp[256];
          sprintf(szTemp, "%.2f\n", i*j);
          cout << szTemp;

          :-D -Dominik

          1 Reply Last reply
          0
          • W wong1907

            hello if "i","j" are double i=1.2345678 j=2.345678 cout<

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            You need to use an iomanip function. Unfortunately the VC 6 STL "documentation" is unhelpful, but something like:

            cout << setprecision(2) << i*j;

            should work. The horrible docs and the complexity of using iomanips is why printf() is favored for this type of output. ;) --Mike-- If it doesn't move and it should: WD-40. If it moves and it shouldn't: duct tape. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

            N 1 Reply Last reply
            0
            • W wong1907

              hello if "i","j" are double i=1.2345678 j=2.345678 cout<

              M Offline
              M Offline
              markkuk
              wrote on last edited by
              #6

              #include <iostream>
              #include <iomanip>

              int main(int argc, char* argv[]) {
              double i = 1.2345678;
              double j = 2.345678;
              std::cout << std::fixed << std::setprecision(2)
              << i * j << std::endl;
              }

              1 Reply Last reply
              0
              • M Michael Dunn

                You need to use an iomanip function. Unfortunately the VC 6 STL "documentation" is unhelpful, but something like:

                cout << setprecision(2) << i*j;

                should work. The horrible docs and the complexity of using iomanips is why printf() is favored for this type of output. ;) --Mike-- If it doesn't move and it should: WD-40. If it moves and it shouldn't: duct tape. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

                N Offline
                N Offline
                Nick Parker
                wrote on last edited by
                #7

                Michael Dunn wrote: You need to use an iomanip function. Unfortunately the VC 6 STL "documentation" is unhelpful I didn't know that the iomanip header file was part of the STL.


                Nick Parker

                You see the Standards change. - Fellow co-worker

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups