Is it possible round up div
operation for BigNumber
?
#2474
julianmrodri
started this conversation in
General
Replies: 1 comment
-
Rounding is actually a far more complex topic, as there are many strategies to "break the tie", but I think what you are describing is actually finding the ceiling, which doesn't deal with tie situations. You can absolutely use the // Your specific case
BigNumber(1428571428571428571428).add(99).div(100)
// In general:
value = 1428571428571428571428
divisor = 100
BigNumber(value).add(divisor - 1).div(divisor) For a quick check on what you want consider the following: // Rounding (towards infinity):
> 120 / 100 => 1.2 => 1
> 150 / 100 => 1.5 => 2
> 170 / 100 => 1.7 => 2
// Ceiling:
> 120 / 100 => 1.2 => 2
> 150 / 100 => 1.5 => 2
> 170 / 100 => 1.7 => 2 Other common rounding strategies are toward-zero, away-from-zero, toward-infinity, toward-negative-infinity, truncation, toward-even, toward-odd and stochastic. Each has their specific purposes and use cases. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I need to round up a
div
operation for aBigNumber
but couldnt find a way to do this.Basically I would like
BigNumber(1428571428571428571428).div(100)
to round up and return for example14285714285714285715
instead of14285714285714285714
.Is there a way to do this? What is the best way to accomplish it in
ethers.js
? Should I be usingFixedNumber
for these type of operations?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions