Skip to content
Merged
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
22 changes: 22 additions & 0 deletions src/BigDecimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ function testUnnecessary(
}
}

test("BigDecimal parsing", (t) => {
const passes = new BigDecimal("1.1")
t.is(passes.toString(), "1.1")
t.is(passes.scale(), 1)
t.is(passes.unscaledValue(), 11n)

let e = t.throws(() => new BigDecimal("1.1.1"))
t.snapshot(e, "too many decimal places")
e = t.throws(() => new BigDecimal("1.1e1"))
t.snapshot(e, "weird exponential notation")
e = t.throws(() => new BigDecimal("1.1e10"))
t.snapshot(e, "normal exponential notation")
e = t.throws(() => new BigDecimal("1.1e"))
t.snapshot(e, "other weird exponential notation")
e = t.throws(() => new BigDecimal("1.1e1.1"))
t.snapshot(e, "super weird exponential notation")
e = t.throws(() => new BigDecimal("abcd"))
t.snapshot(e, "not a number")
e = t.throws(() => new BigDecimal("zxy"))
t.snapshot(e, "another not a number")
})

test("BigDecimal rounding", (t) => {
testRounding(t, "5.5", "6", {
precision: 0,
Expand Down
44 changes: 44 additions & 0 deletions src/BigDecimal.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,50 @@ The actual snapshot is saved in `BigDecimal.test.ts.snap`.

Generated by [AVA](https://avajs.dev).

## BigDecimal parsing

> too many decimal places

Error {
message: 'BigDecimal: multiple decimal points found',
}

> weird exponential notation

Error {
message: 'BigDecimal: exponential notation is not supported',
}

> normal exponential notation

Error {
message: 'BigDecimal: exponential notation is not supported',
}

> other weird exponential notation

Error {
message: 'BigDecimal: exponential notation is not supported',
}

> super weird exponential notation

Error {
message: 'BigDecimal: exponential notation is not supported',
}

> not a number

Error {
message: 'BigDecimal: not a number',
}

> another not a number

Error {
message: 'BigDecimal: not a number',
}

## BigDecimal.compareTo

> Snapshot 1
Expand Down
Binary file modified src/BigDecimal.test.ts.snap
Binary file not shown.
15 changes: 13 additions & 2 deletions src/BigDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,21 @@ export class BigDecimal {
}

// split into integer and decimal parts
const [integer, decimal] = str.split(".")
const [integer, decimal, ...rest] = str.split(".")
if (rest.length > 0) {
error("BigDecimal: multiple decimal points found")
}

// The unscaled value is the integer part followed by the decimal part
this.#value = BigInt(`${integer}${decimal ?? ""}`)
try {
this.#value = BigInt(`${integer}${decimal ?? ""}`)
} catch (e) {
if (e instanceof SyntaxError) {
error("BigDecimal: not a number")
}

throw e
}
// The initial scale is either given (needed for certain math operations) or the length of the decimal part
this.#scale = scale ?? decimal?.length ?? 0

Expand Down
Binary file modified src/collections.test.ts.snap
Binary file not shown.
Loading