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

Tech-Archive recommends: Speed Up your PC by fixing your registry




"Todd Walton" <tdwalton@xxxxxxxxx> wrote in message
news:09e5e6e0-8c7d-4e7e-926f-1920847155c4@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
--


.



Relevant Pages

  • Re: First decade of the 20th century
    ... Mike Barnes: ... does prompt me to ask about leading zeroes in dates. ... than typing a couple of digits. ... Using 5/2 for the second of May was common before computers took ...
    (alt.usage.english)
  • Re: Enigma 1459 - Twelve elevenths
    ... refers to "digits", plural. ... IIRC the original post included a ban on leading zeroes. ...
    (rec.puzzles)
  • RE: Need to have a field always have 13 characters & insert leadin
    ... For direct entry or imports, I do not believe you can do the automatic ... padding of zeroes. ... pads those fields that do not have thirteen digits. ... always see of thirteen characters, why should they care about what is ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Power anagrams
    ... I did a computer search of numbers of up to 12 digits and found 26 ... The two smallest cases, however, involve no zeroes. ... example with an exponent of interest: ... Extending the search up to 18 digits yields a large number of other ...
    (rec.puzzles)
  • Extra digits in merge from Excel (Office 2003)
    ... other users have reported with extra digits after the decimal point. ... I will use two numbers in the Excel file as examples throughout. ... significant digits and they are all zeroes following the "74".) ... Use the DDE conversion ...
    (microsoft.public.word.mailmerge.fields)