std::ostream& operator<<(std::ostream& o, const mat4& m)
{
o << std::setprecision(6) << std::fixed << std::right;
const int w = 8;
for (int i = 0; i < 4; i++)
{
int j = 4 * i;
o << '[' << std::setw(w) << m[j] << ',' << std::setw(w) << m[1 + j] << ',' << std::setw(w) << m[2 + j] << ',' << std::setw(w) << m[3 + j] << ']' << std::endl;
}
return o;
}
I want to print my data as:
[-1.00000, 1.00000, 123.000,-23.0000]
This is what I want to achieve:
I am thinking of using std::fixed
or std::scientific
but cannot figure out how to achieve the above formatting with iomanip. Can this even be done with iomanip or do I need to implement my own methods?
You want setiosflags(ios_base::showpoint)
#include <iomanip>
#include <iostream>
int main()
{
double m[] = {-1, 1, 123, -23};
const int w = 8;
std::cout << std::setiosflags(std::ios_base::showpoint);
std::cout << '[' << std::setw(w) << m[0] << ',' << std::setw(w) << m[1] << ',' << std::setw(w) << m[2] << ',' << std::setw(w) << m[3] << ']' << std::endl;
}
The result is:
[-1.00000, 1.00000, 123.000,-23.0000]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.