C
Christoph Bartoschek
Hi,
I would like to print floating point numbers such that non-scientific output
is used and all significant digits are printed:
template <typename T> void print_number(T num) {
std::cout << ...
}
Here are examples of what I would expect if the floating point type has 6
significant digits:
float a = 0.5;
print_number(a) gives 0.5
// Trailing zeros are not printed
float b = 0.000000000012345678;
print_number(b) gives 0.0000000000123456
// Because non-scientific output is used the output is quite large
float c = 1234567.89;
print_number(c) gives 1234560
// Only the significant digits are printed. A trailing .0 would also be ok.
Is there a way to achieve the desired result by a simple function? I do not
want to reimplement the printf functionality.
Thanks,
Christoph
I would like to print floating point numbers such that non-scientific output
is used and all significant digits are printed:
template <typename T> void print_number(T num) {
std::cout << ...
}
Here are examples of what I would expect if the floating point type has 6
significant digits:
float a = 0.5;
print_number(a) gives 0.5
// Trailing zeros are not printed
float b = 0.000000000012345678;
print_number(b) gives 0.0000000000123456
// Because non-scientific output is used the output is quite large
float c = 1234567.89;
print_number(c) gives 1234560
// Only the significant digits are printed. A trailing .0 would also be ok.
Is there a way to achieve the desired result by a simple function? I do not
want to reimplement the printf functionality.
Thanks,
Christoph