MathUtil.java
5.67 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.laowu.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
public class MathUtil {
/**
* 4位自增字符
*/
public static String getRandomCode(int randomLength) {
StringBuilder str = new StringBuilder();
Random random = new Random();
for (int i = 0; i < randomLength; i++) {
str.append(random.nextInt(10));
}
return str.toString();
}
public static String getRandomString(int length) {
StringBuilder val = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; ++i) {
String charOrNum = random.nextInt(3) % 3 == 0 ? "num" : "char";
if ("char".equalsIgnoreCase(charOrNum)) {
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val.append((char) (random.nextInt(26) + temp));
} else {
val.append(random.nextInt(10));
}
}
return val.toString();
}
//习题/视频 ==> 秒钟向上取整
public static String ceilSecond(int length) {
return String.valueOf((int) Math.ceil((double) length / 60));
}
//习题/视频 ==> 秒钟向下取整
public static String floorSecond(int length) {
return String.valueOf((int) Math.ceil((double) length / 60));
}
//视频秒数处理
public static String vodLength(int i) {
if (i > 60) {
if (i % 60 >= 10) {
return i / 60 + ":" + i % 60;
} else {
return i / 60 + ":0" + i % 60;
}
} else {
if (i % 60 >= 10) {
return "00:" + i;
} else {
return "00:0" + i;
}
}
}
//文件大小处理
public static String fileUnit(int i) {
if (i / 1024 > 1024) {
return String.format("%.2f", (double) i / 1024 / 1024) + "M";
} else {
return String.format("%.2f", (double) i / 1024) + "K";
}
}
//单个题目秒数处理
public static String exerciseLength(int i) {
if (i >= 60 * 60) {
return i / 60 / 60 + "时" + (i - 60 * 60) / 60 + "分" + i % 60 + "秒";
} else if (i >= 60) {
return i / 60 + "分" + i % 60 + "秒";
} else {
return i % 60 + "秒";
}
}
// 除法取整
public static String getAvgIndex(BigDecimal counts, BigDecimal total) {
if (BigDecimal.ZERO.equals(total)) {
return "0";
} else {
return counts.divide(total, RoundingMode.CEILING).stripTrailingZeros().toPlainString();
}
}
//百分比 取整 返回字符串
public static String getPercentAvgIndex(BigDecimal counts, BigDecimal total) {
if (BigDecimal.ZERO.equals(total) || BigDecimal.ZERO.equals(counts)) {
return "0%";
} else {
return counts.multiply(new BigDecimal(100)).divide(total, RoundingMode.CEILING).stripTrailingZeros().toPlainString() + "%";
}
}
//百分比 不带%输出
public static String getPercentAvgIndexWithPercent(BigDecimal counts, BigDecimal total) {
if (BigDecimal.ZERO.equals(total) || BigDecimal.ZERO.equals(counts)) {
return "0";
} else {
return counts.multiply(new BigDecimal(100)).divide(total, 2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString();
}
}
//百分比 不带%输出
public static String getPercentAvgIndexCeil(BigDecimal counts, BigDecimal total) {
if (BigDecimal.ZERO.equals(total) || BigDecimal.ZERO.equals(counts)) {
return "0";
} else {
return counts.multiply(new BigDecimal(100)).divide(total, 2, BigDecimal.ROUND_CEILING).stripTrailingZeros().toPlainString();
}
}
//习题序号生成规则 sid 从数据取出某科目某知识点的最大的序号
public static String getExeCode(String sid) {
Integer integer = Integer.valueOf(sid.substring(9));
return sid.substring(0, 9) + (++integer);
}
// 整型a 整型b a/b 向上取整
public static Integer intDivCeil(long a, int b) {
return (int) Math.ceil((double) a / b);
}
// 整型a 整型b a/b 向下取整
public static Integer intDivFloorPercent(long a, int b) {
return (int) Math.floor((double) a * 100 / b);
}
public static Integer getRandomNum(Integer i) {
Random random = new Random();
return random.nextInt(i);
}
// public static void main(String[] args) {
// for (int i= 1;i<100;i++){
// System.out.println(getRandomNum(2));
// }
// }
public static String secToTime(int time) {
String timeStr ;
int hour;
int minute;
int second;
if (time <= 0) {
return "00:00:00";
} else {
minute = time / 60;
if (minute < 60) {
second = time % 60;
timeStr = "00:"+unitFormat(minute) + ":" + unitFormat(second);
} else {
hour = minute / 60;
minute = minute % 60;
second = time - hour * 3600 - minute * 60;
timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
}
}
return timeStr;
}
public static String unitFormat(int i) {
String retStr = null;
if (i >= 0 && i < 10) {
retStr = "0" + i;
} else {
retStr = "" + i;
}
return retStr;
}
public static String round(Integer a,Integer b){
return Math.round((double) a*100 / (double) b)+"";
}
}