Solidity
如何在類型腳本中減去大數字?
我正在嘗試在類型腳本中減去大數字,但出現錯誤…
const supply1 = 100000; const _amount = 100; const supply = ethers.utils.parseEther(`${supply1}`); const diff = supply - _amount;
該
ethers.utils.parseEther
函式返回一個BigNumber。要對其進行算術運算,您必須顯式呼叫操作實現,在您的情況下,這將是
.sub
需要BigNumberish參數的方法。所以你想改變:
const diff = supply - _amount;
到 :
const diff = supply.sub(_amount);
請記住,
diff
它將是 BigNumber 類型,如果您需要將其轉換為其他類型,例如數字或字元串,您可以參考文件的相應部分。