-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculate.java
More file actions
36 lines (31 loc) · 878 Bytes
/
Calculate.java
File metadata and controls
36 lines (31 loc) · 878 Bytes
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
public class Calculate {
public static int roundToInt(double number){
int periodIndex = 0;
String unroundedNumber = "" + number;
String roundedNumber = "";
for (int counter = 0; counter < unroundedNumber.length(); counter++){
if (unroundedNumber.charAt(counter) == '.'){
periodIndex = counter;
break;
}
}
for (int counter = 0; counter < periodIndex; counter++){
if (counter == (periodIndex - 1)){
if (Integer.parseInt("" + unroundedNumber.charAt(counter+2)) >= 5){
int tempNumber =
Integer.parseInt("" + unroundedNumber.charAt(counter));
tempNumber++;
roundedNumber += tempNumber;
}
else {
roundedNumber += unroundedNumber.charAt(counter);
}
}
else {
roundedNumber += unroundedNumber.charAt(counter);
}
}
int answer = Integer.parseInt(roundedNumber);
return answer;
}
}