marillion
marillion

Reputation: 11170

Excel VBA decimal numbers get converted to integers in text output

I have a spreadsheet, and I have written a macro which uses some of the values in the spreadsheet to output a text file. Everything works fine except one issue:

If I have a floating number like 15.0, it shows up in the output text file as integer 15, without the decimal point. I do not observe the same issue if the number after the decimal point is something other than zero, e.g., 15.1 gets written as 15.1; no problem there.

I wonder if there is some kind of a command or property, which would force all numbers are written to the output file as floating numbers?

This is the line I write the numbers to the text file. WeightList(i) and SpacingList(i) are string arrays, which are used for storing the numbers collected from the spreadsheet.

Print #1, Chr(9); "<truck axleW=" & Chr(34) & _
          WeightList(i) & Chr(34) & " axleS=" & _
          Chr(34) & SpacingList(i) & Chr(34) & "></truck>"

Thank you!

EDIT: This is the line where the numbers are converted to strings, I tried using the format command but this did not help:

WeightList(i) = Format("0.0", Truck(i).AxleWeights(i))

EDIT2: The code works now, after changing the order of the arguments in the format command. Please see the accepted answer for details.

Upvotes: 1

Views: 7897

Answers (2)

Richard Schneider
Richard Schneider

Reputation: 35477

How about the using FormatNumber

FormatNumber(value, 1)

See http://msdn.microsoft.com/en-us/library/xfta99yt(v=vs.80).aspx

Upvotes: 0

Floris
Floris

Reputation: 46375

You can use format(value,"0.0") to convert to a string with the desired sig figs.

EDIT - sorry had order reversed...

Upvotes: 4

Related Questions