简体   繁体   English

如何将 BigNumber 转换为 Typescript 中的普通数字

[英]How to convert a BigNumber to a normal number in Typescript

Guys I am fetching the data from smart contract and it gives a big number in response, I want to convert it into a normal number so that I can use it in order to create a bar chart.伙计们,我正在从智能合约中获取数据,它给出了一个大数字作为响应,我想将其转换为普通数字,以便我可以使用它来创建条形图。 How can this be done?如何才能做到这一点? 在此处输入图像描述

This is the piece of code I wrote so that I could store the values in an array:这是我编写的一段代码,以便可以将值存储在数组中:

//Creating candidatevoterarray for graph
    for (var i =0; i<= 4; i++){
        const temparray = this.candidatearray[i]
        const count = temparray.voteCount
        // this.candidatevotearray.push(count)
        console.log(count)
  }

as docs https://docs.ethers.io/v5/api/utils/bignumber/ say there is a method .toNumber() in BigNumber作为文档https://docs.ethers.io/v5/api/utils/bignumber/说 BigNumber 中有一个方法.toNumber()

There is no such thing as a BigNumber in either TypeScript or ECMAScript , nor in Angular . TypeScriptECMAScript以及BigNumber中都没有BigNumber这样的东西。 This means that the BigNumber must be something you wrote yourself or coming from some library you are not telling us about.这意味着BigNumber必须是您自己编写的,或者来自您没有告诉我们的某个库。

If it is something you wrote yourself, then only you can know how to convert it to a number or a bigint .如果它是您自己编写的,那么只有您知道如何将其转换为numberbigint If it is something from a library, you will have to look into the documentation of that library.如果它来自库,您将不得不查看该库的文档。

However, please note that the main reason why someone would write and/or use something like a BigNumber class is because it cannot be represented by number or bigint in the first place, So, it may very well be the case that your BigNumber cannot be converted (accurately) into number or bigint at all .但是,请注意,有人会编写和/或使用BigNumber class 之类的主要原因是因为它首先不能用numberbigint表示,所以很可能是您的BigNumber不能完全转换(准确)成numberbigint

For example, I just randomly found a library which defines a class / constructor function named BigNumber called bignumber.js .例如,我只是随机找到了一个库,它定义了一个名为BigNumber 的 class / 构造函数 function ,名为 bignumber.js BigNumber It has a BigNumber.prototype.toNumber method , which converts an instance of BigNumber to an ECMAScript primitive number .它有一个BigNumber.prototype.toNumber方法,该方法将BigNumber的实例转换为 ECMAScript 原始number But of course, there are infinitely more BigNumber s than there are number s (more precisely, there are countably infinite BigNumber s but only 2 64 number s), so this conversion cannot possibly be accurate.但是当然, BigNumber的数量比number的数量要多得多(更准确地说, BigNumber的数量可以无限多,但只有 2 64 个number ),所以这种转换不可能是准确的。 In particular, there is a smallest and a biggest number but there is no smallest or biggest BigNumber , so any BigNumber bigger than the biggest number or smaller than the smallest number cannot be represented at all.特别是,有一个最小和一个最大的number ,但没有最小或最大的BigNumber ,因此任何大于最大number或小于最小numberBigNumber根本无法表示。

I do something like this:我做这样的事情:

    const getBalance = async () => {
        try {
            const balance = await token.balanceOf(address)
            console.log(Number(balance._hex))
            return balance;
        } catch (error) {
            console.log(error)
        }
    }

        useEffect(() => {
        getBalance().then((res) => setOwnerBalance(Number(res._hex)))    
    }, [data])  

    console.log(Number(balance._hex))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM