#include <stdio.h>
#include <math.h>
double round_to_digits(double value, int digits)
{
if (value == 0.0) // otherwise it will return 'nan' due to the log10() of zero
return 0.0;
return round(value * factor) / factor;
}
int main(void) {
printf("%.52g\n", round_to_digits
(0.0, 6)); printf("%.52g\n", round_to_digits
(-123.23344553, 6)); printf("%.52g\n", round_to_digits
(-123.23344553, 6)); printf("%.52g\n", round_to_digits
(-123.23344553, 7)); printf("%.52g\n", round_to_digits
(-123.23344553, 8)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+Cgpkb3VibGUgcm91bmRfdG9fZGlnaXRzKGRvdWJsZSB2YWx1ZSwgaW50IGRpZ2l0cykKewogICBpZiAodmFsdWUgPT0gMC4wKSAvLyBvdGhlcndpc2UgaXQgd2lsbCByZXR1cm4gJ25hbicgZHVlIHRvIHRoZSBsb2cxMCgpIG9mIHplcm8KICAgICAgICAgICAgcmV0dXJuIDAuMDsKICAgZG91YmxlIGZhY3RvciA9IHBvdygxMC4wLCBkaWdpdHMgLSBjZWlsKGxvZzEwKGZhYnModmFsdWUpKSkpOwogICByZXR1cm4gcm91bmQodmFsdWUgKiBmYWN0b3IpIC8gZmFjdG9yOyAgIAp9CgppbnQgbWFpbih2b2lkKSB7CgkgCiAgICBwcmludGYoIiUuNTJnXG4iLCByb3VuZF90b19kaWdpdHMoMC4wLCA2KSk7CiAgICBwcmludGYoIiUuNTJnXG4iLCByb3VuZF90b19kaWdpdHMoLTEyMy4yMzM0NDU1MywgNikpOwogICAgcHJpbnRmKCIlLjUyZ1xuIiwgcm91bmRfdG9fZGlnaXRzKC0xMjMuMjMzNDQ1NTMsIDYpKTsKICAgIHByaW50ZigiJS41MmdcbiIsIHJvdW5kX3RvX2RpZ2l0cygtMTIzLjIzMzQ0NTUzLCA3KSk7CiAgICBwcmludGYoIiUuNTJnXG4iLCByb3VuZF90b19kaWdpdHMoLTEyMy4yMzM0NDU1MywgOCkpOwogICAgcmV0dXJuIDA7Cn0=