Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

roundto(input, precision, warningFlag)
function output = roundto(input, precision, warningFlag)
% ROUNDTO effectively rounds a number to a certain number of decimal
% places.  0 gives conventional rounding behaviour, and negative numbers of
% decimal places round off to significant figures.

% number of arguments error check
error(nargchk(1, 3, nargin))

% if the precision wasn't specified, define a default (0)
if nargin < 2
    % defines it as 0, i.e. conventional rounding behaviour
    precision = 0;
end

% defines the rounding factor
roundFactor = 10 ^ precision;

% applies it
output = round(input * roundFactor) / roundFactor;

% if the output is not the same as the input, then there was some rounding
% - if the warningFlag was used, then display a warning
if nargin >= 3 && warningFlag && input ~= output
    % displays a warning
    warning('roundto:rounded', 'Number was more precise than specified so has been rounded.')
end

Contact us