summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Libraries/LibJS/Runtime/MathObject.cpp13
-rw-r--r--Libraries/LibJS/Runtime/MathObject.h1
2 files changed, 14 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp
index 00425bf4c4..5258fc9fc1 100644
--- a/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Libraries/LibJS/Runtime/MathObject.cpp
@@ -38,6 +38,7 @@ MathObject::MathObject()
put_native_function("random", random);
put_native_function("sqrt", sqrt, 1);
put_native_function("floor", floor, 1);
+ put_native_function("round", round, 1);
put("E", Value(M_E));
put("LN2", Value(M_LN2));
@@ -96,4 +97,16 @@ Value MathObject::floor(Interpreter& interpreter)
return Value(::floor(number.as_double()));
}
+Value MathObject::round(Interpreter& interpreter)
+{
+ if (!interpreter.argument_count())
+ return js_nan();
+
+ auto number = interpreter.argument(0).to_number();
+ if (number.is_nan())
+ return js_nan();
+ // FIXME: Use ::round() instead of ::roundf().
+ return Value(::roundf(number.as_double()));
+}
+
}
diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h
index 6e11daf627..f72637ab2a 100644
--- a/Libraries/LibJS/Runtime/MathObject.h
+++ b/Libraries/LibJS/Runtime/MathObject.h
@@ -42,6 +42,7 @@ private:
static Value random(Interpreter&);
static Value sqrt(Interpreter&);
static Value floor(Interpreter&);
+ static Value round(Interpreter&);
};
}