I have a set of doubles that I want to print. My requirements are:
- no scientific notation
- Up to 4 digits after a dot, but if there are zeroes, those should be trimmed (including the dot, if possible)
Unfortunately, no formatting library gives a way to achieve precisely that (I think?)
To give an example:
#include <iostream>
#include <fmt/format.h>
#include <iomanip>
int main() {
std::cout << fmt::format("{:.4}n", 123456789.1); //bad: prints 1.235e+08
std::cout << std::setprecision(4) << 123456789.1 << "n"; //as above
std::cout << fmt::format("{:.4f}n", 123456789.1); //bad: prints trailing zeros: 123456789.1000
printf("%.4fn", 123456789.1); //as above
std::cout << std::fixed << std::setprecision(4) << 123456789.1 << "n"; //as above
}
My current solution is more by hand: printing it as {:.4f}
and then manually trimming trailing zeroes. But surely, there must be a more elegant solution?