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 - How to write a formatted number left-padded right-aligned into a 6-char-wide-column?

Reply
 
Old 04-02-2009   #1 (permalink)
Ken Tucker


 
 

How to write a formatted number left-padded right-aligned into a 6-char-wide-column?

I would like to write a number from variable myvar into a file.
The number should be written in a formatted way in a 6-char width column/field.
The output should be right-aligned and
a) zero padded
b) blank padded

Example: say current value = 45 then the line in the file should look like for:

a)
000045

b)
45

How can I do this formatting in vbscript?

Ken


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


 
 

Re: How to write a formatted number left-padded right-aligned into a 6-char-wide-column?


"Ken Tucker" <kenny23@xxxxxx> wrote in message
news:49d4627e$0$32666$9b4e6d93@xxxxxx-online.net...
Quote:

>I would like to write a number from variable myvar into a file.
> The number should be written in a formatted way in a 6-char width
> column/field.
> The output should be right-aligned and
> a) zero padded
> b) blank padded
>
> Example: say current value = 45 then the line in the file should look like
> for:
>
> a)
> 000045
>
> b)
> 45
>
> How can I do this formatting in vbscript?
>
> Ken
>
How about this:
x = right("000000" & x, 6)
y = right(spaces(6) & y, 6)


My System SpecsSystem Spec
Old 04-02-2009   #3 (permalink)
gimme_this_gimme_that


 
 

Re: How to write a formatted number left-padded right-aligned into a6-char-wide-column?

Function FormatInteger(a,b)
c = cstr(a)
n = b - (Len(c) + 1)
For i = 0 to n
c = "0" & c
Next
FormatInteger = c
End Function


Wscript.echo FormatInteger(45,6)
My System SpecsSystem Spec
Old 04-02-2009   #4 (permalink)
ekkehard.horner


 
 

Re: How to write a formatted number left-padded right-aligned intoa 6-char-wide-column?

Ken Tucker schrieb:
Quote:

> I would like to write a number from variable myvar into a file.
> The number should be written in a formatted way in a 6-char width column/field.
> The output should be right-aligned and
> a) zero padded
> b) blank padded
[...]
Sample code:

Dim dicFuncs : Set dicFuncs = CreateObject( "Scripting.Dictionary" )
Set dicFuncs( "P" ) = GetRef( "FN_P" )
Set dicFuncs( "G" ) = GetRef( "FN_G" )
Set dicFuncs( "E" ) = GetRef( "FN_E" )
Set dicFuncs( "N" ) = GetRef( "FN_N" )
Dim aTests : aTests = Array( _
Array( 6, 45, 123456, 1234567 ) _
)
Dim aTest
For Each aTest In aTests
Dim nNum
For nNUm = 1 To UBound( aTest )
Wscript.Echo "Width:", aTest( 0 ), "Number:", aTest( nNum )
Dim sFunc
For Each sFunc In dicFuncs.Keys()
WScript.Echo " " & sFunc _
& ": >" _
& dicFuncs( sFunc )( aTest( nNum ), " ", aTest( 0 ) ) _
& "<"
Next
Next
Next

Function FN_P( nInt, sFillChar, nWidth )
FN_P = Right( String( nWidth, sFillChar ) & nInt, nWidth )
End Function

Function FN_G( nInt, sFillChar, nWidth )
Dim sInt : sInt = CStr( nInt )
Dim nAdd : nAdd = nWidth - (Len( sInt ) + 1)
Dim nIdx
For nIdx = 0 To nAdd
sInt = sFillChar & sInt
Next
FN_G = sInt
End Function

Function FN_E( nInt, sFillChar, nWidth )
Dim sInt : sInt = CStr( nInt )
Dim nAdd : nAdd = nWidth - Len( sInt )
If 0 < nAdd Then sInt = String( nAdd, sFillChar ) & sInt
FN_E = sInt
End Function

Function FN_N( nInt, sFillChar, nWidth )
Dim sFmt : sFmt = "{0," & nWidth & "}"
FN_N = DNIPrintF( sFmt, Array( nInt ) )
End Function


' ----------------------------------------------------------------------------
'' DNIPrintF( sFmt, aItems ) - use .NET to sprintf()
' ----------------------------------------------------------------------------
Dim goSB : Set goSB = Nothing
Function DNIPrintF2( sFmt, aItems )
If goSB Is Nothing Then
Set goSB = CreateObject( "System.Text.StringBuilder" )
Else
goSB.Length = 0
End If
goSB.AppendFormat_4 sFmt, (aItems)
DNIPrintF2 = goSB.ToString()
End Function


and output:

=== frmNum: format number ==========
Width: 6 Number: 45
P: > 45<
G: > 45<
E: > 45<
N: > 45<
Width: 6 Number: 123456
P: >123456<
G: >123456<
E: >123456<
N: >123456<
Width: 6 Number: 1234567
P: >234567< ############ here are dragons! #########
G: >1234567<
E: >1234567<
N: >1234567<
=== frmNum: 0 done (00:00:01) ======

to show that

P's code is dangerous

G's code can be made more efficient (no string concatenation)

there is lot of potential in the .Net components that can be
used from VBScript (But I don't see a way to specify a fill
character)
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Contacts in left column? Vista mail
Re: how to count number of certain char within string PowerShell
restore left column Vista mail
Is there a way to get Format-Wide to sort by column? PowerShell
RuntimeType format uses excessively wide name column PowerShell


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