Created
June 28, 2016 13:21
-
-
Save glynnbird/7e6b4116bbf1886ce33d1f0d7606bf02 to your computer and use it in GitHub Desktop.
parseInt (solidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2015-2016 Oraclize srl, Thomas Bertani | |
function parseInt(string _a, uint _b) internal returns (uint) { | |
bytes memory bresult = bytes(_a); | |
uint mint = 0; | |
bool decimals = false; | |
for (uint i = 0; i < bresult.length; i++) { | |
if ((bresult[i] >= 48) && (bresult[i] <= 57)) { | |
if (decimals) { | |
if (_b == 0) break; | |
else _b--; | |
} | |
mint *= 10; | |
mint += uint(bresult[i]) - 48; | |
} else if (bresult[i] == 46) decimals = true; | |
} | |
return mint; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment