HP Forums

Full Version: (12C) Banker's Rounding Method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The banker's rounding method involves rounding numeric amounts to the nearest integer. When the number ends in 0.5 (1.5, 3.5, 8.5, etc), special rules apply:

The decimal gets rounded to the nearest even integer. For example: 0.5, 2.5, and 4.5 round down to 0, 2, and 4 respectively. However, 1.5, 3.5, and 5.5 are rounded up to 2, 4, and 6 respectively.

Code:
001  STO 0
002  INTG
003  STO 1
004  LSTx  
005  FRAC
006  STO 2
007   .
008  5
009   -
010   x=0
011   GTO 018
012   RCL 0
013   FIX 0
014   RND
015   STO 4
016   FIX 2
017   GTO 000
018   RCL 1 
019   2
020   ÷
021   FRAC
022   x=0
023   GTO 029
024   RCL 1
025   1
026   +
027   STO 4
028   GTO 000
029   RCL 1
030   STO 4
031   GTO 000

Original blog post: https://edspi31415.blogspot.com/2018/11/...nkers.html
I think banker's rounding method apply to non-integers too (edge case round-to-even).

For banker's rounding to nearest integer, this is shorter

Code:
01 .
02 5
03 X≤Y ; handle negative input
04 CHS
05 -
06 FRAC
07 x=0 ; edge case ?
08 GTO 12
09 Lst-X
10 INTG
11 GTO 00
12 Lst-X ; edge round-to-even
13 2
14 ÷
15 INTG
16 2
17 ×
18 GTO 00

Example:
1233.5 R/S ==> 1234
1233.9 R/S ==> 1234
1234.5 R/S ==> 1234

1233.49 R/S ==> 1233
1234.51 R/S ==> 1235

Updated: code numbering start from 1, instead of 0
Just a slight correction since the code starts at line 1, not 0:

Code:
01   .
02   5
03   x ≤ y
04  CHS
05  -
06  FRAC
07 x = 0
08 GTO 12
09 LST x
10 INTG
11 GTO 00
12 LST x
13 2
14 ÷
15 INTG
16 2
17 *
18 GTO 00

Otherwise, brilliant!
Reference URL's