Round Function

Oct 18, 2009
363
17
For rounding to the nearest integer the easiest way is to add 0.5 and modulus divide by 1. So, for instance:

set myval=3.49999
echo %@eval[(%myval+.5)\1]
 
Oct 18, 2009
363
17
This also works:

echo %@eval[3.14=0]
3
echo %@eval[3.57=0]
4

Good point!

That probably should be mentioned expressly in the Help. When I first saw the post I thought "That looks like a programming error--he's comparing 3.14 to 0?? In a math function, not a logic function?"
 
Oct 18, 2009
363
17
I see it documented at the bottom of the @EVAL help page.... the part that starts with "if displayformat is i.a, then..."
I did see that--but it's not clear that setting it to 0 will cause a simple "round to nearest integer".

I doubt that most people would think "Why not use @eval and set the precision to 0?" Like the original poster, most people would be thinking "Where is the @ROUND[] function? Maybe TCC uses a different name?"
 
May 29, 2008
572
4
Groton, CT
I did see that--but it's not clear that setting it to 0 will cause a simple "round to nearest integer".

I doubt that most people would think "Why not use @eval and set the precision to 0?" Like the original poster, most people would be thinking "Where is the @ROUND[] function? Maybe TCC uses a different name?"
Yeah? Well *I* thought of it. Maybe it's because I'm an ancient FORTRAN programmer. :-)