Solved

number formatting

Posted on 2000-04-09
13
172 Views
Last Modified: 2010-05-02
I wnat to print some text and a number in 2 columns with the number aligned right. How can I print the leading/trailing spaces (instead of zeros) while printing a number so that it's aligned right.

i want the output to be something like this...

xyz      $123.00
xxxxxx   $  5.00

OR..

aaa      $123.00
dasdasd    $5.00

0
Comment
Question by:naikv
13 Comments
 
LVL 6

Expert Comment

by:DrDelphi
Comment Utility
Function padstring(str1, str2 As String, length As Integer) As String
  Numspaces = length - (Len(str1) + Len(str2))
  padstring = str1 + Space$(Numspaces) + str2
End Function
Private Sub Command1_Click()
List1.AddItem (padstring("dave was here", "5.00", 40))
List1.AddItem (padstring("So was joe, really", "23.00", 40))
End Sub




Have fun!!
0
 
LVL 4

Expert Comment

by:wileecoy
Comment Utility
DrDelphi -

Sorry - doesn't work.

Becuase of the "size" of the characters.

For example - the "S" and the "s" are different sizes, and therefore, there is an offset in the listbox.

I have tested this with both TrueType and non TT fonts with no success.

At first I wrote the exact same code, but found that my results weren't what I expected.

0
 

Author Comment

by:naikv
Comment Utility
does'nt work due to different sizes of characters!
0
 

Expert Comment

by:BSmiley
Comment Utility
Can you change the font to be "Courier New" or "FixedSys" so that you can use fixed spacing?  Listboxes don't have a right-align feature, so without that I suspect you won't be able to do it too easily.

