Solidity

string-float 轉換為 uint

  • January 24, 2018

大家好,我需要幫助

試圖製作一個函式,將字元串呈現的浮點數與 2 dec 轉換為 uint * 00

等等 if _s = “970452.22” 結果必須是 “97045222”

但我做錯了(當我嘗試執行時它告訴我“氣體限制超過 3000000 ”)

contract testfunction {

   string public converted;

   function stringFloatToUnsigned(string _s) payable {
       bytes memory _new_s;
       uint k = 0;

       for (uint i = 0; i < bytes(_s).length; i++) {
           if (bytes(_s)[i] == '.') { continue; }

           _new_s[k] = bytes(_s)[i];
           k++;
       }

       converted = string(_new_s);
   }
}

您需要創建_new_s字節數組。例如像

uint stringLength = bytes(_s).length;
bytes memory _new_s = new bytes(stringLength);

然後它似乎對我有用。

引用自:https://ethereum.stackexchange.com/questions/37329