Right Justified / decimal point alignment

Reply

Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Right Justified / decimal point alignment

 
1
  #1
Jan 29th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Right Justified / decimal point alignment

 
0
  #2
Jan 29th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Re: Right Justified / decimal point alignment

 
0
  #3
Jan 29th, 2005
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?






Originally Posted by Chainsaw
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,985
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 927
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Right Justified / decimal point alignment

 
1
  #4
Jan 30th, 2005
When in doubt, use the old workhorse printf(). I have played around with manipulators like [php]cout << setfill('0') << setw(8) << d1;
[/php] with results that cause only consternation. The old printf() like Chainsaw mentioned works well:
[php]// 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;
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,985
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 927
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Right Justified / decimal point alignment

 
0
  #5
Jan 30th, 2005
When challenged, think a little!!!!!!!!!! Yes you can line up your decimal points with std::cout ...
[php]// 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;
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Right Justified / decimal point alignment

 
1
  #6
Jan 30th, 2005
@vegaseat:

It's not a good idea to rely on nonstandard extensions. You should include cstdio when using printf.
  1. #include <cstdio>
  2. #include <iomanip>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. double d1 = 30.768;
  10. double d2 = 1.345;
  11. double d3 = .430;
  12.  
  13. printf("%8.3f\n", d1);
  14. printf("%8.3f\n", d2);
  15. printf("%8.3f\n\n", d3);
  16.  
  17. cout.setf(ios::fixed);
  18. cout<< setw(8) << setprecision(3) << d1 <<endl;
  19. cout<< setw(8) << setprecision(3) << d2 <<endl;
  20. cout<< setw(8) << setprecision(3) << d3 <<endl;
  21. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,985
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 927
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Right Justified / decimal point alignment

 
0
  #7
Jan 31st, 2005
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!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,619
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Right Justified / decimal point alignment

 
0
  #8
Jan 31st, 2005
>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++.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,985
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 927
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Right Justified / decimal point alignment

 
0
  #9
Jan 31st, 2005
Originally Posted by Narue
>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!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC