1

I have the comma separated data below in an HTML textarea element whose contents I store in a variable. Let that variable be called "output".

rowNum,column1,column2
1,1.450512e+005,1.982704e+00300
2,1.401080e+005,2.230725e+00300
3,1.351758e+005,2.643786e+00300
4,1.302602e+005,3.221424e+00300
5,1.253666e+005,3.962995e+00300
6,1.205006e+005,4.867671e+00300
7,1.156674e+005,5.934442e+00300
8,1.108727e+005,7.162115e+00300
9,1.061216e+005,8.549320e+00300
10,1.014195e+005,1.009451e+00400
11,9.677171e+004,1.179595e+00400
12,9.218335e+004,1.365175e+00400
13,8.765956e+004,1.565984e+00400
14,8.320540e+004,1.781796e+00400
15,7.882584e+004,2.012372e+00400
16,7.452578e+004,2.257453e+00400

I then do this:

data = d3.csvParse(output);

Based on this answer, I am using the Number() function to convert these scientific notation numbers to decimal numbers. While it works fine for the numbers in the first column, the second column does not get converted correctly.

For instance: Number(data[rowNum]["column1"]) ealuates correctly to 145051.2 for rowNum = 1.

However, Number(data[rowNum]["column2"]) evaluates to 1.982704e+300 for rowNum = 1.

And it's the same story for every subsequent row up to row 10.

Everything after row 10 evaluates to Infinity.

Number(data[rowNum]["column2"]) evaluates to Infinity for rowNum = 10.

Why is this happening, and how can I prevent it?

Community
  • 1
  • 1
mixedbag99
  • 529
  • 1
  • 4
  • 17
  • 1
    Please, copy the exact structure of your object/array/whatever. – Gerardo Furtado Apr 27 '17 at 11:47
  • try doing `console.log(data[1]["column1"])` but for all the results, and see if the data you are receiving isn't being parsed correctly for the ones having errors. – Webeng Apr 27 '17 at 11:47
  • @GerardoFurtado - updated. Please take a look now. – mixedbag99 Apr 27 '17 at 11:51
  • @Great-Raising that is still **not** an object. That is, at the best, a CSV. Is that a CSV? – Gerardo Furtado Apr 27 '17 at 11:51
  • @GerardoFurtado, you just have to type `Number(1.982704e+300)` to repro. – Kaiido Apr 27 '17 at 11:54
  • Well, that's way above `Number.MAX_SAFE_INTEGER`! – Gerardo Furtado Apr 27 '17 at 11:56
  • @GerardoFurtado Yes and the numbers returning `Infinity` are way above `Number.MAX_VALUE`. – Kaiido Apr 27 '17 at 11:58
  • 2
    I confess that I didn't pay attention to the actual numbers. Thanks to @Kaiido, what's happening here is quite simple: those numbers in the second column are **huge**! They are way beyond the maximum number that you can represent in JavaScript: *"The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript (25^3 - 1)"*. – Gerardo Furtado Apr 27 '17 at 11:58
  • @GerardoFurtado `Number.MAX_VALUE`: 1.79E+308! – altocumulus Apr 27 '17 at 11:58
  • @altocumulus OP wants to show the number in decimal notation, not scientific notation. How can you show `number.MAX_VALUE` in decimal notation? Using 300 digits? – Gerardo Furtado Apr 27 '17 at 12:00
  • If you look at the numbers in column1, they all end like this: +00n, where n is some number. However, the numbers in column2 end like this: +00n00. Is it because of the two trailing zeros? – mixedbag99 Apr 27 '17 at 12:00
  • @GerardoFurtado, your comment pointed me in the right direction. The way the numbers are currently, they are way above the Number.MAX_SAFE_INTEGER value. However, I do not want the converted number to be an integer - I want up to 2 decimal places. The numbers are smaller than Number.MAX_VALUE = 1.79E+308, as mentioned by altocumulus. When I removed the two trailing zeros, my problem got solved. So this was an issue with my data and nothing else. You can turn your comment into an answer, as it helped me solve this problem. – mixedbag99 Apr 27 '17 at 12:13

0 Answers0