There are a number of issues here.
Originally Posted by CarmineCortez
The first is the vectorisation, the above is a vector version of the scalar code:
where the function ceil(u) returns the smallest integer greater than u.
ex = D - ceil(log10(abs(x)));
y = round(x*(10^ex))*(10^(-ex));
^ denotes raising to a power
round(u) rounds u to the nearest integer.
Lets take x=1234.56789, and D=2
It reduces x to 2-significant digits
If you want to try the code there is a free Matlab clone here