0

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

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I am 100% sure. The CSV file that is generated from the above code is displaying the numbers as I showed above
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.
Good point by Murph, open it in Notepad just after generating the file, you'll see that your number is correct.
1

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.

Comments

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

3 Comments

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...
I'm guessing you've have never had to wrestle with excel exports then? :)
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!
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.