1

Hi guys, i need help!

Trying to make a func that converts string-presented float with 2 dec to uint * 00

etc. if _s = "970452.22" result must be "97045222"

but i do something wrong (when i try to execute it tells me "gas limit exceed 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);
    }
}
1

You need to create the _new_s bytes array. Eg something like

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

Then it seems to work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.