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

$string(number) capped at precision-15 #669

Closed
grant-d opened this issue Jan 29, 2024 · 1 comment
Closed

$string(number) capped at precision-15 #669

grant-d opened this issue Jan 29, 2024 · 1 comment

Comments

@grant-d
Copy link

grant-d commented Jan 29, 2024

We have some integers (below the safe limit of 2^53 - 1) that are being rounded to precision 15 once we use $string() on them.
This seems due to the following line of code, specifically the positive unary case, ? Number(val.toPrecision(15))

return (typeof val !== 'undefined' && val !== null && val.toPrecision && isNumeric(val)) ? Number(val.toPrecision(15)) :

I can submit a PR but my question is whether there is a good reason for the limit. I would not want to regress something else.

Repro:

$string(1e15 - 1) //  999...999 (15 digits, correct result)
$string(1e15    ) // 1000...000 (16 digits, correct result)
$string(1e15 + 1) // 1000...000 (16 digits, wrong result - notice no trailing 1)

// For completeness, Number.MAX_SAFE_INTEGER = 2^53-1 = 9,007,199,254,740,991 = ~1e16
$string(9007199254740991) // 9007199254740990 (16 digits, wrong result - notice rounding on last digit)
@andrew-coleman
Copy link
Member

The precision is limited to avoid the inaccuracy of decimal arithmetic showing up in the string (see #194 (comment)). This even shows up occasionally for toPrecision(16), e.g with the expression Account.Order.$string($sum(Product.(Price* Quantity))) on the exerciser sample data.

To work around this, since you know you are dealing with integers, use the $formatInteger() function instead of $string().

E.g. $formatInteger(9007199254740991, '0') => "9007199254740991"

@grant-d grant-d closed this as completed Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants