Comments (10)
Kind of forced to use BigNumber since BN doesn't support decimal (and therefore percents) will try your solution later and post if it worked.
Thanks again for your help!
from bn.js.
Unfortunately BN
does not support decimals.
from bn.js.
@shuckster so what a work around could be then?
The numbers that I am retrieving using truffle
are all BN
and I need to calculate a percentage based on 2 BN
.
from bn.js.
Since you seem to be forced into using two different big-number libraries, it may help you to think about separating your data-values from your data-processors.
Enforce a rule where your numbers must always be string
, never BN
or BigNumber
:
const senderBalanceAsBN = await this.token.balanceOf(sender);
const burnTaxAsBN = await this.token.getBurnTax();
const senderBalance = senderBalanceAsBN.toString();
const burnTax = burnTaxAsBN.toString();
const expectedTokenBurnt = BigNumber(burnTax).dividedBy(senderBalance).multipliedBy(100).toString();
const expectedTokenBurntAsBN = new BN(expectedTokenBurnt);
This is what I've tried to show in the example above: The "numbers" are all string
. If something is BN
, I added *AsBN
so I can see that. Both BN
and BigNumber
have a toString()
method, so always use this to keep your data "pure".
You can then freely choose between BN
and BigNumber
to do your calculations.
from bn.js.
Solved it this way:
const burnTax = await this.token.getBurnTax();
const senderBalance = await this.token.balanceOf(sender);
// TODO: Track ticket https://github.com/indutny/bn.js/issues/275
const expectedTokenBurnt = BigNumber(burnTax.toString())
.dividedBy(100)
.multipliedBy(senderBalance.toString())
.toFixed();
const receipt = await this.token.transfer(recipient, senderBalance, {
from: sender,
});
await expectEvent(receipt, "Transfer", {
from: sender,
to: recipient,
value: senderBalance.sub(new BN(expectedTokenBurnt)),
});
await expectEvent(receipt, "Burn", {
from: this.token.address,
amount: new BN(expectedTokenBurnt),
});
Any plans in the roadmap to add support for decimals?
from bn.js.
Roadmap? I'm not the author of BN
. :)
I'm just looking at this repo because I'm currently interested in big-number libraries. The reason is I'm working on an eslint-plugin that can convert normal JS to BigNumber
:
It's not perfect yet, but it's useful. I don't think it would work for your use-case very well, unfortunately.
Nice work fixing your issue. Good call on using toFixed()
to avoid the e
-syntax!
from bn.js.
Your plugin looks excellent! Dumb and out of the topic question but what are the added benefits or relying on BigNumbers
rather than normal JS
arithmetic? I had to look into BigNumber only because that's what truffle uses to test smart contract, other than that I don't really have an exposure to that.
from bn.js.
Thanks! The short-answer to why libraries like BN
or BigNumber
are needed is because of this:
const sum = 0.1 + 0.2
sum === 0.3
// false
sum
// 0.30000000000000004
Doesn't look right, eh? It's complicated to explain why this happens exactly, but it's to do with how floating-point numbers are stored. They have a binary-representation, not decimal. As a mental exercise, try and think of how 0.1 should be represented as a power-of-two, and how you'd go about removing the errors.
Well, since 1985 this has been the standard for FP in computers. It was fine at the time for performance and scientific reasons (where accuracy is not always important) but for finance it's a disaster.
The other reason to use big-number libraries is because default JS
has a maximum number size. I notice that truffle
is a block-chain library, so probably deals with numbers vastly bigger than what the native arithmetic can handle!
from bn.js.
from bn.js.
You're welcome, and yeah, it's pretty surprising that things still work this way. The standard is IEEE-754 and is used in many languages, not just JavaScript, so it's good to check which kind of FP standard is being used to drive the number-types of your preferred languages.
Integers are immune to these problems, but of course they have a very low maximum-size, which might not work well for block-chain applications.
from bn.js.
Related Issues (20)
- muln with fractional number gives wrong result HOT 3
- Add method for "not equal"
- Assertion failure error message could be helpful
- Use bn.js instead of bignumber.js
- Incorrect multiplication result when using muln() HOT 1
- Converting json.stringified BN back into BN HOT 2
- use in web HOT 1
- TypeError: bn_js_1.default is not a constructor
- Add support for raising a `BN` object to the power of a plain JavaScript Number
- Function `mod` gives incorrect results when the arguments are of opposite signs
- setn function signature in index.tx doesn't correspond with the library HOT 1
- Which rounding method is used?
- Base64 encoding
- Wrong `cmpn` result HOT 1
- The unit of length in `toBuffer(endian, length)` is unclear
- Property 'divmod' does not exist on type 'BN'
- Add link to library this is based on to docs
- BN.muln with number bigger than 0x4000000 HOT 1
- I'm gonna find every one of you
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bn.js.