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