Re: Number Formatting in VBSCRIPT



JRS: In article <gCRgf.7699$ZX3.3102@trnddc06>, dated Wed, 23 Nov 2005
03:46:20, seen in news:microsoft.public.scripting.vbscript, LakeGator
<Not@xxxxxxxxxxxxxxxxxxxxxx> posted :
>
>"Dr John Stockton" <jrs@xxxxxxxxxxxxxxxxxx> wrote in message
>news:QvNIH9Fe$3gDFwmu@xxxxxxxxxxxxxxxxxxxxx
>> JRS: In article <1132677795.105379.242600@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
>> , dated Tue, 22 Nov 2005 08:43:15, seen in news:microsoft.public.scripti
>> ng.vbscript, LakeGator <LakeGator@xxxxxxxxx> posted :
>>
>>>>Try to format a number in VBSCRIPT and ASP so that it represents the
>>>>full four digit requirement for my database, i.e 0007
>>
>> Can anyone suggest an easy way of doing this ?
>>
>>>One approach is to concatenate enough zeroes in front of the target
>>>number and then display the rightmost number of digits you wish:
>>>
>>>intSeven = 7
>>>strSeven = Right("000"&intSeven,4)
>>>Wscript.Echo strSeven
>>
>> FWIW, I expect it to be quicker to add arithmetically :
>>
>> strSeven = Right(10000+intSeven,4)
>>
>Once again, this may be technically correct. Using the arithmetic approach
>would seem to probably save many nanoseconds and is a better approach if you
>can assure that intSeven will be a number. If some bad condition occurred
>that caused a non-numeric value then the arithmetic raises an error which
>should be considered.

If that which is expected to be a number is in fact a non-number, that's
an error anyway; if the system does not detect the error, the user may
not.

>Most of us who use scripting languages are not overly concerned with shaving
>nanoseconds off of operations as we understand that scripts are slow by
>nature as they are not compiled nor optimized. Scripts are generally
>intended for quick and dirty operations.

They are often executed many times in total. But it's valuable to be
always aware of the possibility of being more efficient - a small gain
for negligible effort is still a profit; and sometimes the attitude
leads to a much larger gain.

>Just out of curiosity I decided to test how much difference the approach
>takes. To my surprise the good doctor's approach actually takes 44% MORE
>elapsed time than my dumber approach.

On your system, in the way that you tested it.

For me, my way is over twice as fast at a DOS prompt with cscript, and
over three times as fast on a Web page. Perhaps your timing approach
was dumber.

> I am not as concerned with shaving
>nanoseconds so have no great interest in knowing exactly why. My guess is
>that it could be VBScript having to do multiple sub-type conversions going
>between numeric and string data types.

Which I expect you made it do.

Starting with a Number, both methods use a Right(, 4) and a Number-to-
String. Yours uses a string concatenation, mine uses an addition.


>> --
>> ©
>> ...

Please don't quote signatures.

--
© John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
.



Relevant Pages

  • Re: Message for Duane Hookum re Concatenate
    ... Function Concatenate(pstrSQL As String, _ ... >>Doug Steele, Microsoft Access MVP ... >>> in Duanes code? ... >>>>> Concatenate code which Ive used successfully on a ...
    (microsoft.public.access.modulesdaovba)
  • Re: Excel needs to expand text concatenation capability
    ... Optional DelimitWith As String) As String ... > Optional DelimitWith As String) As String ... > Dim Cel As Range ... >> I would love to be able to concatenate any non-blank entries from a list. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: append memo fields together sequentially
    ... Did you go to the site and download the database and then extract the function and paste it into a module in your database. ... did you save the module with a name OTHER than Concatenate. ... Optional pstrDelim As String = ", ... Dim rs As DAO.Recordset ...
    (microsoft.public.access.queries)
  • Re: Concatenate Function
    ... I am attempting to use Duane Hookom's concatenate function (reproduced ... Function Concatenate(pstrSQL As String, Optional pstrDelim As String = "; ... Dim rs As DAO.Recordset ... Dim strConcat As String 'build return string ...
    (microsoft.public.access.modulesdaovba)
  • Re: [Patch] Support UTF-8 scripts
    ... shell scripts either, but both kinds of script run quite well. ... If you concatenate two plain text files, ... I think decent utf-8 capable programs SHOULD ignore extra BOM markers. ... You can have DOS executables run in dosboxes, ...
    (Linux-Kernel)

Loading