1,105,232 Community Members

Right Justified / decimal point alignment

Member Avatar
dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

I have a column of floating point data that I need to right justify and align the decimal points. Help.

Data looks something like this.

30.768
1.345
.430

Member Avatar
Chainsaw
Posting Pro in Training
434 posts since Jun 2004
Reputation Points: 12 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 

you could, for example, use printf or sprintf or its ilk. Take a look at the specifications for the strings. You can control right or left justification, leading zeros, number of digits after the decimals, etc.

If your output is using a proportional font, the spaces won't be as wide as the digits, so that can be a problem too; hopefully you will use a non-proportional font, or else you'll have to do placement in some measurement like pixels or inches.

Member Avatar
dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm using visual studio C++ I've run across ios class manipulators like set precision, left, right -- haven't used them yet though, wondering if these are the best/simplest approach I should be using. I don't see leading zero manipulators? Any additional advise?

you could, for example, use printf or sprintf or its ilk. Take a look at the specifications for the strings. You can control right or left justification, leading zeros, number of digits after the decimals, etc.

If your output is using a proportional font, the spaces won't be as wide as the digits, so that can be a problem too; hopefully you will use a non-proportional font, or else you'll have to do placement in some measurement like pixels or inches.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 

When in doubt, use the old workhorse printf(). I have played around with manipulators like

cout << setfill('0') << setw(8) << d1;

with results that cause only consternation. The old printf() like Chainsaw mentioned works well:

// right justified numeric output  (Dev-C++)

#include <cstdlib>
#include <iostream>  // may have to include stdio.h with VC++

using namespace std;

int main()
{
  double d1 = 30.768;
  double d2 = 1.345;
  double d3 = .430;
  
  printf("%8.3f\n",d1);
  printf("%8.3f\n",d2);
  printf("%8.3f\n\n",d3);    
    
  system("PAUSE");
  return EXIT_SUCCESS;
}
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

When challenged, think a little!!!!!!!!!! Yes you can line up your decimal points with std::cout ...

// right justified numeric output  (Dev-C++)

#include <cstdlib>
#include <iostream>
#include <iomanip>  // setXXX() functions

using namespace std;

int main()
{
  double d1 = 30.768;
  double d2 = 1.345;
  double d3 = .430;
  
  // this works well, the decimal points line up
  printf("%8.3f\n",d1);
  printf("%8.3f\n",d2);
  printf("%8.3f\n\n",d3); 

  // dito, but keep the order of setXXX() functions!!
  cout << setiosflags(ios::right);
  cout << setiosflags(ios::fixed);
  cout << setw(8) << setprecision(3) << d1 << endl;
  cout << setw(8) << setprecision(3) << d2 << endl; 
  cout << setw(8) << setprecision(3) << d3 << endl << endl; 
    
  system("PAUSE");
  return EXIT_SUCCESS;
}
Member Avatar
Siersan
Light Poster
45 posts since Jan 2005
Reputation Points: 1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
1
 

@vegaseat:

It's not a good idea to rely on nonstandard extensions. You should include cstdio when using printf.

#include <cstdio>
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  double d1 = 30.768;
  double d2 = 1.345;
  double d3 = .430;

  printf("%8.3f\n", d1);
  printf("%8.3f\n", d2);
  printf("%8.3f\n\n", d3);

  cout.setf(ios::fixed);
  cout<< setw(8) << setprecision(3) << d1 <<endl;
  cout<< setw(8) << setprecision(3) << d2 <<endl;
  cout<< setw(8) << setprecision(3) << d3 <<endl;
}
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

A friendly note to Siersan:

Spend some time with your editor to look at the iostream header. The one that comes with Dev-C++ leads you into a trail of internal includes to other header files which in turn include other header files and so on. Surprise, somewhere near the end is stdio.h so printf() is taken care off! Even cstdio simply includes stdio.h .....

You are right, to keep the notorius complainers of your back, it is better to include the obvious headers from the books! My oversight, I profoundly apologize!

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

>Spend some time with your editor to look at the iostream header.
Spend some time with the C++ standard. iostream isn't required to include cstdio or stdio.h, so your code exhibits undefined behavior.

>The one that comes with Dev-C++
Not everyone uses Dev-C++.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

>Spend some time with your editor to look at the iostream header.
Spend some time with the C++ standard. iostream isn't required to include cstdio or stdio.h, so your code exhibits undefined behavior.

>The one that comes with Dev-C++
Not everyone uses Dev-C++.

You are right, to keep the notorius complainers of your back, it is better to include the obvious headers from the books! My oversight, I profoundly apologize!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article