I'm new to solidity but I program in other languages, solidity looks a bit like javascript but recently I noticed a problem with solidity.

it doesn't accept decimals and float numbers, like "0.001", how can I put this value in a variable?

I already tried to do:

(1 * 1/1000) = 0.001

But there's an error I don't know about:

Type rational_const 1 / 1000 is not implicitly convertible to expected type uint256. Try converting to type ufixed8x3 or use an explicit conversion.


Solidity doesn't really support decimal numbers, so you shouldn't try to use them. You should simply use integers.

However, decimal numbers can be used if they are not assigned to. So something like uint a = 1/3 * 6 should work.

Typically, at least for tokens, the used numbers are big enough that no real decimal places are needed. In the case of tokens, there is a different display amount which is shown in user interfaces, but that's not the amount used in the contracts.

  • i need to do something like: bnb/price = returns amount of tokens. for example: 1 bnb / price per token = quantity of tokens.
    – M4theuz
    Sep 8 at 13:51
  • 1 BNB is actually 1e8 jagers (academy.binance.com/en/glossary/jager). That amount is big enough to perform division on. So "1 BNB" is never 1 in contracts, but it's 1e8 Sep 8 at 13:56

Solidity doesnt support decimal numbers thats why each token has their own decimal value as an erc20 or bep20 standard

1 eth for example has 18 decimals So you would write it as 1e18

As per your comment regarding bnb/price = num of tokens If youre calculating that for a swap on pancakeswap then you should call pancakeswap router function getAmountsOut which when supplied with correct paramters should return how many tokens out you get for the bnb you input

If not then lets say you set the price to be 2 bnb Supposing 1 bnb has 8 decimals the formula would be 100000000/200000000 = 1/2 = invalid for solidity But this is where decimals for tokens come in , if your token youre trying to get the number of has a decimal of 9 Then the number of tokens would be 1/2 * 1000000000. That is equal to half a token You can think of decimals as keeping count of cents for the dollar A dollar has two cent digits If you want to get 3 dollars in solidity its wrote as 3 * 10 ** 2 So 300 cents.

Its important to note when youre doing the calculating (Bnb / price)*decimalsOfToken = tokens out This is incorrect because you will get a decimal value before multiplying with decimalsOfToken which is invalid so the solution is to multiply bnb with decimalsOfToken and then dividing by price

  • It is for a sales contract, I need to find out what would be "1 BNB / Price = amount of tokens for the user". Maybe I need to make a rate, but I can't figure out how to make it using a price of 0.001. In my mind I could make 1 BNB / 0.001 = 1000 tokens.
    – M4theuz
    Sep 8 at 14:11
  • I edited my answer hope this helps Sep 8 at 14:13
  • The rate youre talking about should be the decimals of the token you want to calculate the number for based on price. Sep 8 at 14:19

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