Can you use the RichTextBox control (Richtx32.ocx)?  This may help you, although I never have used it for this purpose.  Check out the RightMargin and SelAlignment properties.  This should work in the following cases:
  1) you don't have anything else on the line, and
  2) you always have the same number of digits to the right of the decimal. (All MS fonts have fixed-pitch digits; if you're using a non-MS font this may still be an issue for you.)

Hope this help,
Bob
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
This Function will text align data into a single string that can be used for Printing or a Listbox.

1. Open a New Standard.Exe Project.
2. Add a Listbox (List1) to Form1.
3. Add a Picturebox (Picture1) to Form1.
4. Copy/Paste the following into the Form1 code window.
5. Press F5 to Run.

<----- Code Begin ----->

Option Explicit

Private Sub Form_Load()

   Form1.Move _
      Screen.Width * 0.1, _
      Screen.Height * 0.1, _
      Screen.Width * 0.8, _
      Screen.Height * 0.8
   
   ' Set Working Picture Box To Hidden
   Picture1.Visible = False

   Dim varArray As Variant
   Dim strReturn As String
   
   ' Set Column Alignment L=Left R=Right C=Center
   varArray = Array("L", "R", "C")
   strReturn = x_FormatOutput("Align", varArray)
   
   ' Set Font Data
   varArray = Array( _
      List1.FontBold, _
      List1.FontItalic, _
      List1.FontName, _
      List1.FontSize, _
      List1.FontStrikethru, _
      List1.FontUnderline)
   strReturn = x_FormatOutput("Font", varArray)
   
   ' Set Column Widths
   varArray = Array(500, 500, 500)
   strReturn = x_FormatOutput("Width", varArray)
   
   ' Process Headings
   varArray = Array( _
      "Left", _
      "Right", _
      "Center")
   List1.AddItem x_FormatOutput("Print", varArray)
   
   ' Process Detail
   Dim intIndex As Integer
   For intIndex = 1 To 20
      varArray = Array( _
         intIndex * 2, _
         intIndex * 30, _
         intIndex * 400)
      List1.AddItem x_FormatOutput("Print", varArray)
   Next intIndex

End Sub
Private Sub Form_Resize()
   
   List1.Move _
      ScaleWidth * 0.05, _
      ScaleHeight * 0.05, _
      ScaleWidth * 0.9, _
      ScaleHeight * 0.9

End Sub

Private Function x_FormatOutput _
(ByVal strCommand As String, _
ByVal varData As Variant) _
As String
         
   Static varAlign As Variant
   Static varWidth As Variant
   
   If strCommand = "Align" _
   Then
      varAlign = varData
      Exit Function
   End If
   
   If strCommand = "Font" _
   Then
      Picture1.FontBold = varData(0)
      Picture1.FontItalic = varData(1)
      Picture1.FontName = varData(2)
      Picture1.FontSize = varData(3)
      Picture1.FontStrikethru = varData(4)
      Picture1.FontUnderline = varData(5)
      Exit Function
   End If
   
   If strCommand = "Width" _
   Then
      varWidth = varData
      Exit Function
   End If
     
   If UBound(varData) <> UBound(varAlign) _
   Or UBound(varData) <> UBound(varWidth) _
   Then
      MsgBox "x_FormatOutput: Data Does NOT Match Formatting"
      Exit Function
   End If
   
   Dim lngIndex As Integer
   Dim lngLeft As Long
   Dim lngRight As Long
   Dim lngWidth As Long
   Dim strWork() As String
   ReDim strWork(UBound(varData))
   For lngIndex = 0 To UBound(varData)
      lngWidth = Picture1.TextWidth(varData(lngIndex))
      Select Case varAlign(lngIndex)
         Case "C" ' Center
            lngLeft = ((varWidth(lngIndex) - lngWidth) / 2) _
               / Picture1.TextWidth(" ")
            lngRight = ((varWidth(lngIndex) - lngWidth) - lngLeft) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = Space(lngLeft) _
               & varData(lngIndex) _
               & Space(lngRight)
         Case "R" ' Right
            lngLeft = (varWidth(lngIndex) - lngWidth) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = Space(lngLeft) _
               & varData(lngIndex)
         Case Else ' Left
            lngRight = (varWidth(lngIndex) - lngWidth) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = varData(lngIndex) _
               & Space(lngRight)
      End Select
   Next lngIndex
   
   x_FormatOutput = Join(strWork, " ")
   
End Function

<----- Code End ----->
0
 

Expert Comment

by:AlessandroF
Comment Utility
I use this method

While RSMetodi.EOF <> True
   RSMetodi!codice &  Space(20 - Len(Str(RSMetodi!codice))) & RSMetodi!dollars
RSMetodi.MoveNext
Wend

I put a number of space caracter respect of the len of value the result is:

zzzzzz----$8,90
x-------$565,90

Space(20 - Len(Str(RSMetodi!codice)))

20 is a max len of your 'yz' or    
'xxxxxx' fileds
   
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

Expert Comment

by:mcrider
Comment Utility
Since you're talking about a listbox, try this...Add the following to a MODULE:

   Public Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
   Public Const LB_SETTABSTOPS = &H192


Then you can set up tabs in a listbox like this... This example sets up 3 columns:
 
   Dim Retcode As Long
   ReDim TabStop(1 To 3) As Long
   TabStop(1) = 100
   TabStop(2) = 200
   TabStop(3) = 300
   Retcode = SendMessageArray(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(1))
   List1.Refresh


Then you can add items to the listbox in columns like this:

   List1.AddItem "Hello" & vbTab & "World" & vbTab & "Goodbye"


Hope this helps!



Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
By the way, make sure the listbox is wide enough to see the 3 column in the example I gave...


Cheers!®©
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
mcrider:
Questioner wants a right justify in one of the columns.. as such.. a TextWidth and space padding have to be added somewhere.. <sigh>. You can grab something from my code if you like.. <smile>.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 10 total points
Comment Utility
So try this example:

Start a new project, and add a listbox and a command button to Form1, the paste the following into the "Declarations Section" of Form1:


Private Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_SETTABSTOPS = &H192
Function RightJustify(ByVal Source As String, Size As Long) As String
    Do While Me.TextWidth(Source) < Size
        Source = " " + Source
    Loop
    RightJustify = Source
End Function
Private Sub Command1_Click()
   Dim Retcode As Long
   ReDim TabStop(1 To 3) As Long
   TabStop(1) = 50
   TabStop(2) = 100
   TabStop(3) = 150
   Retcode = SendMessageArray(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(1))
   List1.Refresh

    List1.AddItem "Hello" & vbTab & RightJustify("100.00", 1440) & vbTab & "Goodbye"
    List1.AddItem "Hello" & vbTab & RightJustify("1.05", 1440) & vbTab & "Goodbye"
    List1.AddItem "Hello" & vbTab & RightJustify("15.78", 1440) & vbTab & "Goodbye"
End Sub



Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
Alot of work for just 10 points... ;-)


Cheers!®©
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
mcrider:
<laughing>.. Yeah, ain't it a b*tch.. <smile> and a <wink>
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
<VBG>!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now