-
Notifications
You must be signed in to change notification settings - Fork 5
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
Ability to round up/down in multiply and divide operations #13
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this, it’s looking great! And thanks for fixing the rounding bug 🙏
What would you think of having optionsOrDecimals
as a third parameter, following what we are doing with other functions using optionsOrDigits
?
Also, would it be worth adding the ability to pass a Rounding
to dnum.round()
?
Thanks @bpierre for looking at the PR, replying to your comments
Sorry I don't understand what this means, can you elaborate?
Done :) |
Actually I now realise the library already has |
I mean doing this: export function divide(
num1: Numberish,
num2: Numberish,
optionsOrDecimals?: Decimals | {
decimals?: Decimals,
rounding?: Rounding,
} = {},
): Dnum {
const options = typeof optionsOrDecimals === "number"
? { decimals: optionsOrDecimals }
: optionsOrDecimals
options.rounding ??= "ROUND_HALF"
// …
} To follow the API of e.g. export function format(
dnum: Dnum,
// see toParts() in src/dnum.ts
optionsOrDigits: Parameters<typeof toParts>[1] & {
compact?: boolean;
locale?: ConstructorParameters<typeof Intl.NumberFormat>[0];
signDisplay?: SignDisplay;
} = {},
): string {
const options = typeof optionsOrDigits === "number"
? { digits: optionsOrDigits }
: optionsOrDigits;
// … The general idea being to pick one option from the options object and allow it to be passed directly as a shortcut.
It does yes, but I thought it could be nice to allow using |
Okay I understand now - I don't really have a preference. I followed what you had done for
I agree it's probably a nice thing to have and I don't see it being confusing. Now that you mentioned it, I simplified |
Thinking more about it, I think it would be weird to require consumers to set
Nice! We might even end up with a smaller size than before 😄 |
@bpierre I updated the three operations with |
@gidonkatten looking good! For the docs: maybe we could do it the same way as
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @gidonkatten 🙏
The default behaviour for the
multiply
anddivide
operations was toROUND_HALF
. Added the possibility to specify whether toROUND_UP
orROUND_DOWN
. Rounding down is especially important as that is the typical behaviour in smart contract opcodes (EVM and non-EVM alike).Also fixed a bug in the
divideAndRound
function (which is called by many operations) when the divisor is negative. E.g.divideAndRound(-15n, 2n))
anddivideAndRound(15n, -2n)
yielded different results.Updated docs with latest changes.