Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

We know that in Mathematica there is some default precision, which you can also change. This means, that after a certain decimal(fixed by the precision), the computer puts pseudo random numbers. I am looking for a function F that would put everything zero after a certain decimal. Let us say I want this to be the fourth decimal, then

F[0.12345678]=0.12340000

Does such function exist?

Thank you

share|improve this question
2  
Floor[0.12345678, 0.0001] gives 0.1234 and if you want 4 zeros to the right you can use NumberForm[Floor[0.12345678, 0.0001],{4,8}] –  tchronis Nov 3 '13 at 11:55
    
@tchronis thanks great, and can you think of something equivalent for matrices? You can use it for a matrix, but I have realized that after you use it with matrix, Mathematica does not consider it a matrix any more, for example you can do Eigensystem afterwards –  Mencia Nov 3 '13 at 12:01
1  
Thanks @Mencia , you can apply Floor to the matrix. I suppose you have rectangular Matrices. Floor[matrix,0.0001] works in my machine. And for nice output you can apply the NumberForm function. –  tchronis Nov 3 '13 at 12:05
    
@tchronis great man, that is exactly what I needed, appreciate!! –  Mencia Nov 3 '13 at 12:11

1 Answer 1

up vote 3 down vote accepted

Here are two approaches (trying to emulate the fomratting it just the truncation):

f[x_, n_] := N[IntegerPart[10^n x]/10^n, StringLength@ToString@x]

or

g[x_, n_] := N[Floor[10^n x]/10^n, StringLength@ToString@x]

Evaluating:

f[0.12345678,4]

g[0.12345678,4]

yield:

0.12340000

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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