2
$\begingroup$

I'm using Mathematica to process some grades which are saved via Export[]. However, if I have real versions of integers, the exported numbers don't have a trailing 0. This lack of a trailing 0 messes up the program I am importing the csv file into. Ugh.

   Export[ "test.csv", {1.0, 0.0}]

You'll see that the output is

   1.
   0.

Can anyone give me an easy fix for this? The data I'm exporting is a mixture of strings (some of which are already in double quotes) and numbers.

$\endgroup$
7
  • $\begingroup$ Export["test.csv", NumberForm[{{1., 2.}}, {3, 1}]] $\endgroup$
    – george2079
    Commented Apr 3, 2018 at 20:13
  • $\begingroup$ Removed the Bugs tag, since this behaviour is expected; it's how Mathematica outputs floating-point numbers which happen to be integer. $\endgroup$ Commented Apr 3, 2018 at 20:24
  • 1
    $\begingroup$ "bugs" might apply to the program that needs trailing zeros to read it !! $\endgroup$
    – george2079
    Commented Apr 3, 2018 at 20:41
  • $\begingroup$ I agree with @george2079 but I would also argue it also applies to mathematica. The numbers in the example above are not integers. $\endgroup$ Commented Apr 3, 2018 at 20:49
  • $\begingroup$ @george2079 this output is not correct. I don't want any brackets in my output. $\endgroup$ Commented Apr 3, 2018 at 21:02

1 Answer 1

2
$\begingroup$

forgot we need to do a string conversion as well:

 Export["test.csv", 
     Map[ToString[NumberForm[#, {20, 1}]] &, #, {-1}]] &@{{1., 2.}, {3, 4}}

 FilePrint["test.csv"]

enter image description here

note that this also rounds to a single decimal place. Additionally this breaks when mathematica wants to use scientific notation for large numbers.

$\endgroup$
2
  • 1
    $\begingroup$ Thanks for the solution, I think this is an area where Mathematica is really bothersome. One should be able to output numbers in a sane manner without having two write extensive code. $\endgroup$ Commented Apr 3, 2018 at 21:47
  • $\begingroup$ The ToString[] can be omitted: ExportString[Map[NumberForm[#, {20, 1}] &, {{1., 2.}, {3, 4}}, {-1}], "CSV"] $\endgroup$ Commented Sep 26, 2018 at 8:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.