# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# Round Function

#### Joe Caverly

While TCC does not have a round function, the PSHELL command and/or function can be used to round a number;
Code:
``````echo %@pshell[[math]::Round(22/7)]
3``````

..or...
Code:
``````pshell /s "\$a=22/7"
echo %@pshell[\$a]
3.14285714285714

echo %@pshell[[math]::Round(\$a)]
3``````

Joe

The .NET [math]::Round Method offers additional conventions. Take a look at the Microsoft documentation for more, including;

Rounding Away From Zero
Round to the nearest, or banker's rounding

...and other examples.

Joe

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]

TCC also has the @CEILING and @FLOOR functions.

This also works:

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

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?"

That probably should be mentioned expressly in the Help.
I see it documented at the bottom of the @EVAL help page.... the part that starts with "if displayformat is i.a, then..."

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?"

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. :-)

Yeah? Well *I* thought of it. Maybe it's because I'm an ancient FORTRAN programmer. :-)

That seems more intuitive to me than launching an instance of PowerShell. Which is kind of like mounting a military invasion of Russia to get a cup of tea.

[FOX] Ultimate Translator
Translate