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. Lining up the Decimal Points

Lining up the Decimal Points

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
8 Posts 3 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.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    I would like to output three columns of numbers with the decimal points lined up. I wrote the following piece of code which I thought would do it.

    for( int i = 0; i<100; i++ ) {
    	double f1 = (int)(i/20);
    	double f2 = 3.1415\*i;
    	double f3 = 3.1415\*(f2+3);
    	cout.width( 10 );
    	cout.precision( 4 );
    	cout << std::left << f1 << " ";
    	cout.width( 10 );
    	cout << std::left << f2 << " ";
    	cout.width( 10 );
    	cout << std::left << f3 << endl;
    }
    

    However, the decimal points are not lined up. Also, I want to do this in C++ not C. That is, I do not want to use, printf, fprintf or sprintf. How do I get the decimal points to line up vertical? Thanks Bob

    M D 2 Replies Last reply
    0
    • B BobInNJ

      I would like to output three columns of numbers with the decimal points lined up. I wrote the following piece of code which I thought would do it.

      for( int i = 0; i<100; i++ ) {
      	double f1 = (int)(i/20);
      	double f2 = 3.1415\*i;
      	double f3 = 3.1415\*(f2+3);
      	cout.width( 10 );
      	cout.precision( 4 );
      	cout << std::left << f1 << " ";
      	cout.width( 10 );
      	cout << std::left << f2 << " ";
      	cout.width( 10 );
      	cout << std::left << f3 << endl;
      }
      

      However, the decimal points are not lined up. Also, I want to do this in C++ not C. That is, I do not want to use, printf, fprintf or sprintf. How do I get the decimal points to line up vertical? Thanks Bob

      M Offline
      M Offline
      Malli_S
      wrote on last edited by
      #2

      Check if this is helpful.

      {
      cout << fixed;
      for( int i = 0; i<10; i++ ) {
      double f1 = (int)(i/20);
      double f2 = 3.1415*i;
      double f3 = 3.1415*(f2+3);
      std::cout << std::setfill (' ') << std::setw (10);
      cout.width( 10 );
      cout.precision( 4 );
      cout << f1 << " ";
      cout.width( 10 );
      cout << f2 << " ";
      cout.width( 10 );
      cout << f3 << endl;
      }

      	getch();
      }
      

      [Delegates]      [Virtual Desktop]      [Tray Me !]
      -Malli...! :rose:****

      B 1 Reply Last reply
      0
      • M Malli_S

        Check if this is helpful.

        {
        cout << fixed;
        for( int i = 0; i<10; i++ ) {
        double f1 = (int)(i/20);
        double f2 = 3.1415*i;
        double f3 = 3.1415*(f2+3);
        std::cout << std::setfill (' ') << std::setw (10);
        cout.width( 10 );
        cout.precision( 4 );
        cout << f1 << " ";
        cout.width( 10 );
        cout << f2 << " ";
        cout.width( 10 );
        cout << f3 << endl;
        }

        	getch();
        }
        

        [Delegates]      [Virtual Desktop]      [Tray Me !]
        -Malli...! :rose:****

        B Offline
        B Offline
        BobInNJ
        wrote on last edited by
        #3

        Thanks for the response. However, it failed to produce the results I wanted. Here is part of the output that I got:

             0          0      9.425
             0      3.142      19.29
             0      6.283      29.16
             0      9.425      39.03
             0      12.57       48.9
             0      15.71      58.77
        

        The decimal points are not lined up. What I want is:

             0      0.0         9.425
             0      3.142      19.29
             0      6.283      29.16
             0      9.425      39.03
             0     12.57       48.9
             0     15.71       58.77
        

        In what I want the decimal points are all in the same column.

        M 1 Reply Last reply
        0
        • B BobInNJ

          I would like to output three columns of numbers with the decimal points lined up. I wrote the following piece of code which I thought would do it.

          for( int i = 0; i<100; i++ ) {
          	double f1 = (int)(i/20);
          	double f2 = 3.1415\*i;
          	double f3 = 3.1415\*(f2+3);
          	cout.width( 10 );
          	cout.precision( 4 );
          	cout << std::left << f1 << " ";
          	cout.width( 10 );
          	cout << std::left << f2 << " ";
          	cout.width( 10 );
          	cout << std::left << f3 << endl;
          }
          

          However, the decimal points are not lined up. Also, I want to do this in C++ not C. That is, I do not want to use, printf, fprintf or sprintf. How do I get the decimal points to line up vertical? Thanks Bob

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Have you tried removing std::left?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          B 1 Reply Last reply
          0
          • D David Crow

            Have you tried removing std::left?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            B Offline
            B Offline
            BobInNJ
            wrote on last edited by
            #5

            I tried it with std::left, without std::left and with std::right. In all three cases it did not work. What I believe the problem is that there is no way to specified a fixed number of digits along with a fixed number of digits to the right of the decimal point. I think I am going to go use sprintf and then send the output the string sprintf produces. It is not the "C++ way" of doing things but it works. I want to thank the group for their responses. Bob

            D 1 Reply Last reply
            0
            • B BobInNJ

              I tried it with std::left, without std::left and with std::right. In all three cases it did not work. What I believe the problem is that there is no way to specified a fixed number of digits along with a fixed number of digits to the right of the decimal point. I think I am going to go use sprintf and then send the output the string sprintf produces. It is not the "C++ way" of doing things but it works. I want to thank the group for their responses. Bob

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Try adding cout << setiosflags(ios::fixed) right before the for() loop.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              B 1 Reply Last reply
              0
              • B BobInNJ

                Thanks for the response. However, it failed to produce the results I wanted. Here is part of the output that I got:

                     0          0      9.425
                     0      3.142      19.29
                     0      6.283      29.16
                     0      9.425      39.03
                     0      12.57       48.9
                     0      15.71      58.77
                

                The decimal points are not lined up. What I want is:

                     0      0.0         9.425
                     0      3.142      19.29
                     0      6.283      29.16
                     0      9.425      39.03
                     0     12.57       48.9
                     0     15.71       58.77
                

                In what I want the decimal points are all in the same column.

                M Offline
                M Offline
                Malli_S
                wrote on last edited by
                #7

                But at my end I'm getting proper output. Have a look at this[^].

                [Delegates]      [Virtual Desktop]      [Tray Me !]
                -Malli...! :rose:****

                1 Reply Last reply
                0
                • D David Crow

                  Try adding cout << setiosflags(ios::fixed) right before the for() loop.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  B Offline
                  B Offline
                  BobInNJ
                  wrote on last edited by
                  #8

                  David, Adding the line:

                  cout << setiosflags(ios::fixed);

                  before the for loop did the trick. The program now works as expected. I am happy. I thank you and all the other people who responded to my post for their help in this matter. Bob

                  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