【发布时间】:2022-01-08 12:36:33
【问题描述】:
我可能在做一些愚蠢的事情,但我得到了错误
Uncaught (in promise) Error: Assertion failed,特别是它在 BN.js 内部检查的地方:
function assert (val, msg) {
if (!val) throw new Error(msg || 'Assertion failed');
}
下面是我的代码。我收到一个 29 次方的数字,即小数点后 18 位,我想将其转换为类似于 wei 和 eth 的“非小数”数字:
const post_response_2 = await fetch('/balanceOf-TKN-by-addr', {method: 'POST', body: stringify_post_input, headers: { "content-type": "application/json" } });
var TKN_balance_response = await post_response_2.json();
console.log(TKN_balance_response);
var TKN_BN = await new BN(TKN_balance_response).toString(); //line giving error
var TKN_balance = await web3.utils.fromWei(TKN_BN, 'ether');
console.log("TKN_balance :: "+String(TKN_balance));
在我的服务器端,我得到了988699999999999976500000000000 的响应,在上面的客户端console.log 中,我看到了9.886999999999999e+29。
BN.js 我做错了什么?
编辑
由于有 18 位小数,我希望我的 console.log 的答案是 988699999999.999976500000000000,然后我可以四舍五入,等等。
【问题讨论】: