简体   繁体   中英

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, eg, 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.

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

EDIT - sorry had order reversed...

How about the using FormatNumber

FormatNumber(value, 1)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM