View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Round with significant digits

4.8 | 15 ratings Rate this file 30 Downloads (last 30 days) File Size: 3.05 KB File ID: #26212 Version: 1.6

Round with significant digits



28 Dec 2009 (Updated )

Rounds towards the nearest number with N significant digits.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

This little function rounds a number (or the elements of a vector ot matrix) towards the nearest number with N significant digits.
roundsd(0.012345,3) returns 0.0123
roundsd(12345,2) returns 12000
roundsd(12.345,4,'ceil') returns 12.35
This is a useful complement to Matlab's ROUND, ROUND10 and ROUNDN (Mapping toolbox), especially when dealing with data with a large variety of order of magnitudes.

I was about to upload this file when I found "sd_round.m" by Edward Zechmann (see aknowledgment), which has similar feature (and more). But, because my script is very different in coding (much much shorter), I decided to share it anyway.


Round To A Specified Number Of Significant Digits inspired this file.

This file inspired Radiomics and Rounding Functions Collection And Cheat Sheet.

MATLAB release MATLAB 6.5 (R13)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
15 Oct 2015 Nathaniel Hollingsworth

Thanks for the excellent little utility. I like the different rounding methods that are supported.

The error Colin Morris identified is in line 74:

if ~ischar(method) || ~ismember(opt,method)

The problem comes from the "ismember(opt, method)" requirement. From the R2011b
documentation, "ismember" returns a logical array the same size as the first argument.

Replacing the line with this:

if ~ischar(method) || ~ismember(method, opt)

fixes the problem.

Comment only
15 Oct 2015 Nathaniel Hollingsworth

01 Jul 2015 Nelson

Nelson (view profile)

How is this function different from the Matlab built-in funciton chop?

I noticed chop doesn't have a doc page, so it's impossible to find with docsearch.

Comment only
05 Apr 2015 Colin Morris

Running on 2011a. I believe if found an error between lines 73-76. When explicitly stating the method ( i.e. 'fix',or 'ceil'), I receive the runtime error :

??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> roundsd at 74
if ~ischar(method) || ~ismember(opt,method)

commenting the else statement between lines 73-76 solves the issue for me.


Comment only
02 May 2014 Chris Patterson

13 Nov 2013 Franck Dernoncourt

29 Aug 2013 Yuri K

Yuri K (view profile)

Great function. I should find it earlier.

Will, it's a precision problem of floating point arithmetic. Try:
ans =
This little number trigger the balance. I think it's hard to overcome.

13 May 2013 Will

Will (view profile)

Does not work in all cases. Try rounding 3.55 to 1 decimal place and you get 3.5 instead of 3.6

Comment only
06 Feb 2013 Kamran Syed

veru good

Comment only
15 Nov 2012 Tobias Lamour

a very useful extension to the usual round functions.

one small speed improvement can be achieved. Using find is very slow. one can use logical indexing just like this:
y(x==0) = 0;
it does the same job while much faster with lager arrays.

20 Sep 2012 Walter de Vries

Very nice simple and compact code.
Should be build into next Matlab versions.

30 Aug 2012 Milan

Milan (view profile)

13 Aug 2012 Nik F

Nik F (view profile)

23 Dec 2011 K E

K E (view profile)

15 Mar 2011 Brent Boehlert

Much appreciated -- thanks.

06 Jan 2011 K E

K E (view profile)

So useful that it should be built in to the Matlab round function, as it is in other languages

06 Oct 2010 Sunday

Sunday (view profile)

05 Oct 2010 Forrest Brett

25 Mar 2010 Oleg Komarov

Oleg Komarov (view profile)

I think your submission deserves a much better overall rating.

24 Mar 2010 François Beauducel

Thanks Oleg.
Yes, I will complete the "see also" function list in future updates.

1) you effectively need the "find" in Matlab versions < 7... and also single pipe for the OR test.
2) like you I have hesitated between "switch" and "feval". But the feval code is much shorter...


Comment only
24 Mar 2010 Oleg Komarov

Oleg Komarov (view profile)

I would mention in the see also line the other rounding functions you use.

I never used matlab < 7 so I don't know if this applies:
1) no need to use find: y(find(x==0)) = 0;
2) I would use switch case instead of feval.


Comment only
22 Mar 2010 François Beauducel

Thanks Daniel for your time. I submitted an updated version that corrects the zero-value behaviour, and I added some options for the rounding method.
About the mlint messages, I kept my original syntax which is voluntarily backward compatible (with Matlab versions < 7).

Comment only
17 Mar 2010 Daniel Armyr

Daniel Armyr (view profile)

I also noticed some mlint-messages that I fixed. The core of the code now looks like this:

if ~isnumeric(n) || numel(n) ~= 1 || n < 0 || mod(n,1) ~= 0
error('N argument must be a scalar positive integer.')

valuesZero = (x==0);
og = 10.^(floor(log10(abs(x)) - n + 1));
y = round(x./og).*og;
y(valuesZero) = 0;

Comment only
17 Mar 2010 Daniel Armyr

Daniel Armyr (view profile)

I just wrote one of these and was anout to upload it. But since you had allready done it, there is no point. This file would get a very high mark by me for several reasons:
1) Simple function, no bloated features
2) Error checking.
3) Well formated documentation.

Unfortunately, your script doesn't handle 0. That pulls down the mark significantly.

28 Dec 2009 1.1

works with negative numbers.

29 Dec 2009 1.2

works with negative numbers.

20 Mar 2010 1.3

- adds different methods for rounding;
- corrects a bug for 0 values (now produces 0 instead of NaN), thanks to Daniel Armyr's comment.

14 Nov 2014 1.5

solve the floating point arithmetic problem with roundsd(3.55,2) pointed out by Will and Yuri K.

03 Apr 2015 1.6

fix a problem with vectors with negative and positive exponent values (problem since the last update only).

Contact us