diff --git a/devs/run-tests/02numbers.ts b/devs/run-tests/02numbers.ts index a8c93e682d..e26621618e 100644 --- a/devs/run-tests/02numbers.ts +++ b/devs/run-tests/02numbers.ts @@ -242,10 +242,15 @@ function testEnumToString() { assert(enumTest + "" === "1", "enum tostring in concatenation") } +function testToFixed() { + const numObj = 12345.6789; + + assert(numObj.toFixed(5) === '12346', 'testToFixed') // '12346'; rounding, no fractional part +} + testComma() testNums() testNaN() testUnaryPlus() testEnumToString() - - +testToFixed() \ No newline at end of file diff --git a/packages/core/src/corelib.d.ts b/packages/core/src/corelib.d.ts index 09a94a2b29..845dd7bc3b 100644 --- a/packages/core/src/corelib.d.ts +++ b/packages/core/src/corelib.d.ts @@ -197,7 +197,18 @@ interface StringConstructor { declare var String: StringConstructor interface Boolean {} -interface Number {} + +interface NumberConstructor { + readonly prototype: Number +} + +declare var Number: NumberConstructor + +interface Number { + + // todo + toFixed(digits?: number): string +} interface RegExp {} interface IterableIterator {} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 99a878bcdc..8d8b740b2e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -14,4 +14,5 @@ import "./button" import "./magneticfieldlevel" import "./buzzer" import "./ledstrip" -import "./gamepad" \ No newline at end of file +import "./gamepad" +import "./number" \ No newline at end of file diff --git a/packages/core/src/number.ts b/packages/core/src/number.ts new file mode 100644 index 0000000000..a8cf82b0d7 --- /dev/null +++ b/packages/core/src/number.ts @@ -0,0 +1,3 @@ +Number.prototype.toFixed = function(digits: number = 0): string { + return ""; +} \ No newline at end of file diff --git a/runtime/devicescript/objects.c b/runtime/devicescript/objects.c index b75762b7b7..1e80756c70 100644 --- a/runtime/devicescript/objects.c +++ b/runtime/devicescript/objects.c @@ -261,8 +261,9 @@ static const uint8_t builtin_proto_idx[] = { [DEVS_BUILTIN_OBJECT_BUFFER] = 10, [DEVS_BUILTIN_OBJECT_GPIO_PROTOTYPE] = 11, [DEVS_BUILTIN_OBJECT_GPIO] = 12, + [DEVS_BUILTIN_OBJECT_NUMBER_PROTOTYPE] = 13, }; -#define MAX_PROTO 12 +#define MAX_PROTO 13 devs_maplike_t *devs_get_builtin_object(devs_ctx_t *ctx, unsigned idx) { if (idx < sizeof(builtin_proto_idx)) {