1.

Solve : VBS/Batchscript hybrid Eval.?

Answer»

Win XP Home 32 bit SP.3+ Cmd.exe

The following script returns Pi with 14 decimal places.  Is it possible to select the number of decimal places returned by Eval please?

Code: [Select]echo off
cls
setlocal 
echo wscript.echo eval(wscript.arguments(0))>%temp%\eval.vbs

set a=22
set b=7

set calc=%a%/%b%
call :calculate
set Pi=%result%

echo %Pi%

del %temp%\eval.vbs

exit /b

:calculate
for /f %%1 in ('cscript //nologo %temp%\eval.vbs "%calc%"') do (
    set result=%%1
)
Why?
Is this homework?
If you need a short  pi, just use a value you want as a global constant.
Of course, it will not be as accurate.

Pi to 12 decimal places is 3.141592653589Evaluate the expression

Code: [Select]Atn(1)*4
(instead of 22/7)Sorry, I didn't mean to ask how to calculate Pi, my question is Is it possible to select the number of decimal places returned by Eval please?     This holds good regardless what the values evaluated are, can Eval return a selected number of decimal places?

I'd like to use Eval when calculating dollar amounts.

Thanks.You are concerned with the visual output, - right?

Eval is common to several script things. Implementations vary. But yes, you can specify how many decimals to use. Read this:
http://en.wikipedia.org/wiki/Eval
The above article is a broad coverage. But it has the vital details.
Here is is the catch, you may have to provide some way for Eval to do what you want it to do. So you might want to use some other method to insure that your output is in a FIXED decimal format.
If you are lazy, use something that defines the output format.

What I want to say is that having two significant decimals is not the same as forcing two decimals in the visual output.
Example:
3.158 could be shown as 3.16
3.14 could be shown as 3.14
3.10 could be shown as 3.1   oops!!
you want a fixed decimal output, like currency, -Right?

Petroutsos writes, "The topic of PRINTING with Visual BASIC is a non-trivial topic...
Mastering Visual Basic .NET [Paperback]
Evangelos Petroutsos (Author)
http://www.amazon.com/Mastering-Visual-Basic-Evangelos-Petroutsos/dp/0782128777

But don't buy the book. Somebody will give you a clear answer.Call me crazy but I bet you can ROUND any number to the length of the decimal you want.1.

Quote

The following script returns Pi with 14 decimal places.

It does not. The fraction 22/7 is NOT equal to pi.

2. To use that VBScript one-liner to give a result rounded to 2 decimal places do this cscript //nologo %temp%\eval.vbs "round(%calc%,2)"

3. To get pi, use 4*ATN(1) as the expression to be evaluated

Code: [Select]c:\>for /f %A in ('cscript //nologo eval.vbs "round(4*ATN(1),5)"') do echo %A
3.14159
Remember to use 2 % signs in a batch (e.g. %%A)


what concerns me is the use of floating point with currency values, so I'll point out the existence of the CCur() function which will convert the VBScript value into a Currency (scaled 64-bit Integer) Variant, which is accurate to 4 decimal places and doesn't suffer from the various gotchas that one finds with floating point.

Quote
Petroutsos writes, "The topic of printing with Visual Basic is a non-trivial topic...
Mastering Visual Basic .NET [Paperback]
Nobody said anything about printing, or .NET, for that matter...Thank you all for your inputs.   

At my fairly basic level of knowledge some details in the replies are beyond my understanding but Round as shown by Salmon Trout does what I was looking for.

Kind regards to all.
Betty.Another option for you as well.
Code: [Select]C:\Users\Squash\batch>for /f %A in ('cscript //nologo eval.vbs "FormatNumber(22/7,9)"') do echo %A
3.142857143 Quote from: Squashman on December 09, 2011, 04:57:34 PM
Another option for you as well.
Code: [Select]C:\Users\Squash\batch>for /f %A in ('cscript //nologo eval.vbs "FormatNumber(22/7,9)"') do echo %A
3.142857143
Does not work for me.
Quote from: Geek-9pm on December 09, 2011, 05:31:46 PM
Does not work for me.
Did you create the eval.vbs script first?
Code: [Select]C:\Users\Squash\batch>echo wscript.echo eval(wscript.arguments(0))>eval.vbs

C:\Users\Squash\batch>for /f %A in ('cscript //nologo eval.vbs "FormatNumber(22/7,5)"') do echo %A
3.14286

C:\Users\Squash\batch>for /f %A in ('cscript //nologo eval.vbs "FormatNumber(4*ATN(1),5)"') do echo %A
3.14159

C:\Users\Squash\batch>for /f %A in ('cscript //nologo eval.vbs "FormatNumber(4*ATN(1),9)"') do echo %A
3.141592654if you don't want to round but rather want to truncate values:

Code: [Select]Function Truncate(ByVal Value,Byval NumDigits)
    Dim digitfactor
    digitfactor = (10^numdigits)
    Truncate = Fix(Value * digitfactor)/digitfactor
End Function
or just use that as part of the expression as needed, with eval.vbs.

Code: [Select]
for /f %A in ('cscript //nologo eval.vbs "Fix(Atn(1)*4*(10^4))/(10^4)"') do echo %A
replacing 4 with the desired number of significant digits. this should give back 3.1415 (rather than a rounded value of 3.1416)The OP started with the 22/7 thing and then later said she wanted to represent decimals like in dollar$. That is confusing. Who would ever calculate dollar values using a bad form of Pi?

And if for some reason you had to use Pi to calculate some kind of monetary thing, and if it was ever recursive or iterative, Pi would have to have more that just two decimals. The error would accumulate.

 Dollars requires a currency format. It has rules that go beyond Eval and Round and things like that. Currency imposes a strict format for the output as it would appear on a console or printer.

I still do not understand her intended use. Setting the number of decimal places does not mean that the output is consistent with a range of values that may occur in monetary transactions. They teach that in Computers 101. Is the OP just trying to bait us?
I'm back again to try to explain...

My original query was Is it possible to select the number of decimal places returned by Eval please? .   As simple as that.   

I now realise that I should not have posted referring to Pi or to currency amounts, those two subjects may have clouded the issue.   I had, and still have, no intention of calculating Pi, or using Pi in any calculation, the script was used as an example only and in my earlier vain attempts to solve my query without referring it to the forum.   All I wanted to know was if the number of decimal places returned by Eval could be selected.   

However, the experience has had its benefits, I now have several options and have enhanced my scant knowledge base.   It was never my intention to bait anyone, as a newbie I'm not clever enough or devious enough to do that, or to waste the very VALUABLE time of the forum gurus

If I ever post another query rest assured I will have given the format and WORDING of the query a lot more consideration than I did on this occasion.

Again thanks to all, please consider the subject closed.

Betty



Discussion

No Comment Found