function y = rnd(x,n)

%----------------------------------------------------------------------------
% Jonathan R. Senning <senning@gordon.edu>
% Gordon College
% October 22, 2000
%
% Usage: y = rnd( x, n )
%
% Returns:
%	y:	The value of x rounded to n decimal places
%
% Parameters:
%       x:	The value to round
%	n:	The number of significant decimal places to keep.
%
% Rounds x to n decimal places
%----------------------------------------------------------------------------

% Since we can't take the log of a negative number, we need to
% strip any negative sign from x and reapply it at the end.  In
% the event that x is zero, we just want to return zero.

s = 1;
if ( x < 0 )
    x = -x;
    s = -1;
elseif ( x == 0 )
    y = x;
    return
endif

% Compute the proper power of 10 to multiply x by so that n decimal
% places are left to the left of the decimal point.

a = -( floor( log10( x ) ) - ( n - 1 ) );

% Now multiply x by 10^a, round to the nearest integer, and then
% multiply by 10^(-a).  Also, don't forget to reapply the sign of x.

y = s * round( x * 10^a ) * 10^(-a);
        
endfunction
