How to align string...

Hi.

I need to align string on the receipt.

Example:

Name           Quantity                    Price            Sum
-----------------------------------------------------------

xxxxxxxxx     1.00                       1.00            1.00
yyyyyyyyy     10.00                       10.00            100.00
zzzzzzzzz     1.00                       1.00            1.00
------------------------------------------------------------

how to align yyyyyy so that end result look like:



Name           Quantity                    Price            Sum
-----------------------------------------------------------

xxxxxxxxx     1.00                       1.00            1.00
yyyyyyyyy   10.00                     10.00       100.00
zzzzzzzzz     1.00                       1.00            1.00
------------------------------------------------------------

Currently i am playing with padleft and padright, but i just can get it.

Name string is always up to 20 characters,  others can vary but they will rarely be over 8 characters each, except maybe total which can get up to 10 characters.


Can i set fixed positions, let's say Name can be from 0 to 20, Quantity from 24-32..etc and inside those parameters make them align left or right so that numbers are nicely aligned?
GrogelAsked:
Who is Participating?
 
CodeCruiserCommented:
0
 
rushtoshankarSr Manager Software DevelopmentCommented:
for column one
For i = 0 to columwidth
    ' if the data to be aligned left
     if left align and i = 0 then
         string = data
         i = length of data + 1
     ' if the data to be aligned on right
     elseif right align and i = columnwidth - datalen
         string = string + data
         i = columnwidth
    ' for both case apped the
    else
         string = string + " "
    end if
next i
do this for rest of the columns
Now you have the first row ready in the string
now loop it for each row of data

column widht can be adjusted at any time with column witdh variable

Hope this logic helps you
0
 
x77Commented:
Use String.Format:

string.Format ("{0,-10} {1,8:n2} {2,8:n2}","xx",1.1,1.15)
"xx             1,10     1,15"
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.