4.8 | 14 ratings Rate this file 76 Downloads (last 30 days) File Size: 3.02 KB File ID: #26212

Round with significant digits


François Beauducel


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.

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 (20)
02 May 2014 Chris Patterson

Chris Patterson

13 Nov 2013 Franck Dernoncourt

Franck Dernoncourt

29 Aug 2013 Yuri K

Yuri K

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


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

Kamran Syed

veru good

Comment only
15 Nov 2012 Tobias Lamour

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

Walter de Vries

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

30 Aug 2012 Milan


13 Aug 2012 Nik F

Nik F

23 Dec 2011 K E


15 Mar 2011 Brent Boehlert

Brent Boehlert

Much appreciated -- thanks.

06 Jan 2011 K E


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

06 Oct 2010 Sunday


05 Oct 2010 Forrest Brett

Forrest Brett

25 Mar 2010 Oleg Komarov

Oleg Komarov

I think your submission deserves a much better overall rating.

24 Mar 2010 François Beauducel

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

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

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

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

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

works with negative numbers.

29 Dec 2009

works with negative numbers.

20 Mar 2010

- 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

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

Contact us