Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 291
Sophie259 Avatar asked Jun 22 '26 16:06

Sophie259


1 Answers

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))
like image 143
BlueWater86 Avatar answered Jun 24 '26 06:06

BlueWater86



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!