How to round table cell by 2 digit?
nickhuangxinyu opened this issue · comments
xyhuang commented
I add
std::cout<<setiosflags(std::ios::fixed)<<setprecision(2);
in table inline std::ostream &operator<<(std::ostream &stream, const Table &table)
like this:
inline std::ostream &operator<<(std::ostream &stream, const Table &table) {
stream << std::setiosflags(std::ios::fixed) << std::setprecision(2);
const_cast<Table &>(table).print(stream);
return stream;
}
but it still output a lot of digit
+----------+----------+
| huang | asd |
+----------+----------+
| 1.111111 | 1.000000 |
+----------+----------+
can you give me some tips?
Pranav commented
You don't need to do anything to tabulate
to make this happen.
Like I've said before, tabulate
simply takes a list of strings. You can format your data however you want before passing it to tabulate
.
So this works:
#include "tabulate.hpp"
#include <iomanip>
#include <sstream>
auto to_string(double value, std::size_t precision = 2) {
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << value;
return stream.str();
}
int main() {
double pi = 3.14159265359;
double v2 = 0.100000000001;
tabulate::Table table;
table.add_row({to_string(pi), to_string(v2)});
std::cout << table << "\n";
}
will print:
+------+------+
| 3.14 | 0.10 |
+------+------+