forked from EtherAuthority/Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogEtoLongNumber.PHP
52 lines (36 loc) · 1.44 KB
/
logEtoLongNumber.PHP
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
52
/*
This function creates LogE numbers like 2.5E+15 into normal number string like: 2500000000000000
*/
function logEtoLongNumber($amountInLogE){
$amountInLogE = (string)$amountInLogE;
$noDecimalDigits = "";
if(strrchr($amountInLogE, "E-")){
$splitString = explode("E-",$amountInLogE); //split the string from 'e-'
$noDecimalDigits = str_replace(".", "", $splitString[0]); //remove decimal point
//how far decimals to move
$zeroString = "";
for($i=1; $i < $splitString[1]; $i++){
$zeroString .= "0";
}
return "0.".$zeroString.$noDecimalDigits;
}
else if(strrchr($amountInLogE, "E+")){
$splitString = explode("E+", $amountInLogE); //split the string from 'e+'
$ePower = (int)$splitString[1];
$noDecimalDigits = str_replace(".","",$splitString[0]); //remove decimal point
if($ePower >= count($noDecimalDigits)-1){
$zerosToAdd = ($ePower ) - count($noDecimalDigits);
for($i=0; $i <= $zerosToAdd; $i++){
$noDecimalDigits .= "0";
}
}
else{
//this condition will run if the e+n is less than numbers
$stringFirstHalf = array_slice($noDecimalDigits,0,$ePower+1);
$stringSecondHalf = array_slice($noDecimalDigits,$ePower+1);
return $stringFirstHalf.".".$stringSecondHalf;
}
return $noDecimalDigits;
}
return $amountInLogE; //by default it returns stringify value of original number if its not logarithm number
}