Giter Site home page Giter Site logo

Easiest way to get percentage about bn.js HOT 10 CLOSED

aress31 avatar aress31 commented on September 1, 2024
Easiest way to get percentage

from bn.js.

Comments (10)

aress31 avatar aress31 commented on September 1, 2024 1

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.

shuckster avatar shuckster commented on September 1, 2024

Unfortunately BN does not support decimals.

from bn.js.

aress31 avatar aress31 commented on September 1, 2024

@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.

shuckster avatar shuckster commented on September 1, 2024

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.

aress31 avatar aress31 commented on September 1, 2024

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.

shuckster avatar shuckster commented on September 1, 2024

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:

Video of plugin-usage in VSCode

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.

aress31 avatar aress31 commented on September 1, 2024

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.

shuckster avatar shuckster commented on September 1, 2024

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.

aress31 avatar aress31 commented on September 1, 2024

from bn.js.

shuckster avatar shuckster commented on September 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.