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

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

 

posted on 2019-04-18 16:34  陶攀峰  阅读(1188)  评论(0编辑  收藏  举报

顶部 底部