Rounding Functions

Rounding to the Nearest Integer

Function Syntax (LM:Round <n>)
Current Version 1.0
Donate
Arguments
Symbol Type Description
n Real Real Number to Round
Returns
Type Description
Integer The result of rounding the supplied number
Select all
(defun LM:Round ( n ) (fix (+ n (if (minusp n) -0.5 0.5))))

Example Function Call

_$ (LM:Round 4.56)
5

Rounding to a Number of Decimal Places

Function Syntax (LM:RoundTo <n> <p>)
Current Version 1.0
Donate
Arguments
Symbol Type Description
n Real Real Number to Round
p Integer Number of decimal places to round to
Returns
Type Description
Real The result of rounding the supplied number to the specified number of decimal places.
Select all
(defun LM:Roundto ( n p ) (/ (fix (+ (* n (setq p (expt 10. p))) (if (minusp n) -0.5 0.5))) p))

Example Function Call

_$ (LM:Roundto 6.7352 3)
6.735
_$ (LM:Roundto 6.7352 2)
6.74
_$ (LM:Roundto 6.7352 1)
6.7
_$ (LM:Roundto 6.7352 0)
7.0

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010