Linked Questions

33
votes
4answers
21k views

How can I represent decimal values in Solidity?

I just started writing some solidity and I noticed that there are no decimal values such as double or float. What do you guys do when you need to return send to a wallet let's say 1.57 ether? Or when ...
20
votes
6answers
5k views

Can I square root in Solidity?

Is it possible to square root 50 or should I use 7.071067. I'm very new in programming and I also understand that it's possible in all other languages, but I'm not able to see where the limitations ...
25
votes
4answers
24k views

How can I perform float type division in solidity?

How can I achieve proper division of two integers in solidity? Suppose I perform : 3/2 the solution provided is 1 and not 1.5 please help.
16
votes
4answers
10k views

Can't do any integer division

I'm building a new smart contract but really can't figure out how to perform integer division. I know that fixed point numbers haven't been implemented yet but it should be possible to perform 100/3 = ...
9
votes
2answers
4k views

Finding and Using Solidity Libraries (ie. Math functions)

How do I find and use any existing Solidity libraries like you might in Javascript? For example, Math functions: How do I implement the equivalent of Math.sqrt(x) in a solidity function? Related: ...
6
votes
1answer
1k views

Can you input decimals number in smart contract?

I'm having an issue where send 0.1 or 1.1 is saved as 0 and 1 in my contract's storage. Does anyone knows how to properly pass decimals ?
1
vote
1answer
889 views

Solidity 'real' type and fixed point regimes

I'm sure I read somewhere that Solidity had a fixed point type 'real' of format 32byte.32byte, though the docs say Fixed Point numbers are still to come. Until it's implemented, is there any best ...
3
votes
2answers
234 views

price conversion using chainlink ETHUSD price

I'm storing a product's USD value in contract. mapping (string => uint256) productUSD; For example, if it's $52 I'd store it as productUSD[_product] = 52; Now, when people paying in ETH, I want to ...
1
vote
1answer
364 views

Why does division still truncate even though readthedocs.io says it does not?

According to readthedocs.io: "Division on integer literals used to truncate in earlier versions, but it will now convert into a rational number, i.e. 5 / 2 is not equal to 2, but to 2.5". Yet, ...
0
votes
1answer
34 views

I want to store decimal value in a storage variable and later use that variable at same precision in the formula later internally

I want to store uint public MINTING_VELOCITY = 0.000002; And later use it in the formula: A x t x MINTING_VELOCITY ,where A = 2000 ,t(staking_duration_time) = 120 Issue is currently MINTING_VELOCITY ...
2
votes
1answer
56 views

Bundling two assets in a contract and issuing shares for them

Say you are tasked with coding a smart contract with the following features: Users can deposit token A and receive liquidity shares in return. The contract starts selling token A for token B ...
0
votes
2answers
21 views

Decimal in Solidity [duplicate]

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