Java关于数字工具类持续汇总

1描述:求int数组中最大值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 01
* 描述:求int数组中最大值
* 【时间 2019年3月5日下午3:21:36 作者 陶攀峰】
*/
public static int test01(int[]sz) {
int max = sz[0];
for(int x=1; x<sz.length; x++)
{
if(sz[x]>max){
max = sz[x];
}
}
return max;
}
2描述:数组排序(从小到大)~传入int数组 .返回int数组.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 02
* 描述:数组排序(从小到大)~传入int数组 .返回int数组.
* 【时间 2019年3月5日下午3:24:11 作者 陶攀峰】
*/
public static int[] test02(int[]sz) {
for(int x=0; x<sz.length-1; x++)
{
for(int y=x+1; y<sz.length; y++)
{
if(sz[x]>sz[y])
{
int temp = sz[x];
sz[x] = sz[y];
sz[y] = temp;
}
}
}
return sz;
}
3描述:冒泡排序(从小到大)~传入int数组 .返回int数组.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 03
* 描述:冒泡排序(从小到大)~传入int数组 .返回int数组.
* 【时间 2019年3月5日下午3:25:23 作者 陶攀峰】
*/
public static int[] test03(int[]sz) {
for(int x=0; x<sz.length-1; x++)
{
for(int y=0; y<sz.length-x-1; y++)
{
if(sz[y]>sz[y+1])
{
int temp = sz[y];
sz[y] = sz[y+1];
sz[y+1] = temp;
}
}
}
return sz;
}
4描述:两数相除 返回值:百分比 [0-100].[0-9][0-9]%
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
/**
* 04
* 描述:两数相除 返回值:百分比 [0-100].[0-9][0-9]%
* 【时间 2019年3月5日下午3:51:14 作者 陶攀峰】
*/
public static String test04(double a,double b){
if (a==0||b==0) {
return "0.00%";
}else {
//定义返回值:百分比 [0-100].[0-9][0-9]%
String bfb="";
BigDecimal aBD=new BigDecimal(a+"");
BigDecimal bBD=new BigDecimal(b+"");
// filter=a.bcde a/b 保留四位小数 并且四舍五入
String filter=new BigDecimal(a+"").divide(new BigDecimal(b+""), 4, RoundingMode.HALF_UP)+"";
if(!filter.substring(0, 1).equals("0")){//如果a!=0
bfb=new BigDecimal(filter).multiply(new BigDecimal("100"))
.toString().substring(0, 6)+"%";
}else{
if (!filter.substring(2, 3).equals("0")) {//如果a=0 b!=0
bfb=new BigDecimal(filter).multiply(new BigDecimal("100"))
.toString().substring(0, 5)+"%";
}else{
if (!filter.substring(3, 4).equals("0")) {//如果a,b=0 c!=0
bfb=new BigDecimal(filter).multiply(new BigDecimal("100"))
.toString().substring(0, 4)+"%";
}else{
if (!filter.substring(4, 5).equals("0")) {//如果a,b,c=0 d!=0
bfb=new BigDecimal(filter).multiply(new BigDecimal("100"))
.toString().substring(0, 4)+"%";
}else{
if (!filter.substring(5, 6).equals("0")) {//如果a,b,c,d=0 e!=0
bfb=new BigDecimal(filter).multiply(new BigDecimal("100"))
.toString().substring(0, 4)+"%";
}else {//如果a,b,c,d,e=0
bfb="0.00%";
}
}
}
}
}
return bfb;
}
}
5描述:两数相除得到百分比值例如 test05(2,3) 返回 67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 05
* 描述:两数相除得到百分比值
* 例如 test05(2,3) 67
* 【时间 2019年3月5日下午3:53:34 作者 陶攀峰】
*/
public static int test05(int a ,int b){
//百分比
int bfb=0;
if (a==0||b==0) {
bfb=0;
}else {
bfb=(int)((new BigDecimal((float) a / b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())*100);
}
return bfb;
}
6描述:去除小数点后无用的0 ,如果都是0去除小数点
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 描述:去除小数点后无用的0 ,如果都是0去除小数点
* 【时间 2019年3月21日上午8:34:49 作者 陶攀峰】
*/
public static String deleteNoUseZero(String str) {
if(str.indexOf(".") > 0){
//正则表达
str = str.replaceAll("0+?$", "");//去掉后面无用的零
str = str.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
}
return str;
}
7描述:给数据添加分位符,例如156326849.251 >>> 156,326,849.251
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 描述:给数据添加分位符,例如156326849.251 >>> 156,326,849.251
* 【时间 2019-05-24 09:24:17 作者 陶攀峰】
*/
public static String change_number_format(String number) {
boolean buer=number.contains("-");
number=number.replace("-", "");
String[]sz=number.split("\\.");// . 需要转义
int yu=sz[0].length()%3;
String return_string=sz[0].substring(0, yu);
for (int i = 0; i < sz[0].length()/3; i++) {//应该加几次逗号
return_string=return_string+","+sz[0].substring(i*3+yu, i*3+yu+3);
}
if (sz.length==2) {//如果长度大于2,再加上小数
return_string=return_string+"."+sz[1];
}
if (return_string.substring(0,1).equals(",")) {//如果第一个字符为逗号,去除逗号
return_string=return_string.substring(1,return_string.length());
}
if (buer) {
return_string="-"+return_string;
}
return return_string;
}