Render JavaScript number as solidity ERC20 decimals

梦想与她 提交于 2019-12-10 20:41:56

问题


When you create an ERC20 cryptocurrency in solidity you initialize it with a number of decimals. If you total supply is 10k and the number of decimals is 4, your token supply will display as 100000000 (10,000.0000).

In Solidity, you simply do YourNumber*10**4 to initialize a number like 10,000.0000 where YourNumber = 10,000

I wanted to do a simple calculator in JavaScript where, based on the user input we give them their input in decimals of a token.

Say the maximum number of decimals is 4 and the user inputs 250,000, we will show them 250,000.0000. If the user inputs 1, we will show them 1.0000. However, if the user inputs 25.5, we will show them 25.5000 Unfortunately, this logic doesn't work in JavaScript or any other programming language I know

let converted = (this.state.conversion)*(10**14);

What are the potential solutions?


回答1:


I think what you are looking for is Number.prototype.toFixed

var a = 250000, b = 1, c = 25.5

console.log(a.toFixed(4), b.toFixed(4), c.toFixed(4))

And if you need the comma seperator, Intl.NumberFormat:

console.log(new Intl.NumberFormat('en-GB', { useGrouping: true, minimumFractionDigits: 4 }).format(250000))


来源:https://stackoverflow.com/questions/54384890/render-javascript-number-as-solidity-erc20-decimals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!