2 ms·
You could try: %06.*g for width 6 down to 1. I believe this will always find an answer, and the one with the largest number of significant digits. For exa
by BeeOnRope 7y ago
You could try:
%06.*g
for width 6 down to 1.
I believe this will always find an answer, and the one with the largest number of significant digits.
For example:
void format(double val, int width, char* buf) {
for (int w = width; w > 0; w--) {
int count = sprintf(buf, "%06.*g", w, val);
if (count == width) return;
}
}
"Working" example (unsafe, exits on failure, etc):
https://godbolt.org/z/WiaPkT https://godbolt.org/z/WiaPkT
Note that you can't represent negative values equal or below -1e-100 since those take 7 characters.