1
$\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$
  • $\begingroup$ Export["test.csv", NumberForm[{{1., 2.}}, {3, 1}]] $\endgroup$ – george2079 Apr 3 '18 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$ – Patrick Stevens Apr 3 '18 at 20:24
  • 1
    $\begingroup$ "bugs" might apply to the program that needs trailing zeros to read it !! $\endgroup$ – george2079 Apr 3 '18 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$ – mikemtnbikes Apr 3 '18 at 20:49
  • $\begingroup$ @george2079 this output is not correct. I don't want any brackets in my output. $\endgroup$ – mikemtnbikes Apr 3 '18 at 21:02
1
$\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$
  • 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$ – mikemtnbikes Apr 3 '18 at 21:47
  • $\begingroup$ The ToString[] can be omitted: ExportString[Map[NumberForm[#, {20, 1}] &, {{1., 2.}, {3, 4}}, {-1}], "CSV"] $\endgroup$ – J. M. will be back soon Sep 26 '18 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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