Formatting output in C++


Formatting output in C++



In a C++ code I have a matrix of double variables which I print out. However because all of them have different number of digits, the output format is destroyed. One solution is to do
cout.precision(5) but I want different columns have a different precision. Also, because there are negative values in some cases, the presence of the - sign also causes problems. How to get around this and produce a properly formatted output?


cout.precision(5)


-




6 Answers
6



Off the top of my head, you can use setw(int) to specify the width of the output.



like this:


std::cout << std::setw(5) << 0.2 << std::setw(10) << 123456 << std::endl;
std::cout << std::setw(5) << 0.12 << std::setw(10) << 123456789 << std::endl;



gives this:


0.2 123456
0.12 123456789





just FYI : while doing std::setw(x) make sure x is greater then your decimal precision.
– solti
Dec 24 '16 at 7:26



The key is, as others have said, to use manipulators. What they
neglected to say is that you normally use manipulators that you write
yourself. An FFmt manipulator (which corresponds to the F format in
Fortran is fairly easy:


FFmt


F


class FFmt
{
int myWidth;
int myPrecision;
public:
FFmt( int width, int precision )
: myWidth( width )
, myPrecision( precision )
{
}

friend std::ostream&
operator<<( std::ostream& dest, FFmt const& fmt )
{
dest.setf( std::ios_base::fixed, std::ios_base::formatfield );
dest.precision( myPrecision );
dest.width( myWidth );
return dest;
}
};



This way, you can define a variable for each column, say:


FFmt col1( 8, 2 );
FFmt col2( 6, 3 );
// ...



and write:


std::cout << col1 << value1
<< ' ' << col2 << value2...



In general, except in the simplest programs, you should probably not be
using the standard manipulators, but rather custom manipulators based on
your application; e.g. temperature and pressure if that's the sort of
thing your dealing with. In this way, it's clear in the code what
you're formatting, and if the client suddenly asks for one more digit in
the pressure, you know exactly where to make the change.


temperature


pressure





Remember that you need to include <iomanip> to make it find setw ()
– user1603472
Jun 18 '14 at 13:37





@user1603472 Yes, but my code doesn't use std::setw.
– James Kanze
Jun 18 '14 at 17:10


std::setw





sorry, my bad, i clicked the wrong place, was a quick comment to put in
– user1603472
Jun 18 '14 at 18:13



Use manipulators.



From sample here:


#include <iostream>
#include <iomanip>
#include <locale>
int main()
{
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "Left fill:n" << std::left << std::setfill('*')
<< std::setw(12) << -1.23 << 'n'
<< std::setw(12) << std::hex << std::showbase << 42 << 'n'
<< std::setw(12) << std::put_money(123, true) << "nn";

std::cout << "Internal fill:n" << std::internal
<< std::setw(12) << -1.23 << 'n'
<< std::setw(12) << 42 << 'n'
<< std::setw(12) << std::put_money(123, true) << "nn";

std::cout << "Right fill:n" << std::right
<< std::setw(12) << -1.23 << 'n'
<< std::setw(12) << 42 << 'n'
<< std::setw(12) << std::put_money(123, true) << 'n';
}



Output:


Left fill:
-1.23*******
0x2a********
USD *1.23***

Internal fill:
-*******1.23
0x********2a
USD ****1.23

Right fill:
*******-1.23
********0x2a
***USD *1.23



Take a look at stream manipulators, especially std::setw and std::setfill.


std::setw


std::setfill


float f = 3.1415926535;
std::cout << std::setprecision(5) // precision of floating point output
<< std::setfill(' ') // character used to fill the column
<< std::setw(20) // width of column
<< f << 'n'; // your number



Try using setw manipulator. Please refer http://www.cplusplus.com/reference/iostream/manipulators/setw/ for further information



There is a way using i/o manipulators, but I find it unwieldy. I would just write a function like this:


template<typename T>
std::string RightAligned(int size, const T & val)
{
std::string x = boost::lexical_cast<std::string>(val);
if (x.size() < size)
x = std::string(size - x.size(), ' ') + x;
return x;
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?