diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-05 00:29:01 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-05 00:29:01 +0200 |
commit | 16bd99aa527ff13dd92cf52c51c0d9fca9054f6c (patch) | |
tree | 965926cf15c2f2189080266a111d799184f464f1 /Libraries/LibJS | |
parent | afff22830862d8812880ec92d37031a56ef799d8 (diff) | |
download | serenity-16bd99aa527ff13dd92cf52c51c0d9fca9054f6c.zip |
LibJS: Add Math.round()
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r-- | Libraries/LibJS/Runtime/MathObject.cpp | 13 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/MathObject.h | 1 |
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&); }; } |