-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACSL2_STRING15_SM.java
76 lines (69 loc) · 2.09 KB
/
ACSL2_STRING15_SM.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.Float;
/*Seneca Meeks
* ACSL2
*/
public class ACSL2_STRING15_SM {
public static StringBuilder rounding(StringBuilder s, float val, int dec, int req_length) //Rounding method
{
double div = (Math.pow(10, (double) dec));
float round = Math.round(val * (Math.pow(10, (double) dec)));
double fin_round = (round)/(div);
String fin_num = Double.toString(fin_round);
s.append(fin_num);
viable(s, req_length, dec);
return s;
}
public static StringBuilder viable(StringBuilder a, int req_length, int dec)
{
char[] space = new char[req_length];
Arrays.fill(space, '#');
String r = a.toString();
char[] nums = r.toCharArray();
if((req_length) == a.length()){ //if no padding is required
System.out.println(a);
return a;
}
else if((req_length) > a.length()){ //if left padding is required
String output = new String(space);
String output1 = new String(a);
String final_output = output.substring(output1.length()) + output1;
StringBuilder b = new StringBuilder("");
b.append(final_output);
System.out.println(b);
return b;
}
else{ //if new format is required
space[(req_length)-(dec)-1] = '.';
StringBuilder c = new StringBuilder("");
String output = new String(space);
c.append(output);
System.out.println(c);
return c;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
while(scan.hasNext()) {
//Declarations
String[] input = scan.nextLine().split(", "); //stores input
String num = input[0];
int req_length = Integer.parseInt(input[1]);
int dec_num = Integer.parseInt(input[2]);
StringBuilder sign = new StringBuilder("");
if(Character.isDigit(num.charAt(0))){
Float val = Float.parseFloat(num);
rounding(sign, val, dec_num, req_length);
}
else{
sign = sign.append(num.charAt(0));
String num1 = num.substring(1);
Float val = Float.parseFloat(num1);
rounding(sign, val, dec_num, req_length);
}
}
}
}