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.

M.Sheremain
  • 511
  • 2
  • 6
  • 10

1 Answers1

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

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193