On Feb 25, 2:18*am, jacob navia <ja...@nospam.comwrote:

Hi

"How can I round a number to x decimal places" ?

This question keeps appearing. I would propose the following

solution

#include <float.h>

#include <math.h>

double roundto(double x, int digits)

{

* * * * int sgn=1;

* * * * if (x == 0)

* * * * * * * * return 0;

* * * * if (digits DBL_DIG)

* * * * * * * * digits = DBL_DIG;

* * * * else if (digits < 1)

* * * * * * * * digits = 1;

* * * * if(x < 0.0) {

* * * * * * * * sgn = -sgn;

* * * * * * * * x = -x;

* * * * }

* * * * double p = floorl(log10l(x));

* * * * p--;

* * * * p = digits-p;

* * * * double pow10 = pow(10.0, p);

* * * * return sgn*floor(x*pow10+0.5)/pow10;

}

long double roundtol(long double x, int digits)

{

* * * * int sgn=1;

* * * * if (x == 0)

* * * * * * * * return 0;

* * * * if (digits LDBL_DIG)

* * * * * * * * digits = LDBL_DIG;

* * * * else if (digits < 1)

* * * * * * * * digits = 1;

* * * * if(x < 0.0) {

* * * * * * * * sgn = -sgn;

* * * * * * * * x = -x;

* * * * }

* * * * long double p = floorl(log10l(x));

* * * * p--;

* * * * p = digits-p;

* * * * long double pow10 = powl(10.0, p);

* * * * return sgn*floorl(x*pow10+0.5)/pow10;}

#include <stdio.h>

int main(void)

{

* * * * double d = 1.7888889988996678;

* * * * long double ld = 1.7888889988996678998877L;

* * * * for (int i = 0; i<=DBL_DIG;i++) {

* * * * * * * * printf("i=%d: %.15g\n",i,roundto(d,i));

* * * * }

* * * * printf("\n * * *1.7888889988996678\n");

* * * * for (int i = 0; i<=LDBL_DIG;i++) {

* * * * * * * * printf("i=%d: %.18Lg\n",i,roundtol(ld,i));

* * * * }

* * * * printf("\n * * *1.7888889988996678998877L\n");

* * * * return 0;

}

--------------------------------------------------------------------

I would propose it to add it to the FAQ.

The snippets solution, tweaked a little bit:

#include <math.h>

/* round number n to d decimal points */

long double roundl(const long double n, const unsigned d)

{

long double p = powl(10., d);

return floorl(n * p + .5) / p;

}

/* round number n to d decimal points */

double roundd(const double n, const unsigned d)

{

return (double) roundl((long double) n, d);

}

/* round number n to d decimal points */

double roundf(const float n, const unsigned d)

{

return (float) roundl((long double) n, d);

}

#ifdef UNIT_TEST

#include <stdio.h>

#include <stdlib.h>

#include <float.h>

int main(void)

{

long double pi = 3.1415926535897932384626433832795;

long double npi = -pi;

unsigned digits;

for (digits = 0; digits <= LDBL_DIG; digits++) {

printf("Rounding by printf gives: %20.*f\n",

digits, pi);

printf("Rounding approximation by function gives: %20.*f\n",

LDBL_DIG, roundl(pi, digits));

printf("Rounding approximation by function gives: %20.*f\n",

DBL_DIG, roundd(pi, digits));

printf("Rounding approximation by function gives: %20.*f\n\n",

FLT_DIG, roundf(pi, digits));

}

for (digits = 0; digits <= LDBL_DIG; digits++) {

printf("Rounding by printf gives: %20.*f\n",

digits, npi);

printf("Rounding approximation by function gives: %20.*f\n",

LDBL_DIG, roundl(npi, digits));

printf("Rounding approximation by function gives: %20.*f\n",

DBL_DIG, roundd(npi, digits));

printf("Rounding approximation by function gives: %20.*f\n\n",

FLT_DIG, roundf(npi, digits));

}

return 0;

}

#endif

/*

Rounding by printf gives: 3

Rounding approximation by function gives: 3.000000000000000

Rounding approximation by function gives: 3.000000000000000

Rounding approximation by function gives: 3.000000

Rounding by printf gives: 3.1

Rounding approximation by function gives: 3.100000000000000

Rounding approximation by function gives: 3.100000000000000

Rounding approximation by function gives: 3.100000

Rounding by printf gives: 3.14

Rounding approximation by function gives: 3.140000000000000

Rounding approximation by function gives: 3.140000000000000

Rounding approximation by function gives: 3.140000

Rounding by printf gives: 3.142

Rounding approximation by function gives: 3.142000000000000

Rounding approximation by function gives: 3.142000000000000

Rounding approximation by function gives: 3.142000

Rounding by printf gives: 3.1416

Rounding approximation by function gives: 3.141600000000000

Rounding approximation by function gives: 3.141600000000000

Rounding approximation by function gives: 3.141600

Rounding by printf gives: 3.14159

Rounding approximation by function gives: 3.141590000000000

Rounding approximation by function gives: 3.141590000000000

Rounding approximation by function gives: 3.141590

Rounding by printf gives: 3.141593

Rounding approximation by function gives: 3.141593000000000

Rounding approximation by function gives: 3.141593000000000

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.1415927

Rounding approximation by function gives: 3.141592700000000

Rounding approximation by function gives: 3.141592700000000

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.14159265

Rounding approximation by function gives: 3.141592650000000

Rounding approximation by function gives: 3.141592650000000

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.141592654

Rounding approximation by function gives: 3.141592654000000

Rounding approximation by function gives: 3.141592654000000

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.1415926536

Rounding approximation by function gives: 3.141592653600000

Rounding approximation by function gives: 3.141592653600000

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.14159265359

Rounding approximation by function gives: 3.141592653590000

Rounding approximation by function gives: 3.141592653590000

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.141592653590

Rounding approximation by function gives: 3.141592653590000

Rounding approximation by function gives: 3.141592653590000

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.1415926535898

Rounding approximation by function gives: 3.141592653589800

Rounding approximation by function gives: 3.141592653589800

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.14159265358979

Rounding approximation by function gives: 3.141592653589790

Rounding approximation by function gives: 3.141592653589790

Rounding approximation by function gives: 3.141593

Rounding by printf gives: 3.141592653589793

Rounding approximation by function gives: 3.141592653589793

Rounding approximation by function gives: 3.141592653589793

Rounding approximation by function gives: 3.141593

Rounding by printf gives: -3

Rounding approximation by function gives: -3.000000000000000

Rounding approximation by function gives: -3.000000000000000

Rounding approximation by function gives: -3.000000

Rounding by printf gives: -3.1

Rounding approximation by function gives: -3.100000000000000

Rounding approximation by function gives: -3.100000000000000

Rounding approximation by function gives: -3.100000

Rounding by printf gives: -3.14

Rounding approximation by function gives: -3.140000000000000

Rounding approximation by function gives: -3.140000000000000

Rounding approximation by function gives: -3.140000

Rounding by printf gives: -3.142

Rounding approximation by function gives: -3.142000000000000

Rounding approximation by function gives: -3.142000000000000

Rounding approximation by function gives: -3.142000

Rounding by printf gives: -3.1416

Rounding approximation by function gives: -3.141600000000000

Rounding approximation by function gives: -3.141600000000000

Rounding approximation by function gives: -3.141600

Rounding by printf gives: -3.14159

Rounding approximation by function gives: -3.141590000000000

Rounding approximation by function gives: -3.141590000000000

Rounding approximation by function gives: -3.141590

Rounding by printf gives: -3.141593

Rounding approximation by function gives: -3.141593000000000

Rounding approximation by function gives: -3.141593000000000

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.1415927

Rounding approximation by function gives: -3.141592700000000

Rounding approximation by function gives: -3.141592700000000

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.14159265

Rounding approximation by function gives: -3.141592650000000

Rounding approximation by function gives: -3.141592650000000

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.141592654

Rounding approximation by function gives: -3.141592654000000

Rounding approximation by function gives: -3.141592654000000

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.1415926536

Rounding approximation by function gives: -3.141592653600000

Rounding approximation by function gives: -3.141592653600000

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.14159265359

Rounding approximation by function gives: -3.141592653590000

Rounding approximation by function gives: -3.141592653590000

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.141592653590

Rounding approximation by function gives: -3.141592653590000

Rounding approximation by function gives: -3.141592653590000

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.1415926535898

Rounding approximation by function gives: -3.141592653589800

Rounding approximation by function gives: -3.141592653589800

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.14159265358979

Rounding approximation by function gives: -3.141592653589790

Rounding approximation by function gives: -3.141592653589790

Rounding approximation by function gives: -3.141593

Rounding by printf gives: -3.141592653589793

Rounding approximation by function gives: -3.141592653589793

Rounding approximation by function gives: -3.141592653589793

Rounding approximation by function gives: -3.141593

*/