将JavaScript编号呈现为ERC20小数位数 [英] Render JavaScript number as solidity ERC20 decimals

查看:100
本文介绍了将JavaScript编号呈现为ERC20小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您牢固地创建ERC20加密货币时,请使用小数位数对其进行初始化。如果总供应量为10k,小数位数为4,则令牌供应量将显示为100000000(10,000.0000)。



在Solidity中,您只需执行YourNumber * 10 ** 4即可初始化类似10,000.0000的数字,其中YourNumber = 10,000



<我想用JavaScript做一个简单的计算器,在该计算器中,根据用户输入,我们以令牌的小数形式为他们提供输入。



假设小数位数最多为4,用户输入250,000,我们将显示250,000.0000。如果用户输入1,我们将显示1.0000。但是,如果用户输入25.5,我们将向他们显示25.5000不幸的是,此逻辑不适用于JavaScript或我知道的任何其他编程语言

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

潜在的解决方案是什么?

解决方案

我认为您正在寻找的是 Number.prototype.toFixed



  var a = 250000,b = 1,c = 25.5console.log(a.toFixed(4),b.toFixed(4),c.toFixed(4)) 



如果需要逗号分隔符,请国际数字格式



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


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?

解决方案

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

这篇关于将JavaScript编号呈现为ERC20小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