Kyle V.
Kyle V.

Reputation: 4792

Preserve Zeroes Before Decimal When Converting Double to String

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

How do I keep the leading 0 before the decimal point when the value is < 1?

Upvotes: 4

Views: 1647

Answers (2)

George
George

Reputation: 2213

As an alternative, you can use FormatNumber. In the example below, the middle number (in this case 4) is the number of digits desired after the decimal point. More information on this function 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 around with each function to get the idea which function fits your requirements best.

Upvotes: 1

Tom Collins
Tom Collins

Reputation: 4069

Use the format function.

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

0 & # are placeholders. 0 will put a zero in that spot if no other value exists, including leading & trailing zeros. # puts the value into that spot, but no leading or trailing zeros.

Upvotes: 6

Related Questions