User Misha ( posted:

There are many different ways to do the rounding, I think Gary had 5 at last

Which is appropriate depends on the type of numbers and decimal places you
will be wanting, also whether the accuracy is important enough to justify
the extra functions.

Following is a short comparison of the default sprintf rounding plus two
other methods, if you paste the print log into something with fixed width
text font, it should line up a bit nicer.

Note that accuracy also depends on the size of the number you use, really
big ones will cause problems.

For perfect results everytime, you could stick with strings only (I have not
shown how to do this , or export to an external function.

I have included a six decimal place number for Murad, enjoy!

Note if you use these functions I would also recommend testing them across
the range of numbers you will use to make sure they are supported.

# test some custom rounding functions

# generally lose accuracy with large numbers,
# workaround is to define large numbers as string instead

# default (dp is decimal place, n is number)
function round_1(dp, n)
return sprintf("%."dp"f", n);

# add 0.005 (eg) only if needed, use mostly string functions
function round_2(dp, n)
auto pos;
return sprintf("%."dp"f",

# multiply by 10 to power of required decimal places, this gets the digit to
be rounded
# immediately to the right of the decimal point.
# then round it and restore by dividing by same powers of 10
function round_3(dp, n)
return sprintf("%."dp"f", int("" n*10**dp+0.5)/10**dp);

public testvalue [ ] = {2.295, 2.395, 2.2295, 22222.2222295,
"22222.2222295", "137543467.295"};

for (i=0;i<=5;i++)
printf("\r\ntest value: " testvalue[i]);
for (dp=0;dp<=6;dp++)
printf("%2d : %-12s , %-12s ,