0

Its outputing 173512312(1735.12312) but I just want first 4 letters

    contract SimpleAuction {
  AggregatorInterface internal ref;
  // For ETH-USD on ropsten, input the address:
  // 0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507
  address _aggregator = 0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507;
  string toni;
  constructor() public {
    
    ref = AggregatorInterface(_aggregator);
    
  }
    

  function getLatestAnswer() public view returns (int256) {
    return ref.latestAnswer();
  }    


    int256 private Limit;
    
    //bool sell = false;
    //this define input for limit
    int256 yes = 1;
    int256 no = 2;

    
    function getEth(int256 price) public view returns (uint256){
       
    }
    //logic for limit
    function setLimit(int256 newLimit) public {
        Limit = newLimit;
        sellLogic();
     }    
    
    function sellLogic() private {
       
        //if else to se if it will sell during the dump
        int256 price = ref.latestAnswer();
        if (price > Limit){
            sell = true;
        }else {
            sell = false;
        }
        
1
  • Which function are you calling?
    – Ismael
    Commented Mar 18, 2021 at 5:23

1 Answer 1

1

I don't think you want the first four letters (characters). It's fine now, but what would happen if the price dropped to $900 ? Your system would break.

What you probably want is to drop the last 5 numbers. And, preferably rounded up (although rounding probably starts to matter only when the price drops to some very low numbers).

Since the value is an integer, you can just play with numbers. To drop the last 5 numbers from 173512312, you can just divide it by 100000. Since Solidity doesn't understand decimal points in numbers, it will simply drop those and you'll get 1735.

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.