diff --git a/AUTHORS b/AUTHORS index d0556f92..23d317f3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -19,3 +19,4 @@ Will Speak Damien Radtke Max Ferguson Sven Bergström +Kyle Charters diff --git a/doc/site/modules/core/num.markdown b/doc/site/modules/core/num.markdown index 7f9a4870..e11c07e3 100644 --- a/doc/site/modules/core/num.markdown +++ b/doc/site/modules/core/num.markdown @@ -97,6 +97,15 @@ The natural logarithm of the number. Raises this number (the base) to `power`. Returns `nan` if the base is negative. +### **round** + +Rounds the number to the nearest integer. + + :::wren + System.print(1.5.round) //> 2 + System.print((-3.2).round) //> -3 + System.print((-3.7).round) //> -4 + ### **sin** The sine of the number. diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index ed452361..c1d3bb32 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -593,6 +593,7 @@ DEF_NUM_FN(ceil, ceil) DEF_NUM_FN(cos, cos) DEF_NUM_FN(floor, floor) DEF_NUM_FN(negate, -) +DEF_NUM_FN(round, round) DEF_NUM_FN(sin, sin) DEF_NUM_FN(sqrt, sqrt) DEF_NUM_FN(tan, tan) @@ -1270,6 +1271,7 @@ void wrenInitializeCore(WrenVM* vm) PRIMITIVE(vm->numClass, "cos", num_cos); PRIMITIVE(vm->numClass, "floor", num_floor); PRIMITIVE(vm->numClass, "-", num_negate); + PRIMITIVE(vm->numClass, "round", num_round); PRIMITIVE(vm->numClass, "sin", num_sin); PRIMITIVE(vm->numClass, "sqrt", num_sqrt); PRIMITIVE(vm->numClass, "tan", num_tan); diff --git a/test/core/number/round.wren b/test/core/number/round.wren new file mode 100644 index 00000000..10cbccc9 --- /dev/null +++ b/test/core/number/round.wren @@ -0,0 +1,8 @@ +System.print(123.round) // expect: 123 +System.print((-123).round) // expect: -123 +System.print(0.round) // expect: 0 +System.print((-0).round) // expect: -0 +System.print(0.123.round) // expect: 0 +System.print(12.3.round) // expect: 12 +System.print((-0.123).round) // expect: -0 +System.print((-12.3).round) // expect: -12