tags:

views:

115

answers:

4

Hello Exeperts,

I am creating a csv file from my vb.net application as shown below

Dim csvFile As String = My.Application.Info.DirectoryPath & "\Test.csv"
        Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, False)

        outFile.WriteLine("Column 1, Column 2, Column 3")
        outFile.WriteLine("1.23, 4.56, 123456545666644565554")
        outFile.WriteLine("3.21, 6.54, 9.87")

But the value in the csv file appears like 1.25778E+12 for 123456545666644565554

How do I format it in vb.net. please help

+1  A: 

Are you sure that in the raw text output the value 123456545666644565554 is output as 1.25778E+12? Are you perhaps seeing this in Microsoft Excel or some other application where you imported this data? Because the value 123456545666644565554 is enclosed in the string "1.23, 4.56, 123456545666644565554" I find it highly suspect that any formatting is being applied in the raw CSV file.

Jason
A: 
    outFile.WriteLine("Column 1, Column 2, Column 3")
    outFile.WriteLine("1.23, 4.56, '123456545666644565554")
    outFile.WriteLine("3.21, 6.54, 9.87")
BenB
....that doesn't work at all.
Meta-Knight
I'm sorry acadia, but adding a quote in front of your numbers is not a good solution. If you ever import this number in some other other system you'll get invalid values...
Meta-Knight
I'm guessing you've have never had to wrestle with excel exports then? :)
BenB
acadia, I have to agree with Meta-Knight here - this is only useful for excel viewing. As others have pointed out, the dump is correct, but excel tries to format the cell for you. So, if you need it in other programs, dump 2 files!
BenB
+3  A: 

Open your CSV file in Notepad instead of Excel, and I'm sure your number will appear correctly. The problem is not your CSV (CSV is just text anyway, it doesn't do any formatting), it's just that when you open it in Excel, it will display it in scientific format because the number if too long. If you change the Excel column formatting options to display as text format instead of number format, your number will appear correctly.

Meta-Knight
I am 100% sure. The CSV file that is generated from the above code is displaying the numbers as I showed above
acadia
Generate the .csv and then *before you do anything else* open it with a text editor - what's the value at this point. The lines above **cannot** write 1.25778E+12 to a file so something else is happening along the way.
Murph
Good point by Murph, open it in Notepad just after generating the file, you'll see that your number is correct.
Meta-Knight
A: 

Store the values in a variable of the proper type and then convert it and write it to the file.

Dim d As Double = 1.234
Dim number As String = CStr(d)
ChadNC