Skip to content

Using BigInts with Amount should not be painful #107

@eemeli

Description

@eemeli

With the changes in #86 and #104, we've introduced a regression for BigInt Amounts. Specifically, we've made it so that seemingly innocent code like this will fail:

let a = new Amount(42n, { unit: 'kilometer' });
let b = a.convertTo({ unit: 'meter', fractionDigits: 0 });
BigInt(b.value); // Uncaught SyntaxError: Cannot convert 4.2000e+4 to a BigInt

or even more simply:

let a = new Amount(42n, { fractionDigits: 0 });
BigInt(a.value); // Uncaught SyntaxError: Cannot convert 4.2e+1 to a BigInt

The problem here is that while Number('4.2000e+4') succeeds, BigInt() can't handle string inputs in exponential notation, even if they're representing an integer value.

I think it would be nice if the code presented above would not fail.

My (at least initially) preferred solution here would be to use SignedInteger notation to represent integer values for which the precision matches the integer digit count, i.e. using '42' instead of '4.2e+1'.

This won't of course guarantee that all values are convertible to bigints, but it would at least help ensure that with { fractionDigits: 0 } they are.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions