Render JavaScript number as solidity ERC20 decimals

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?

Answers:

Answer

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))

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.