Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VBS/Batchscript hybrid Eval.  (Read 13969 times)

0 Members and 1 Guest are viewing this topic.

Betty

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows XP
    VBS/Batchscript hybrid Eval.
    « on: December 08, 2011, 10:51:10 PM »
    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
    )
    To the world you are just one person,
    To one person you are the world.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: VBS/Batchscript hybrid Eval.
    « Reply #1 on: December 08, 2011, 10:58:30 PM »
    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.141592653589

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS/Batchscript hybrid Eval.
    « Reply #2 on: December 08, 2011, 11:51:08 PM »
    Evaluate the expression

    Code: [Select]
    Atn(1)*4
    (instead of 22/7)
    I was trying to dereference Null Pointers before it was cool.

    Betty

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows XP
      Re: VBS/Batchscript hybrid Eval.
      « Reply #3 on: December 09, 2011, 01:34:54 AM »
      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.
      To the world you are just one person,
      To one person you are the world.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: VBS/Batchscript hybrid Eval.
      « Reply #4 on: December 09, 2011, 02:29:01 AM »
      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.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: VBS/Batchscript hybrid Eval.
      « Reply #5 on: December 09, 2011, 05:50:08 AM »
      Call me crazy but I bet you can ROUND any number to the length of the decimal you want.

      Salmon Trout

      • Guest
      Re: VBS/Batchscript hybrid Eval.
      « Reply #6 on: December 09, 2011, 08:21:11 AM »
      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)


      « Last Edit: December 09, 2011, 08:42:31 AM by Salmon Trout »

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: VBS/Batchscript hybrid Eval.
      « Reply #7 on: December 09, 2011, 02:22:08 PM »
      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...
      I was trying to dereference Null Pointers before it was cool.

      Betty

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows XP
        Re: VBS/Batchscript hybrid Eval.
        « Reply #8 on: December 09, 2011, 02:40:15 PM »
        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.
        To the world you are just one person,
        To one person you are the world.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: VBS/Batchscript hybrid Eval.
        « Reply #9 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

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: VBS/Batchscript hybrid Eval.
        « Reply #10 on: December 09, 2011, 05:31:46 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.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: VBS/Batchscript hybrid Eval.
        « Reply #11 on: December 09, 2011, 06:15:12 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.141592654

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: VBS/Batchscript hybrid Eval.
        « Reply #12 on: December 09, 2011, 07:06:40 PM »
        if 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)
        I was trying to dereference Null Pointers before it was cool.

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: VBS/Batchscript hybrid Eval.
        « Reply #13 on: December 09, 2011, 09:00:00 PM »
        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?

        Betty

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows XP
          Re: VBS/Batchscript hybrid Eval.
          « Reply #14 on: December 10, 2011, 12:36:52 AM »
          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

          To the world you are just one person,
          To one person you are the world.

          Salmon Trout

          • Guest
          Re: VBS/Batchscript hybrid Eval.
          « Reply #15 on: December 10, 2011, 01:47:55 AM »
          Why bother using a batch script to create (and leave behind, I daresay) a VBScript which does non-integer arithmetic (the results of which you can't use in a batch) when you can just use VBScript? It's not all that difficult.

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: VBS/Batchscript hybrid Eval.
          « Reply #16 on: December 10, 2011, 09:22:57 AM »
          Betty, thanks for coming back. Sorry for any rude remarks.

          As has been stated, Eval()  in VBscript only takes a single argument. To force it to be only two decimal places takes something additional.

          For people who work in IT, but are not programmers, a tool called  PowerShell is currently the commendation of Microsoft. And even some programmers prefer it for everyday tasks.

          Salmon Trout

          • Guest
          Re: VBS/Batchscript hybrid Eval.
          « Reply #17 on: December 10, 2011, 11:40:20 AM »
          Quote from: Geek
          Eval()  in VBscript only takes a single argument. To force it to be only two decimal places takes something additional.

          No it doesn't. This has already been shown! The argument can be more or less any string.

          example

          Code: [Select]
          C:\>eval round(4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)+(1/17)-(1/19)+(1/21)-(1/23)+(1/25)),2)
          3.22

          If one typed more terms one would get a closer approximation to π

          I see some other false stuff has been posted by another; I shall not comment: it won't be there for long. I thought that VBScript, like most languages, does indeed calculate trig functions. They are produced internally by using the Taylor series, aren't they? (Or is it Cordic now?) or are they interpolated from a table? Anybody (Anyone who actually knows, that is)?





          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: VBS/Batchscript hybrid Eval.
          « Reply #18 on: December 10, 2011, 12:52:03 PM »
          No it doesn't. This has already been shown! The argument can be more or less any string.
          ...
          Pi is not longer the issue. But my choice is banana crane.
          But eval()  in VBS takes  one argument. In the syntax it says one argument. The argument and be an expression or a function.
          My impression was the he OP wanted something simple where the number of decimals could be specifically given.
          Suppose there was a thing called fancyeval()  that take a number and a string. and returns a string.  then you could say
          fancyeval(23.1,"$####.##") and it would give
          $  23.10
          Of course, there is not such thing called fancyeval() in VBS.
          The OP at one time mentioned something about currency. She did not make it clear if she wants to do currency formatting. There is a data type for currency, but I do not think it is in VBS.

          There is a thing called:
          FormatCurrency Format a number with a currency symbol
          http://ss64.com/vb/formatcurrency.html
          Quote
          Format a number with a currency symbol.

          Syntax
                FormatCurrency(number [,DecimalPlaces [,IncludeLeadingZero
                    [,UseParenthesis [, GroupDigits]]]] )

          Key
             number              The number to format.
           
             DecimalPlaces       Number of digits to display after the decimal point.

             IncludeLeadingZero  Include a leading zero for numbers <1 and > -1

             UseParenthesis      Show negative numbers in Parentheis (500) = -500

             GroupDigits         Group large numbers with commas (or the regional delimiter)

          Example

          Set intDemo = 150000.56
          WScript.Echo FormatCurrency(intDemo,1)

          “The best way to destroy the capitalist system is to debauch the currency. By a continuing process of inflation, governments can confiscate, secretly and unobserved, an important part of the wealth of their citizens” - John Maynard Keynes - W. Edwards Deming




          Salmon Trout

          • Guest
          Re: VBS/Batchscript hybrid Eval.
          « Reply #19 on: December 10, 2011, 01:30:58 PM »
          Pi is not longer the issue. But my choice is banana crane.

          That is not the only piece of nonsense in your post. Why do you continually plague us with this sort of thing?

          Quote
          But eval()  in VBS takes  one argument. In the syntax it says one argument. The argument and be an expression or a function

          You wrote that, and then proceeded to demonstrate that you failed to understand its meaning.

          Quote
          My impression was the he OP wanted something simple where the number of decimals could be specifically given.
          Suppose there was a thing called fancyeval()  that take a number and a string. and returns a string

          You have FormatNumber(Expression [,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]])

          Code: [Select]
          C:\>Eval FormatNumber(23.1,2)
          23.10

          Quote
          There is a data type for currency, but I do not think it is in VBS.

          Code: [Select]
          C:\>eval FormatCurrency(23.1,2)
          £23.10

          Note we use pound signs here where I live.

          Maybe you should take a breather, Geek, and stop digging?
          « Last Edit: December 10, 2011, 01:49:42 PM by Salmon Trout »

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: VBS/Batchscript hybrid Eval.
          « Reply #20 on: December 10, 2011, 01:49:18 PM »
          Quote
          In VB 6, the Currency data type was designed for financial calculations. But Microsoft decided that it just didn't do the job so they dropped it and now we have the Decimal data type in VB.NET.

          This article tells you all about the Decimal data type in VB.NET: What's new, what works and what doesn't. Like the rest of .NET, Decimal is far more powerful. And like the rest of .NET, there are hidden traps. Just to get started, here's one you might not have seen before:

          If you just happened to use the VB 6 Currency data type to create a record in a file using a structure like this one ...

          Private Type FileRecord
             Field1 As Integer
             CurrencyField As Currency
             Field2 As Double
          End Type
          http://visualbasic.about.com/od/usingvbnet/a/decdatatype.htm
          If this is of interest, click on the ink above.
          The only point I want to make is that round() is not the way to format  When doing financial things,  the correct format is very important

          The quote below is not code.
          Quote
          C:\>Eval FormatNumber(23.1,2)
          23.10


          Salmon Trout

          • Guest
          Re: VBS/Batchscript hybrid Eval.
          « Reply #21 on: December 10, 2011, 01:50:39 PM »
          I thought that many accounting applications intended for serious work use cents to avoid rounding errors.

          « Last Edit: December 10, 2011, 02:23:23 PM by Salmon Trout »

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: VBS/Batchscript hybrid Eval.
          « Reply #22 on: December 10, 2011, 02:08:40 PM »
          Salmon Trout, Are you British? You have little sense of humor.  ;D

          Salmon Trout

          • Guest
          Re: VBS/Batchscript hybrid Eval.
          « Reply #23 on: December 10, 2011, 02:15:41 PM »
          Salmon Trout, Are you British? You have little sense of humor.  ;D

          Yes, I am British, (actually born in England to Syrian mother and Jamaican father) and I thought I had a reasonably developed sense of humour, (but I cannot stand Monty Python!) however it is sometimes difficult to convey and perceive humour in web forum posts, I guess.

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: VBS/Batchscript hybrid Eval.
          « Reply #24 on: December 10, 2011, 02:25:15 PM »
          OK.  :)
          They say the Brits are slow to to laugh.
          So from now on I will only joke with you on Friday.
          That way you can start Monday with a smile.

          Salmon Trout

          • Guest
          Re: VBS/Batchscript hybrid Eval.
          « Reply #25 on: December 10, 2011, 02:30:26 PM »
          The quote below is not code.
          Quote
          C:\>Eval FormatNumber(23.1,2)
          23.10


          What do you mean? It shows an expression being passed as an argument to this VBScript one-liner, and the output:

          Code: [Select]
          Wscript.echo eval(WScript.Arguments(0))
          When the script runs, it substitutes the passed argument and therefore the operation is as if the line was this:

          Code: [Select]
          Wscript.echo eval("FormatNumber(23.1,2)")
          The WScript evaluates a valid expression.

          It produce the same output as this VBScript code:

          Code: [Select]
          Wscript.echo FormatNumber(23.1,2)
          Not sure what you're trying to say there Geek.







          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: VBS/Batchscript hybrid Eval.
          « Reply #26 on: December 10, 2011, 02:48:29 PM »
          Quote
          Not sure what you're trying to say there Geek.
          The above dis a quote.
          Code: [Select]
          Not sure what you're trying to say there GeekThe above is not code.

          Code should be ready to o code. So the user can just copy and paste it into whatever project he is doing.
          The code below can work inside of VBS
          Code: [Select]
          Wscript.echo eval("FormatNumber(23.1,2)")

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: VBS/Batchscript hybrid Eval.
          « Reply #27 on: December 10, 2011, 04:52:45 PM »
          Code should be ready to o code. So the user can just copy and paste it into whatever project he is doing.
          And who put you in charge of this type of thing? Besides, your example wouldn't work with your special rules either. They can't paste VBScript code into a batch file and have it work. Monotype is used either to indicate source code or output.

          Anyway, the reason that there is no "FancyEval" is because it would be redundant. It wouldn't even make sense. What would it's preconditions be? What would this return with your fictitious "fancyEval" function:

          Code: [Select]
          WScript.Echo FancyEval("Left$(""test"",2)"),2)
          How do you "round" a string? does the function not allow for expressions containing strings? What about intermediate expressions dealing with strings? Is it only the result that must be a number? From the sounds of things all your fancy eval is is a cheap wrapper around FormatNumber. Which again, is redundant. If the person calling eval wants to round values, they can use the built-in functions for it.

          Some information on Eval's implementation; It's based on JScript's Eval() Function and discusses why VBScript has three (Execute(), ExecuteGlobal(), and Eval()). It's also important to note how there is no restriction, arbitrary or otherwise, on the return value of Eval(). Adding a parameter like "NumDigitsAfterDecimal" to Eval (or a variant thereof) would have the aforementioned issue that "DigitsAfterDecimal" Doesn't make sense for Strings, Objects, or Anything that isn't a number. How would you round a FileSystemObject to 2 decimal places? Is the parameter optional? What happens if you don't pass it? Can it ever return anything that isn't a number? What happens if it deals in strings/objects/etc? The parameter wouldn't make sense, and this nor would the function. In what way does the added parameter make the Evaluation "Fancy", anyway?

          Quote
          If this is of interest, click on the ink above.
          The only point I want to make is that round() is not the way to format  When doing financial things,  the correct format is very important
          As long as they aren't storing the results from the round and are instead just using it for output, it doesn't matter. That's more an "exercise  for the reader". How does it feel the echo exactly what I already said in regards to the Currency Variant Subtype, by the way? VBScript is NOT VB.NET and it's NOT VB6. If you are going to link docs- link to docs for VBScript.

          I was trying to dereference Null Pointers before it was cool.

          Salmon Trout

          • Guest
          Re: VBS/Batchscript hybrid Eval.
          « Reply #28 on: December 10, 2011, 05:16:29 PM »
          BC_P, bottom line is: Geek was talking what we in the UK call "bollocks". He has got away with it for far too long. At times (and this is definitely one of those times) he is as stupid and useless as Bill Richardson, and it astonishes me that measures have not been taken.