Click here to Skip to main content
14,698,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 fix this? 
Note: it's get Ugly scientific notation format when digit number count greater then 12 number digit.

Many thanks.


What I have tried:

I have tried to convert number in string and generate csv but still same issue.
Posted
Updated 29-Oct-20 1:10am
v2
Comments
Tomas Takac 22-Mar-18 3:35am
   
How are you writing the CSV file? Please update your question with the code.
Maciej Los 22-Mar-18 4:20am
   
What kind of application do you use to open csv or where do you display the data?
If you're using MS Excel - yeah, you'll see scientic format, but the numbers are correct. Open csv file in Notepad and you'll see proper number format.

I strongly believe that Uroš Šmon[^] is right as to this statement: "Csv is very simple text format, that knows nothing about its content". You did not provide a way you save numbers into csv file, so we can't help you to resolve your issue, because we have no access to your screen or data. I tested below code and it works perfect, even if Excel displays the numbers in scientic format.

decimal[] numbers = {43564765876978, 56576867897699, 45765879789789};
string path = @"D:\text.csv";

string[] lines = numbers.Select(x=>x.ToString()).ToArray();

File.WriteAllLines(path, lines);

lines = File.ReadAllLines(path);
//result:
//43564765876978 
//56576867897699 
//45765879789789 
   
Comments
Niranjankr 23-Mar-18 1:36am
   
Please open your text.csv file. issue exists.

result is ::::

4.35648E+13
5.65769E+13
4.57659E+13
Maciej Los 23-Mar-18 2:45am
   
No, there's no isssue, when you'll use method my method. I have no idea what method you use. As i mentioned, when you open this file using Excel, you'll always see scientic format.
Niranjankr 23-Mar-18 5:32am
   
"you'll always see scientic format."

If we do not want to open in scientific format then how it is possible.
CPallini 23-Mar-18 4:10am
   
My 5.
Maciej Los 23-Mar-18 12:18pm
   
Thank you, Carlo!
Quote:
But these keep getting replaced with values such as 4.575769E+12

This not a csv related problem, if you output to screen, you will see the exact same values.
It is the program that write to csv that is guilty.
It can be the formatting when you output the numbers or the datatype that store the data.
But as long as we don't see your code, it is difficult to tell.
   
Comments
CHill60 22-Mar-18 9:12am
   
It's more likely that the OP is using Excel to view the file! :-)
Patrice T 22-Mar-18 9:31am
   
Possible, but I see nothing about Excel.
CHill60 22-Mar-18 13:08pm
   
It's something I see here daily - the users complain that the numbers "have gone weird" because they insist on opening CSV files with Excel - which automatically changes the numerics to scientific notation
Problem is not on csv side. Csv is very simple text format, that knows nothing about its content. It looks like you are writing floating point numbers into it, using default format. Use format with zero decimal places on write, or convert numbers to integer
   
Comments
Niranjankr 22-Mar-18 2:59am
   
I am sending string value. it is big digit(17). "15704984052700001"

that is append on stringbuilder.
Uroš Šmon 22-Mar-18 3:20am
   
csv has no type on column, they are all strings. Check how csvhelper is working with columns and column types. I have never used it, because csv is so simple
Maciej Los 22-Mar-18 4:36am
   
5ed!
CPallini 23-Mar-18 4:10am
   
5.
if we 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.


Note :: it is not a proper solution but we can use to show it and while read data from csv need to ignore it.
   
Comments
Maciej Los 23-Mar-18 3:08am
   
Note that we're talking about csv file instead of Excel... So, your solution is wrong.
By The Way: adding a ['] at the front of number, changes number to text. Are you sure that OP wants to store text instead of numbers?
Niranjankr 26-Mar-18 3:35am
   
Note :: it is not a proper solution but we can use to show it and while read data from csv need to ignore it.
Niranjankr 23-Mar-18 3:44am
   
Yes sure.
Issue must be of data type.
You must be converting data type double to string, which converts number into exponential form.
e.g.
double val = 43564765876978;
string s = val.ToString();

instead of above use ulong as following and every thing is ok.
ulong val = 43564765876978;
string s = val.ToString();
   
Comments
Maciej Los 21-Aug-19 7:33am
   
I'd suggest to read a question again. Your answer is not related to the question. BTW: Converting numbers to string and vice versa is very bad idea.
You can simply add a "\t" at the end of the number. This will add a space after and stop converting it into Scientific Notation.

Eg:
let number = 123456789 + "\t";
   
Comments
CHill60 29-Oct-20 7:19am
   
What is let?
The number is not being converted to anything, the OP was clearly opening the CSV with Excel and the display of the value was in scientific notation.
CSV contents are all strings.
And this solution does not work anyway - try saving 12345678934567 , 12345678934567, to a .csv file and opening it in Excel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900