Csvhelper: Writing long numbers to csv?

Created on 19 Jul 2015  路  13Comments  路  Source: JoshClose/CsvHelper

Hey,

So I'm trying to write long numbers to a csv eg.

43564765876978
56576867897699
45765879789789

But these keep getting replaced with values such as 4.575769E+12, I'm assuming its because the csv is making the column a number column and its too big? How can I use csvhelper to fix this? Many thanks.

Most helpful comment

you can also place the \t at the end or the beginning of your value.

All 13 comments

Can you give me an example code to reproduce this?

Doing this:

var num = 43564765876978;
var converter = new DoubleConverter();
converter.ConvertToString( new TypeConverterOptions(), num ).Dump();

Gives me this: 43564765876978

My application requires the user to type in the id of a user and these are stored as items in a listbox, the numbers I've just used and the output are:

65786876975476876 = 6.57869E+16
6576789790789789678 = 6.57679E+18
6576575675675686 = 6.57658E+15

The code for writing the csv is:

using (var writer = new StreamWriter(path + filename))
{
    var csv = new CsvWriter(writer);
    csv.Configuration.Encoding = Encoding.UTF8;

    foreach (var value in lstCsvUsers.Items)
    {
        csv.WriteField(value.ToString());
        csv.NextRecord();
    }
    writer.Close();
}

WriteField is going to write whatever string you give to it.

Is Items a list of double? How are you converting the string entered by the user into a double?

Items is a ItemCollection,
I'm not converting it I'm just adding it to the listbox as seen in the code below

private async void btnCsvAddUser_Click(object sender, RoutedEventArgs e)
{
    if (tiCsvUserID.IsSelected)
    {
        if (!Methods.IsValidUser(txtCsvUserId.Text)) // Check to see if this user exists
            return;
    }

    lstCsvUsers.Items.Add(txtCsvUserId.Text);
}

Basically I'm just dealing with a string of numbers.

Gif of input and output: http://gyazo.com/f1aecc5bae80dcfa067bc907fd7cfaf4

Can you open the written CSV file with a text editor and see what the values are there? From the animated gif you have, it looks like the exponential notation is only shown in the Excel cells, and the formula is showing the correct number. What does the raw data in the file look like?

Opening it in notepad the data looks fine, took a look more at excel and the cells need to be formatted as "Text" to prevent the notation. Is this possible?

Not through CSV it's not. CSV is just comma separated values that Excel happens to be able to open. If you start making formatting changes, when can't save as CSV anymore, and it will want to save as XLS instead. If you choose to save as CSV, all the formatting you put in place will be stripped out.

Hmm okay, thank you for your help.
I'll just ignore these values in excel as the originals look to be fine, excel is just being a typical microsoft product.

Are you trying to create an Excel spreadsheet, or a CSV file? There's a big difference there.

There is no other spreadsheet software out there that even gets close to Excel. If by

excel is just being a typical microsoft product

you mean "awesome", then I'd agree. :) I've been very please with everything MS has been putting out for the past 5+ years or so.

Actually, if you place a ' at the front of the number in the csv cell, excel will format it as text. But then you will have to parse out the ' if you plan to use it as a number. This is specific to Excel and may or may not work in other spreadsheet applications.

+1 to MS Office being awesome!

you can also place the \t at the end or the beginning of your value.

you can also place the \t at the end or the beginning of your value.

Thank you, this is exactly what I required.

you can also place the \t at the end or the beginning of your value.
The number:
"43564765876978"
becomes
"43564765876978 "

Please note the space above.

If the csv is required to be processed by computer, will it require extra trim at the end?

If so, is there any solution instead of "\t"?

Was this page helpful?
0 / 5 - 0 ratings