Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - This Is Not The Problem You're Looking For

Reply
 
Old 04-09-2009   #1 (permalink)
Todd Walton


 
 

This Is Not The Problem You're Looking For

I want to print two numbers side by side, and I want to be able to
visually scan down the list and have them lined up. So my list of:

1 10
323 894
541 3001

becomes:

001 0010
323 0894
541 3001

And so… how to write VBScript to pad the number with leading zeroes…
I searched the Internet for a while. There’s lots of talk about ways
to *strip* leading zeroes. There’s a VBScript function (FormatNumber)
that will put one leading zero on if it’s a fraction. Then about
twenty minutes later I found a way of putting leading zeroes on if you
know your number is below a certain length:

Right(“0000” & myNumber, 4)

That creates a four to eight digit number and then chops off and
returns the last four. Giving it “45” would create “000045” and then
return the right four digits, “0045”. That’s good except I don’t know
if my numbers will never be more than four digits or if they might get
to five or six digits.

Suddenly it occurs to me that if I truly don’t know the max length of
my numbers then there’s really no way I could *ever* line them up.
What if my number were suddenly 23 digits long? Or 400? I had been
searching for a general solution, but I suddenly found that... well…
maybe I do know the max length. So I can use the Right() trick.

And then it hits me. Why couldn’t I just print my number and then a
tab?

And thus, I had been working on the wrong problem. Go figure.

-todd


My System SpecsSystem Spec
Old 04-09-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: This Is Not The Problem You're Looking For


"Todd Walton" <tdwalton@xxxxxx> wrote in message
news:09e5e6e0-8c7d-4e7e-926f-1920847155c4@xxxxxx
I want to print two numbers side by side, and I want to be able to
visually scan down the list and have them lined up. So my list of:

1 10
323 894
541 3001

becomes:

001 0010
323 0894
541 3001

And so… how to write VBScript to pad the number with leading zeroes…
I searched the Internet for a while. There’s lots of talk about ways
to *strip* leading zeroes. There’s a VBScript function (FormatNumber)
that will put one leading zero on if it’s a fraction. Then about
twenty minutes later I found a way of putting leading zeroes on if you
know your number is below a certain length:

Right(“0000” & myNumber, 4)

That creates a four to eight digit number and then chops off and
returns the last four. Giving it “45” would create “000045” and then
return the right four digits, “0045”. That’s good except I don’t know
if my numbers will never be more than four digits or if they might get
to five or six digits.

Suddenly it occurs to me that if I truly don’t know the max length of
my numbers then there’s really no way I could *ever* line them up.
What if my number were suddenly 23 digits long? Or 400? I had been
searching for a general solution, but I suddenly found that... well…
maybe I do know the max length. So I can use the Right() trick.

And then it hits me. Why couldn’t I just print my number and then a
tab?

And thus, I had been working on the wrong problem. Go figure.

-todd

=================

Tabs won't solve your problem either, for these reasons:
a) Not every output device recognises tabs. Try msgbox to see what I mean.
b) Many output devices equate a tab to 8 characters. What if your numbers
are longer?
By the way, a subject line such as "Formatting number colums" would have
been a lot more informative than "This is not the problem you're looking
for", unless you're the author of the puzzle column of a magazine. If you
are then you're probably posting in the wrong newsgroup.


My System SpecsSystem Spec
Old 04-09-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: This Is Not The Problem You're Looking For


"Todd Walton" <tdwalton@xxxxxx> wrote in message
news:09e5e6e0-8c7d-4e7e-926f-1920847155c4@xxxxxx
I want to print two numbers side by side, and I want to be able to
visually scan down the list and have them lined up. So my list of:

1 10
323 894
541 3001

becomes:

001 0010
323 0894
541 3001

And so… how to write VBScript to pad the number with leading zeroes…
I searched the Internet for a while. There’s lots of talk about ways
to *strip* leading zeroes. There’s a VBScript function (FormatNumber)
that will put one leading zero on if it’s a fraction. Then about
twenty minutes later I found a way of putting leading zeroes on if you
know your number is below a certain length:

Right(“0000” & myNumber, 4)

That creates a four to eight digit number and then chops off and
returns the last four. Giving it “45” would create “000045” and then
return the right four digits, “0045”. That’s good except I don’t know
if my numbers will never be more than four digits or if they might get
to five or six digits.

Suddenly it occurs to me that if I truly don’t know the max length of
my numbers then there’s really no way I could *ever* line them up.
What if my number were suddenly 23 digits long? Or 400? I had been
searching for a general solution, but I suddenly found that... well…
maybe I do know the max length. So I can use the Right() trick.

And then it hits me. Why couldn’t I just print my number and then a
tab?

And thus, I had been working on the wrong problem. Go figure.

-todd
----------
As you imply, there is no "perfect" solution. I never use vbTab because I
get results like:

23 456 52
233333333 456 52

One solution is a function that accepts a parameter indicating the expected
maximum length, and which outputs something to indicate overflow. For
example:
========
strValue1 = "3.45"
strValue2 = "20384"
strValue3 = "2304567"

Wscript.Echo FormatNum(strValue1, 3) & " " & FormatNum(strValue2, 4) & " " &
FormatNum(strValue3, 7)

Function FormatNum(ByVal strValue, ByVal lngMax)
' Check for overflow.
If (Len(CStr(strValue)) > lngMax) Then
FormatNum = String(lngMax, "*")
Exit Function
End If
' Format string with leading zeros up to indicated maximum length.
FormatNum = Right(String(lngMax, "0") & CStr(strValue), lngMax)
End Function
==========
You could also pass numeric values to the function and they would be
converted to strings.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
the problem with ndis.sys Blue Screen of Death problem solved? Vista General
Re: Windows Mail Attachement Problem and Adobe Player Problem with IE8 Vista mail
Multi-select problem in Windows Explorer BIG PROBLEM!!!!!! Vista General
Generic McAfee Problem Message in Vista Problem Reports Vista performance & maintenance
Vista Upgrade Problem - Windows Explorer Loop problem Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46