# Programs

## Mathematics Programs

### Rounding

#### Summary

Programs:

ROUND, ROUNDNEA, ROUNDUP, ROUNDDOW, ROUNDDIG

Version:

1.0, 4 September 2001

Description:

These programs round a quantity to either a specified number of digits or a specified digits place. All of round to even nearest, round to higher nearest, round up, and round down are provided.

Compatibility:

 CFX-9850G Compatible with CFX-9850G FX-7400G Compatible with FX-7400G

#### Detailed Description

Rounding a numeric quantity to the nearest specified digits place is finding the closest value whose decimal representation ends at the specified digits place. For example, 1.573329 rounds to 2, 1.6, 1.57, 1.573, 1.5733, or 1.57333 as the rounding occurs to the units, tenths, hundredths, thousandths, ten thousandths, or hundred thousandths place.

When a quantity is exactly half way between two available values, the direction in which to round is not obvious. Typically, rounding takes place to the available value farther away from zero. This is called round to higher nearest, and is generally the rounding method taught in primary school. However, rounding to the value with an even last digit has better error properties. This is called round to even nearest, and is generally the rounding method used in statistics and finance.

Two other operations are sometimes desired: choosing the closer to zero of the two nearest values whose decimal representation ends at a specified digits place, or choosing the farther from zero of the two values. These operations are not necessarily rounding, as the closer of the two values to the value being “rounded” is not always chosen. However, these operations are termed rounding down (or rounding towards zero, or truncation) and rounding up (or rounding towards infinity). Thus, the value 1.1 would round down to 1 and round up to 2. Consistently but perhaps surprisingly, when rounding to multiples of 100 (the digit place is 2), 1.1 would round down to 0 and round up to 100.

The program ROUND performs a round to even nearest operation. The program ROUNDNEA performs a round to higher nearest operation. The program ROUNDUP performs a round up operation. The program ROUNDDOW performs a round down operation. Finally, the program ROUNDDIG performs a round to even nearest operation to a digit position scaled by the magnitude of the value being rounded: it rounds the value to a specified number of digits, rather than a specified digit place.

#### Usage

Load the desired rounding program. Place the value to be rounded in A and the power of 10 of the digit place at which round is to occur in B. (For program ROUNDDIG, place the number of digits in B.) Call the desired rounding program. The rounded value is returned in Ans.

Digit places are indicated by the power of ten represented by the digit place: 1 indicates tens, 0 indicates units, -1 indicates tenths, and so forth.

#### Program Source

The programs are available as a text file with .CAT contents, or may be entered as shown below. A semicolon (“;”) marks the beginning of a comment, and is not to be entered into the calculator. Remember that these programs are copyrighted; see the copyright issues page for limitations on redistribution.

Program ROUND (84 bytes):

```; Variables:
;   A is value to round
;   B is digit place to which to round
; Symbols:
;   10^x is ten to the x'th operator
;   -> is assignment arrow
;   < is less than relational
;   (-) is unary negation operator
;   / is divide operator
;   = is equality relational

10^x(Int B)->B     ; Turn power of 10 into scale factor
If A<0             ; If A is negative
Then (-)B->B       ;   hold sign on power of 10
IfEnd              ; [End of test if A is negative]
Abs (A/B)+.5->A    ; Get scaled A, plus half
If A=Int A         ; Test whether is integer (scaled A ends in .5)
Then 2Int (A/2)->A ;   If so, round down to even
Else Int A->A      ;   If not, truncate (after adding .5, round)
IfEnd              ; [End of test whether scaled A is integer]
AB->A              ; Rescale A and restore sign
A                  ; Return as Ans
```

Program ROUNDNEA (58 bytes):

```; Variables:
;   A is value to round
;   B is digit place to which to round
; Symbols:
;   10^x is ten to the x'th operator
;   -> is assignment arrow
;   < is less than relational
;   (-) is unary negation operator
;   / is divide operator

10^x(Int B)->B     ; Turn digit place into scale factor
If A<0             ; If A is negative
Then (-)B->B       ;   hold sign on power of 10
IfEnd              ; [End of test if A is negative]
BInt (Abs (A/B)+.5)->A
; Scale A, round, rescale, and reapply sign
A                  ; Return as Ans
```

Program ROUNDUP (75 bytes):

```; Variables:
;   A is value to round
;   B is digit place to which to round
; Symbols:
;   10^x is ten to the x'th operator
;   -> is assignment arrow
;   < is less than relational
;   (-) is unary negation operator
;   / is divide operator

10^x(Int B)->B     ; Turn digit position into scale factor
If A<0             ; If A is negative
Then (-)B->B       ;   then hold sign on B
IfEnd              ; [End of test if A is negative]
Abs (A/B)->A       ; Scale A
If A<>Int A        ; Test if A is non integral
Then 1+Int A->A    ;   If so, round up
IfEnd              ; [End of test if A is non integral]
AB->A              ; Rescale A
A                  ; Return as Ans
```

Program ROUNDDOW (36 bytes):

```; Variables:
;   A is value to round
;   B is digit place to which to round
; Symbols:
;   10^x is ten to the x'th operator
;   -> is assignment arrow
;   / is divide operator

10^x(Int B)->B     ; Turn digit index into scale factor
BInt (A/B)->A      ; Scale, truncate, and rescale A
A                  ; Return as Ans
```

Program ROUNDDIG (47 bytes):

```; Variables:
;   A is value to round
;   B is number of places to which to round
; Symbols:
;   10^x is ten to the x'th operator
;   <> is not equal relational
;   -> is assignment arrow

If A<>0            ; Test if A is zero
Then Int log Abs A-B+1->B
;   If not, turn relative digit position into
;     absolute digit position
IfEnd              ; [End of test if A is zero]
Prog "ROUND"       ; Do round to even nearest
```

Page last updated 25 November 2001.

# Brian’s Casio Calculator Corner

Home

Programs

Index

Mathematics

Index

Factorization

Bisection

Newton’s Method

Complex Zeroes

Best Fraction Approximation

Pseudorandom Numbers

Romberg Integration

First Degree ODE

Rounding

Permutations

Finance

Physics

Utility

Tutorial

Puzzles

Site Information