function y = sigfig(x,n,do_round=1)
  %----------------------------------------------------------------------------
  % Jonathan R. Senning <senning@gordon.edu>
  % Gordon College
  % October 22, 2000
  % January 2015: Revised and renamed from rnd() 
  %
  % Usage: y = sigfig( x, n [, do_round] )
  %
  % Returns:
  %       y:    The value of x rounded or chopped to n significant figures
  %
  % Parameters:
  %       x:    The value to round or chop
  %       n:    The number of significant decimal places to keep.
  %  do_round:  non-zero for rounding (default), 0 for truncation
  %
  % Rounds or chops x to n significant 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
  end

  % 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.

  if ( do_round ~= 0 )
    y = s * round( x * 10^a ) * 10^(-a);
  else
    y = s * fix( x * 10^a ) * 10^(-a);      
  end

end
