Message Boards Message Boards

GROUPS:

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

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

Posted 5 years ago

Thank you for the reply That would work is I did not want to keep all the digits if they are meaningful. a and b might have more than 16 digits I want to keep. It is not end of the world for me. I just want to keep the output file clean.

If you just want to trim padded 0's use InputForm[a, NumberMarks->False] That will yield 1.23 without changing a.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract