Cardinal
Cardinal

Reputation: 105

Arbitrary accuracy of the fractional numbers in Matlab

I need a function of Matlab to do below operation (I don't know how to explain it in English)

suppose:

a = - 1.200000005

I need a function which does:

f(a) = -1.2; or f(a) = -1.2000

I mean, I want to set the arbitrary accuracy.

Upvotes: 0

Views: 60

Answers (1)

Adriaan
Adriaan

Reputation: 18177

function [output]=ArbAcc(a,digits)
    output = round(a*power(10,digits))/power(10,digits);
end

This creates a function ArbAcc which multiplies your data, rounds it and finally shifts it back to get your desired output.

Or, as @HamtaroWarrior said, use roundn (note that the mapping toolbox is required for roundn):

a = roundn(a,-digits);

If I had continued reading the documentation I would have seen that roundn is no longer recommended, use round:

Y = round(X,N);% rounds to N digits

Upvotes: 1

Related Questions