round-to-decimal ( x n -- y ) (original) (raw)

Vocabulary
math.functions

Inputs

x a real
n an integer

Outputs

y a real

Word description
Outputs the number closest to x, rounded to n decimal places.

Notes
The result is not necessarily an integer.

Examples

USING: math.functions prettyprint ; 1.23456 2 round-to-decimal .
1.23

USING: math.functions prettyprint ; 12345.6789 -3 round-to-decimal .
12000.0

Definition

USING: kernel math ;

IN: math.functions

: round-to-decimal ( x n -- y ) 10^ [ * round ] [ / ] bi ;