| Description |
### Why these Functions? ###
All the rounding functions you will ever need: collected together, illustrated and embedded in one PDF!
This collection of functions provides six rounding methods, and two wrappers that round to significant figures or decimal places. The rounding functions are simple and fast, and can be used independently. The wrapper functions "round2sf" and "round2dp" can be used with any MATLAB function that rounds numeric values to integer values (the default is MATLAB's "round").
This collection is also illustrated in a PDF together with the standard MATLAB rounding functions. The M-files from this collection are embedded in the PDF, so you only need to keep the PDF file handy...
### The Function M-files ###
Asymmetric Rounding:
- round2dn (round half down)
- round2up (round half up)
Unbiased Rounding:
- round2ev (round half to even)
- round2od (round half to odd)
- round2ze (round half toward zero)
- round2ra (round half randomly/stochastically)
Significant Figures Wrapper:
- round2sf (input a function handle to choose the rounding method)
Decimal Places Wrapper:
- round2dp (input a function handle to choose the rounding method)
All of these functions are illustrated and embedded in one PDF:
- round2xx.pdf
### Rounding Examples ###
round2dn(-3.5:3.5)
ans = [-4,-3,-2,-1,0,1,2,3]
round2up(-3.5:3.5)
ans = [-3,-2,-1,0,1,2,3,4]
round2ev(-3.5:3.5)
ans = [-4,-2,-2,0,0,2,2,4]
round2od(-3.5:3.5)
ans = [-3,-3,-1,-1,1,1,3,3]
round2ze(-3.5:3.5)
ans = [-3,-2,-1,0,0,1,2,3]
round2ra(-3.5:3.5)
ans = [-4,-2,-2,-1,1,2,3,3]
### Significant Figures Examples ###
round2sf([1111,222.2,33.33,4.444,0.5555],1)
ans = [1000,200,30,4,0.6]
round2sf([1111,222.2,33.33,4.444,0.5555],3)
ans = [1110,222,33.3,4.44,0.556]
round2sf([1111,222.2,33.33,4.444,0.5555],3,@ceil)
ans = [1120,223,33.4,4.45,0.556]
round2sf([6.6666,6.6666;6.6666,6.6666],[1,2;3,4])
ans = [7,6.7;6.67,6.667]
### Decimal Places Examples ###
round2dp([1111,222.2,33.33,4.444,0.5555],1)
ans = [1111,222.2,33.3,4.4,0.6]
round2dp([1111,222.2,33.33,4.444,0.5555],2)
ans = [1111,222.2,33.33,4.44,0.56]
round2dp([1111,222.2,33.33,4.444,0.5555],2,@ceil)
ans = [1111,222.2,33.33,4.45,0.56]
round2dp([6.6666,6.6666;6.6666,6.6666],[1,2;3,4])
ans = [6.7,6.67;6.667,6.6666]
### Note ###
Compared to similar functions available on File Exchange, these functions:
- Allow N-D input matrices.
- Allow significant-figures or decimal-places to be defined with either a scalar or a matrix.
- Return Inf and NaN values unchanged.
- Correctly round complex numbers (real & imaginary separately).
- Do not call "feval", "eval", "num2str" or "str2num".
- Do not call "roundn" (available in the mapping toolbox).
- Rounding methods distinguish between X.5 and X.5+/-eps. |