Skip to content

Instantly share code, notes, and snippets.

@glynnbird
Created June 28, 2016 13:21
Show Gist options
  • Save glynnbird/7e6b4116bbf1886ce33d1f0d7606bf02 to your computer and use it in GitHub Desktop.
Save glynnbird/7e6b4116bbf1886ce33d1f0d7606bf02 to your computer and use it in GitHub Desktop.
parseInt (solidity
// 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