Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csv.stringify StringifyOptions to output numbers in fixed point notation without trailing zeros #6080

Open
dandv opened this issue Sep 30, 2024 · 1 comment
Labels
csv suggestion a suggestion yet to be agreed

Comments

@dandv
Copy link

dandv commented Sep 30, 2024

When outputting CSV fields that are small numbers, the output can end up in scientific notation. This can confuse spreadsheet software.

image

Describe the solution you'd like

A StringifyOption to disable scientific notation.

Describe alternatives you've considered

Using Big.js.

@kt3k kt3k added suggestion a suggestion yet to be agreed csv labels Oct 9, 2024
@lionel-rowe
Copy link
Contributor

A few things to bear in mind:

  • Number.MIN_VALUE is 5e-324
  • Number.MAX_VALUE is 1.7976931348623157e+308
  • In Excel, entering the string 5e-324 into a cell gives a string; 1e-307 gives the number value of 1e-307; 1e-308 gives 0 (0.00E+00); 0.000000001 (1e-9) remains displayed as a decimal point, whereas 0.0000000001 (1e-10) starts displaying as scientific notation.
  • In Excel, entering the string 1e308 into a cell gives a string; 1e307 gives the number value of 1e307; 10000000000 (1e10) remains displayed as a decimal point, whereas 100000000000 (1e11) starts displaying as scientific notation.
  • File size would increase significantly for certain types of datasets if e.g. 1e100 was getting written out as 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 each time.
  • Saving from Excel to a CSV prints cells with scientific notation as n.nnE[+-]n format. Re-opening them in Excel gives a dialog box that gives the option to treat them as numbers. Excel also seems to handle numbers formatted like 1.2e-8 the same way (YMMV with other spreadsheet software).
  • JS and (I think?) most other programming languages handle the formatting variations between 1e5, 1E5, 1.0E+05 etc. perfectly fine, so if using scientific notation at all, it might be worth preferring whichever version of scientific notation is most widely compatible with spreadsheet programs (if there's a difference).
  • For pure plaintext readability, scientific notation is obviously preferable for very small and very large numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
csv suggestion a suggestion yet to be agreed
Projects
None yet
Development

No branches or pull requests

3 participants