-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryConvertStringToUint.sol
More file actions
51 lines (43 loc) · 1.31 KB
/
LibraryConvertStringToUint.sol
File metadata and controls
51 lines (43 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
library ConvertStringToUint {
function stringToUint(string _amount) internal constant returns (uint result) {
bytes memory b = bytes(_amount);
uint i;
uint counterBeforeDot;
uint counterAfterDot;
result = 0;
uint totNum = b.length;
totNum--;
bool hasDot = false;
for (i = 0; i < b.length; i++) {
uint c = uint(b[i]);
if (c >= 48 && c <= 57) {
result = result * 10 + (c - 48);
counterBeforeDot ++;
totNum--;
}
if(c == 46){
hasDot = true;
break;
}
}
if(hasDot) {
for (uint j = counterBeforeDot + 1; j < 18; j++) {
uint m = uint(b[j]);
if (m >= 48 && m <= 57) {
result = result * 10 + (m - 48);
counterAfterDot ++;
totNum--;
}
if(totNum == 0){
break;
}
}
}
if(counterAfterDot < 18){
uint addNum = 18 - counterAfterDot;
uint multuply = 10 ** addNum;
return result = result * multuply;
}
return result;
}
}