Message Boards Message Boards

0
|
8251 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Trimming trailing zeros from a number?

Posted 8 years ago

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?

POSTED BY: Erdem Uguz
5 Replies
Posted 8 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 BY: Hans Milton

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 BY: Jason Biggs
Posted 8 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 BY: Erdem Uguz
Posted 8 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.

POSTED BY: Erdem Uguz

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

POSTED BY: John Vande Vate
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