There is no exact representation of 34.568, or almost every other decimal
number, in binary floating point. Most implementations of floating
point use binary floating point, including the Intel x86 processors.

Note that I absolutely DON'T CARE what's done with the exactly-halfway-in-between
cases of, say, rounding 1.05 to 1 decimal place. The non-exact representation of
decimal fractions pretty much ensures that the exactly-halfway-in-between situation
will hardly ever occur. If you do care, you'll need to handle it specially.
There is also not much consensus about how the exactly-halfway-in-between case
SHOULD be handled, with the major camps being round up, round away from zero,
and round to even.

Is there a std lib rounding function that will round a real to a given number of decimal places? Something like:

double round_it(double number, int decimal_digits)

pass 34.5678, 3 to it and it returns 34.568

I tried to write one a couple of years back, and posted it here. The general
consensus was that a good function (that rounds to the nearest decimal
representable by the precision of a double) was impossible to write in ANSI
C with any efficency.

You will index beyond the string you are getting
and touching memory you do not own. You assume that
there is a digit at the position of the point + "ndigits".

Another serious problem is that you just increase the
digit before the end alphabetically!!

If you had '9' you will get ':' as the result of your addition.
You do not carry over the digits. '9' should be transformed
into '0' and the same operation should be repeated until
there are no more digits in the whole string. This is called
"carry propagation".

In a more cosmetic way, you could use strchr to eliminate the
loop since you are just looking for a point. You do
char *p = strchr(n,'.');
if (p == NULL)
return n;
int_digits = p - n;

The termination clause of your "for" loop is just that the pointer
points to something different than the '.' char. If you receive
a string without a point you start an infinite loop since the terminating
zero will be ignored (it *is* different than '.') and you will go on
scanning beyond the end of the string with bad consequences:
either a crash or ending in a random fashion when you hit
some byte that has the value of '.'

Using strchr avoids that problem and is maybe faster. strchr
doesn't read beyond the end of the string, respecting the
terminating zero.

But for strings that contain a point, and have more
digits than the specified precision your function will
work. Of course if we do not hit the 9 in the previous
position. :-)

Suppose you receive: round("12.1",5);
The code I posted assumes validity of input. You will index beyond the string you are getting and touching memory you do not own. You assume that there is a digit at the position of the point + "ndigits".
A full-fledged version would return the unmodified 1st argument
if the 2nd argument was >= the number of places available.

Another serious problem is that you just increase the digit before the end alphabetically!!

If you had '9' you will get ':' as the result of your addition. You do not carry over the digits. '9' should be transformed into '0' and the same operation should be repeated until there are no more digits in the whole string. This is called "carry propagation".
Doh! Good call on this. That's what I get for posting code I
wrote in five minutes and tested twice. Back to the drawing board.
But for strings that contain a point, and have more digits than the specified precision your function will work. Of course if we do not hit the 9 in the previous position. :-)