-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
43 lines (37 loc) · 1.21 KB
/
Copy pathController.java
File metadata and controls
43 lines (37 loc) · 1.21 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
public class Controller
{
public String decimalToBinary(double num, int k_precis){
String binary ="";
int integr= (int) num;
double fract = num - integr;
while(integr > 0){
int rem = integr % 2;
binary += ((char)(rem+'0'));
integr /=2;
}
binary = reverse(binary);
binary +=('.');
while (k_precis-- > 0){
fract *=2;
int fract_b = (int) fract;
if(fract_b == 1){
fract -= fract_b;
binary += (char)(1+'0');
}else{
binary += (char)(0+'0');
}
}
return binary;
}
public String reverse(String input){
char[] temArray = input.toCharArray();
int lef, righ =0;
righ= temArray.length - 1;
for(lef = 0; lef < righ;lef++,righ--){
char tem = temArray[lef];
temArray[lef] = temArray[righ];
temArray[righ] = tem;
}
return String.valueOf(temArray);
}
}