0

I want to list token on marketplace and price in form 0.0001 or 0.2 etc how can i do that ? solidity just unit. thanks a lot.

3 Answers 3

1

Hi Developer advocate from Chainstack here.

Basically solidity doesn't support float point calculation.

If you are talking about Eth, the basic value of Solidity is Wei. So 0.5 eth is equivalent to 500000000000000000 Wei.

If you are talking about an ERC-20 token, you need to leverage the decimals field. For example. If you set your decimal to be 2 and someone calls transfer("0x123123_add_in_hex",234), he/she is transferring 2.34 tokens.

Hope that helps.

3
  • thanks . i'm using BSC Testnet for transactions and take action of transaction by BNB. So in my contract, i dont's using unit256 for price, instead, using wei ?
    – Ngan Dinh
    Commented Feb 1, 2023 at 8:38
  • Hmm wei is more like a unit and uint256 is a data type. I am not entirely sure what is the use case here but I believe using uint256 is correct in this context. Unless you are referencing the "native token" - eg. ether/BNB - holding by the address, I don't think you need to use Wei/eth.🤔 Commented Feb 1, 2023 at 9:19
  • How wei/eth work can be found here: docs.soliditylang.org/en/v0.8.17/… Commented Feb 1, 2023 at 9:21
1

You can use exponential notation to do this:

uint256 value = 0.001e18;

Just make sure to write e18, since 1 ETH = 10^18 wei.

0

The only unit of value that we have that can be used to store decimal are the wei and the gwei. But what's the wei ? It's the unit used for gas.

How can we get a random number and convert it to wei ?

answer :

there are 1 billion wei in one gwei.
there are 10^9 or 1,000,000,000 gwei in one ether.
therefore 1eth is 10^18 wei.
number_to_store = etherAmount * 10**18;
1
  • thank you guys, finally i handled that problem. I was confusing with handling between contract & frontend :_))
    – Ngan Dinh
    Commented Feb 7, 2023 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.