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