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 13968 times)

0 Members and 1 Guest are viewing this topic.

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.