Re: Number Formatting in VBSCRIPT
- From: Dr John Stockton <jrs@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 24 Nov 2005 14:20:17 +0000
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.
.
- References:
- Number Formatting in VBSCRIPT
- From: simon . croome
- Re: Number Formatting in VBSCRIPT
- From: LakeGator
- Re: Number Formatting in VBSCRIPT
- From: Dr John Stockton
- Re: Number Formatting in VBSCRIPT
- From: LakeGator
- Number Formatting in VBSCRIPT
- Prev by Date: Getting Checkpoint VPN-1 status?
- Next by Date: WSH library reference
- Previous by thread: Amen...
- Next by thread: Re: Number Formatting in VBSCRIPT
- Index(es):
Relevant Pages
|
Loading