REBOL [
Title: "Round"
Date: 3-Jul-2002
Name: Round
Version: 1.0.0
File: %Round.r
Author: "Andrew Martin"
Purpose: "Rounds a number at any given place.^/"
Email: Al.Bri@xtra.co.nz
Web: http://valley.150m.com
Category: [util math 1]
Acknowledgements: {
^-^-Thanks to Geo Massar and for his example code.
^-^-Thanks to Christian "CHE" Ensel for inspiration.
^-^-}
Example: [
round 123.45
round 123.55
round -123.45
round -123.55
round/at 123.344 2
round/at 123.345 2
round/at 12345 -2
round/at -12345 -2
round/at (1.22 // 1E-2) 2
round/at 214748.36471 4
round/at 214748.36477 4
]
]
Round: func [
"Rounds a number at any given place."
[catch]
Number [number!]
/At Place [integer!]
][
throw-on-error [
Place: either none? Place [1] [10 ** Place]
Number: Place * Number
Number: Number + either positive? Number [0.5][-0.5]
Number: Number - (Number // 1)
Number / Place
]
]