• Status: Solved
• Priority: Medium
• Security: Public
• Views: 178

# put a double in a fixed width txt file

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
weeb0
• 2
• 2
1 Solution

Middle School Assistant TeacherCommented:
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

Commented:
A double is usually 16 characters long...
0

Commented:
...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

Author Commented:
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

Middle School Assistant TeacherCommented:

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...
Debug.Print strValue
End Sub

Dim i As Integer
Dim spacesNeeded As Integer
Dim strLength As Integer
Dim spaces As String

strLength = Len(strLength)
Else
spaces = Space(spacesNeeded)
End If
End Function
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.