Trimming trailing zeros from a number?

Posted 5 years ago
4405 Views
|
5 Replies
|
1 Total Likes
|
 Hi,I am creating many lists during a calculation and finally report some of them. Couple of them have insignificant trailing zeros such as: a=1.23000000000000000000000 and I would like to trim those zeros. I was thinking something like N[a,Accuracy[a]] but it is not working.Is there a way of doing this?
5 Replies
Sort By:
Posted 5 years ago
 Are you writing the output to a file, in a data table format, or is it staying in the notebook?When outputting to a text file where I want a particular format (so that my fortran or python function can import it), there are various tricks to use (NumberForm, ToString, FortranForm, etc). The exact method depends on how you want the output formatted.Give this thread a read, http://mathematica.stackexchange.com/q/19387/9490 , and you might find what you need there.
Posted 5 years ago
 Thank you for you reply I am exporting the data as .csv , I found that is the best way of keeping all the digits. FortranForm does what I need but with one problem. Here is an example what I am doing.  A = {1.000000000000000000, 2.000000000000000000}; (*this is the position*) B = {1.456783454656574576, 4.546562323253456534}; (*value at that position*) C=Transpose[{A, B}] (*and I write C to .csv file*) And when I use FortranForm on A it is not anymore a list therefore I can't create C.
Posted 5 years ago
 Maybe you could use rounding and $MachineEpsilon to get rid of the trailing zeros: a = {1.000000000000000000, 2.000000000000000000}; (*this is the position*) b = {1.456783454656574576, 4.546562323253456534}; (*value at that position*) c = Round[#,$MachineEpsilon] & /@ Transpose[{a, b}]  {{1., 1.45678}, {2., 4.54656}} FortranForm@c  List(List(1.,1.4567834546565746),List(2.,4.546562323253457)) \$MachineEpsilon  2.22045*10^-16
 If you just want to trim padded 0's use  InputForm[a, NumberMarks->False]  That will yield 1.23 without changing a.