-
Notifications
You must be signed in to change notification settings - Fork 857
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
[experimental] A library to make it easy to do math and work with amounts #2125
Labels
enhancement
New feature or request
Comments
Bullish on a string formatting API that looks something like: function formatLamportsAsSol(formatter: Intl.NumberFormat, lamports: Lamports): string {
return formatter.format(`${lamports}E-9`);
}
function formatSolAsLamports(formatter: Intl.NumberFormat, solDecimalString: string): string {
return formatter.format(`${solDecimalString}E9`);
} Nicely does: console.log(formatSolAsLamports(formatter, '2.123456789'));
> "2123456789" ^ these are my half-baked thoughts. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The new web3.js leans into JavaScript
BigInts
and opaque types for amounts likeLamports
. WhileBigInts
can prevent truncation errors (see #1116) they are in some ways more difficult to do math on. While theLamports
type gives developers a strong and typesafe signal that a given input or output is in lamports rather than SOL, it offers no way to convert between the two or to prepare one for UI display in the other.The goal of this issue is to develop an API that allows developers to do both without introducing rounding or precision errors.
Spitballing an API
Values as basis points
Starting with values expressed as an opaque type having basis points and a decimal:
And coercion functions:
Important
Decimals must be 0n or greater.
Math
TODO
Formatting
Tip
Apparently we can achieve UI display using
Intl.NumberFormat
v3 by leaning on scientific notation. Check it out.See also
Convert values to decimal strings for formatting.
Let callers supply a formatter.
Maybe, just maybe, we can make a passthrough:
The text was updated successfully, but these errors were encountered: