Round with significant digits

Rounds towards the nearest number with N significant digits.

ダウンロード: 6.6K

更新 2015/4/3


編集メモ: This file was selected as MATLAB Central Pick of the Week

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.


François Beauducel (2023). Round with significant digits (, MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R13
Windows macOS Linux

ヒントを得たファイル: Round to a Specified Number of Significant Digits

ヒントを与えたファイル: RADIOMICS

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
バージョン 公開済み リリース ノート

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

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

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

works with negative numbers.

works with negative numbers.