Solved

put a double in a fixed width txt file

Posted on 2004-09-24
8
170 Views
Last Modified: 2010-05-02
I have to put 3 double in a fixed width txt file.  How much characters I must specify in the width of a double ?
0
Comment
Question by:weeb0
  • 2
  • 2
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12142703
Use the format function to display the double with the number of digits you want:

Private Sub Command1_Click()
    Dim myDouble As Double
    myDouble = 1 / 3
    Debug.Print myDouble
    Debug.Print Format(myDouble, "0000.0000")
End Sub
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12143007
A double is usually 16 characters long...
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12143091
...however, if  it is less than 1, you may need an extra character to store the 0 and if it is a negative number, another extra character to store the -.  

I would use 18 characters in a fixed width file, read it as a string and then use CDbl to format as a double...

Don't forget to error handle, when writing, to account for rational decimals, such as 0.25.  Convert to string (with CStr) and then add however many spaces to make 18 chars...
0
 
LVL 1

Author Comment

by:weeb0
ID: 12143575
is there an easy way to format the fixed width ???

I would like to have trailing spaces for a total of 30 characters in 1st field, the second one is a date... so, I know how to deal with it ...
the the 2 last are double with 2 decimals.

thank you
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 20 total points
ID: 12143972
Here is one way to pad your string with trailing spaces:

Private Sub Command1_Click()
    Dim dblValue As Double
    Dim strValue As String
   
    dblValue = 1 / 3
    strValue = Format(dblValue, "#,##0.00") ' or however you decide to format it...
    strValue = trailingPad(strValue, 30)
    Debug.Print strValue
End Sub

Private Function trailingPad(ByVal strInput As String, ByVal padLength As Integer)
    Dim i As Integer
    Dim spacesNeeded As Integer
    Dim strLength As Integer
    Dim spaces As String
   
    strLength = Len(strLength)
    If strLength > padLength Then
        trailingPad = Left(strInput, padLength)
    Else
        spacesNeeded = padLength - strLength
        spaces = Space(spacesNeeded)
        trailingPad = strInput & spaces
    End If
End Function
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change vba from autofit to 13.5 width? 4 29
MS Access - Capture pressed key onclick 4 42
MS Access Search and Replace Using VBA 6 85
Hide vba in gp 7 105
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 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