Retain trailing zeros and decimal points on converting to string

chris klein

Registered User.
Local time
Today, 11:18
Joined
Dec 19, 2002
Messages
69
I'm using Access VBA to write numeric values (double) from the current record on a form to a text file.
Each line of the text file has to be formatted as a series of 16-character-long units: For example (using _ to indicate a blank)
"254.01__________23.0001_________45.045609_______"

I get the 16-character units using the following function, where PS is assigned the numeric value:

Public Function PadStr(ToLen, PS)

ExistLen = Len(PS)
AddLen = ToLen - ExistLen
If AddLen > 0 Then
For n = 1 To AddLen
PS = PS & " "
Next n
End If
PadStr = PS
End Function

An example is PadStr(16,Me![calcium])

This function works fine BUT, it strips trailing 0s, and it strips the decimal point if the number is an integer value. For example:
250.010 is returned as "250.01__________" (number followed by 10 blanks) and
250.0 is returned as "250_____________"

IS THERE SOME WAY to prevent this stripping, to get:
"250.010_________" and
"250.0___________" ??
 
Last edited:
Does Me![calcium] show it right, (have the zeros)? If yes then try:
Code:
PadStr(16,CStr(Me![calcium]))
and in the function header
Code:
Public Function PadStr(ToLen, PS as String)
 
this is probably quite tricky, as you have a variable number of trailing decimals,

with an (discrete) integer, vba correctly determines there are no trailing decimals. I think you would have to test for thre particular case of an integer (as opposed to a real number) and add the required number of decimals to the generated string.
 
Thanks for replies. I found that the solution suggested by JHB did not solve the issue as trailing zeros and decimal point were still lost. In contrast, gemma's suggestion led me in the right direction: I discovered the use of doing a logical test with "Int(PS) = PS" as in the following:
If Int(PS) = PS then
ExistLen = Len(PS)
PS = CStr(PS) & ".0"
ExistLen = ExistLen + 2
etc.
This ensures that an integer value converted to string is always output with ".0" , which is sufficient for my needs.
 
glad you got it working

now i see your solution, you could probably save a line or two

If Int(PS) = PS then PS = CStr(PS) & ".0"
ExistLen = len(ps)
 

Users who are viewing this thread

Back
Top Bottom