Results 1 to 6 of 6
  1. #1
    Persist is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32

    Format decimal numbers with fixed width

    Hello



    How can I format a decimal number so that:
    . the width of the whole number is 10 characters
    . 2 digits always after the decimal point
    . for positive and negative numbers

    Format(34.5, “ ###0.00”) gives 34.50
    This varies the number of spaces to the left of decimal point

    Format(34.5, “ 0000.00”) gives 0034.50
    This gives a fixed width but not for negative numbers and it looks ugly

    I want to print tables and have the columns line up
    For example (I hope the numbers below do line up)
    Number1 Number2
    -12.30 102.70
    4.50 -300.50

  2. #2
    dsmacs is offline Advanced Beginner
    Windows XP Access 2010 (version 14.0)
    Join Date
    Oct 2009
    Location
    Perth Western Australia
    Posts
    52
    What about printing from a contionous form or report and right aligning the field and changed to fixed?

  3. #3
    Persist is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32
    I am printing to a text file

    Print #1, Format(Number1,"##0.00"), Format(Number2, "##0.00")
    so I would like some function to do suitable formatting for me.

  4. #4
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,043
    Hi,

    you could use the format$ function to have a text string as result and then use the space function to add the necessary number of spaces in front of the formatted number.
    for instance to get a string of 10 chars:
    Space$(10- len(Format$(Number1,"##0.00"))) & Format$(Number1,"##0.00")

  5. #5
    Persist is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32
    Thanks for your neat suggestion.
    Based on it, I wrote the following function.
    It may well have some flaws - but the results are easier to see now.

    Function MyFmt(aNum As Double, Width As Integer) As String
    ' Function to format a number with
    ' two digits after decimal pont
    ' a width of Width characters including the sign
    ' I think Width should be greater than 13 + 1 for a sign = 14
    Dim nSpaces As Integer
    Dim aString As String
    aString = Format$(aNum, "##,###,##0.00")
    nSpaces = Width - Len(aString)
    If nSpaces < 0 Then
    nSpaces = 0
    End If
    MyFmt = Space$(nSpaces) & aString
    End Function

    I am suprised that VB does not contain something like this or more powerful. The old language FORTRAN did it.

    Thanks again.

  6. #6
    Persist is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32
    I have improved this formatting function
    Can people see any way to make this more efficient

    Function MyFmt(aNum As Double, Width As Integer, nDig As Integer) As String
    ' Function to format a number controlling width with:
    ' aNum: the number to be presented as a string
    ' Width: the minimum width of the string including any sign
    ' nDig: the number of digits after the decimal point
    Dim nSpaces As Integer
    Dim aString As String
    Dim strFmt As String

    strFmt = "##,###,##0"
    If nDig > 0 Then
    strFmt = strFmt & "."
    For nSpaces = 1 To nDig
    strFmt = strFmt & "0"
    Next nSpaces
    End If ' nDig

    aString = Format$(aNum, strFmt)
    nSpaces = Width - Len(aString)
    If nSpaces < 0 Then
    nSpaces = 0
    End If
    MyFmt = Space$(nSpaces) & aString
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. The section width is greater than the page width?
    By Gary_Marshall in forum Reports
    Replies: 2
    Last Post: 11-13-2009, 01:54 PM
  2. Replies: 5
    Last Post: 09-16-2009, 01:56 AM
  3. Formatting Fields and Fixed Width Exporting
    By Panman01 in forum Access
    Replies: 1
    Last Post: 05-22-2009, 09:32 AM
  4. Non programer needs help in decimal numbers
    By Peter O in forum Access
    Replies: 1
    Last Post: 10-07-2008, 12:29 PM
  5. Use fixed height and width for the Access window
    By AndrewAfresh in forum Access
    Replies: 3
    Last Post: 07-05-2006, 09:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums