Preserve zeros before decimal precision when converting Double to String

Using VB6, when I have double i = -0.1 , if I convert it to a string with strTemp = Str(i) , I lose the leading 0 to decimal and end with only -.1

How to keep leading value 0 before decimal point when value is <1

+4
source share
2 answers

Use the format function.

 strtemp = Format(i, "0.####") 

0 and # are placeholders. 0 will put a zero in this place if there is no other value, including leading and trailing zeros. # puts the value in this place, but has no leading or trailing zeros.

+6
source

Alternatively you can use FormatNumber . In the example below, the average (4 in this case) is the number of digits required after the decimal point. More about this feature HERE

 strTemp = FormatNumber (i,4,vbTrue) 

There are some differences between the output of both functions. Depending on your requirements, you can use one or the other. Play with each function to get an idea of ​​which function best suits your needs.

+1
source

All Articles