Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Number.toFixed function #621

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions devs/run-tests/02numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
13 changes: 12 additions & 1 deletion packages/core/src/corelib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ import "./button"
import "./magneticfieldlevel"
import "./buzzer"
import "./ledstrip"
import "./gamepad"
import "./gamepad"
import "./number"
3 changes: 3 additions & 0 deletions packages/core/src/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Number.prototype.toFixed = function(digits: number = 0): string {
return "";
}
3 changes: 2 additions & 1 deletion runtime/devicescript/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down