File Exchange

image thumbnail

Rounding Off Number(s) to Specified Significant Figures

version (1.3 KB) by Yuzo Toya
Round off number(s) to specified significant figures, using two simple/powerful MATLAB functions.


Updated 28 Apr 2009

View License

spa_sf Rounding Off Number(s) to Certain Significant Figures

N = spa_sf(X,SF) is the rounded off number(s) of X with the specified significant figures SF. X could be a scalar or a vector; however, SF must be a scalar.

Example 1)
X = 3.14159265359;
SF = 3;
N = spa_sf(X,SF)

> N = 3.1400

Example 2)
X = 3.14159265359; X=[X,X*2;X*3,X*4];
SF = 3;
N = spa_sf(X,SF)

> N =
3.1400 6.2800
9.4200 12.6000

See also: num2str, str2num

* I hope it would be of any help.

Cite As

Yuzo Toya (2020). Rounding Off Number(s) to Specified Significant Figures (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Tianbo Xie

with this program, if you try to round 3.0014 to 2 significant number(which is 3.001), it won't work.

Very nice, usable right out of the box (or, right from a quick google search). Thanks!

Kumar V

really helpful


How to ceil/floor round to the significant number?


Yuzo Toya

Neha, You probably did not place the program / code (spa_sf.m) in the MATLAB's search path. Try running the program after moving the file "spa_sf.m" to the MATLAB's search path or to the current directory. Alternatively, you can set the path to the directory where the file "spa_sf.m" is stored on your computer. (From the Menubar, File >> Set Path...)


It is not working. I am trying to round off a matrix which contains double values, it shows the error:
Undefined function or method 'spa_sf' for input arguments of type 'double'.

it is nice, but how to round-off complexed values ?

Yuzo Toya

Note: This function provides expected results when handing numerical values in double or less precision.

Yuzo Toya

The beauty rests in the simplicity of it all, but I am not claiming for its originality -- it's all there in the MATLAB tool package. I decided to share "this code" mainly because of its simplicity, and hope that you will find it useful in any way.

Jos (10584)

Yes, too simple and its functionality is already abundantly available here of the FEX. However, I should compliment you for the nice help section!

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux