5

For example. I can make a static price.

 function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner {
    sellPrice = newSellPrice;
    buyPrice = newBuyPrice;
}

But how can make a floating price.

1 Answer 1

7

The Ethereum VM does not support floating point operations currently.

What you can do is to use a divisor in your calculations.

Eg:

uint256 divisor = 1000000;
uint256 sellPrice = 1234; // To represent 0.001234
uint256 buyPrice = 1123;  // To represent 0.001123
uint256 amount = 1 ether;
uint256 sellAmount = amount * sellPrice / divisor;

You can see this technique being used in buy() function of GNTTokenTrader to represent the "floating point" prices in https://cryptoderivatives.market/.

Here is a Browser Solidity screen demonstrating the calculations: enter image description here

1
  • Just updated with a Browser Solidity screenshot demonstrating the calculations. Jan 29, 2017 at 8:14

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.