• 【Java自定义工具类】百分比计算工具类以及计算相关的问题(138)


    详解:
    1.列举了4种百分比转化方式;
    2.解决百分比计算加和不为100%的问题;
    3.百分比计算保留小数点后一位或者两位或者N位;
    4.double类型数字相加减的时候结果与预期不符合;
    5.不同入参方式计算(数组和单个值);

    详解4:举例说明
    eg:131+121=251.00000000000000001(且大概率难以重现,有时候有这种情况,有时候又不会发生)
    原因:在数值相加减时,会先被转化成机器数然后再运算,即二进制的形式。但是在转换成二进制代码表示的时候,存储小数部分的位数会有不够的现象,即无限循环小数,这就是造成微小差距的主要原因。

    工具类代码:

    package com.day02;
    
    import java.text.DecimalFormat;
    
    /*
     * 百分比计算工具类
     * 
     */
    
    public class PercentageUtil {
    	public static void main(String[] args) {
    		
    		// 测试方式一
    		System.out.println("测试方式一:"+calculationMethod_1(14,30));	
    		System.out.println("----------------");
    		// 测试方式二
    		System.out.println("测试方式二:"+calculationMethod_2(123.446));
    		System.out.println("----------------");
    		// 测试方式三
    		double[] nums = {33.0, 35.0, 18.0, 12.0,21.0}; // 要计算的数
    		String[] result = calculationMethod_3(nums);	
    		for (int i = 0; i < result.length; i++) {
    			System.out.println("测试方式三:"+result[i]);
    		}
    		System.out.println("----------------");
    		// 测试方式四
    		String d = calculationMethod_4(50,1000);
    		System.out.println("测试方式四:"+d);
    		
    	}
    	
    	/*
    	 * 计算方式一:
    	 * 
    	 */
    	public static String calculationMethod_1(double value,double total) {
    		double percentage = (value / total) * 100;  // 格式化百分比
    		// 创建DecimalFormat对象,指定保留一位小数,如果想保留两位,改成new DecimalFormat("0.00")
    		DecimalFormat decimalFormat = new DecimalFormat("0.0");  
    		String Percentage = decimalFormat.format(percentage) + "%";  // 格式化百分比
    		return Percentage;
    	}
    	
    	/*
    	 * 计算方式二:
    	 * 
    	 */
    	public static String calculationMethod_2(double number) {
    		double roundedNumber = (double) Math.round(number);
            DecimalFormat df = new DecimalFormat("#.##");  // 保留两位("#.##");保留一位("#.#");
            //DecimalFormat 的 setDecimalSeparatorAlwaysShown() 方法默认是 true,即小数点后的零将始终显示出来。要避免输出后缀 0,设置该属性为 false
            df.setDecimalSeparatorAlwaysShown(false);
            String formattedNumber = df.format(number);
    		return formattedNumber;
    	}
    	
    	/*
    	 * 计算方式三:各自所占比例相加为100%;
    	 * 
    	 */
    	public static String[] calculationMethod_3(double nums[]) {
    		String[] result = new String[nums.length];
    		double total = 0.0; // 总数
    		// 计算总数
    		for (double num : nums) {
    		   total += num;
    		}
    		// 计算各自所占比例和相应的百分比
    		for (int i = 0; i < nums.length; i++) {
    		    double ratio = (nums[i] / total) * 100;
    		    // 创建DecimalFormat对象,指定保留一位小数(保留两位:0.00)
    		    DecimalFormat decimalFormat = new DecimalFormat("0.0");  
    			String Percentage = decimalFormat.format(ratio);  // 格式化百分比
    			// 最后一位数据采用减法,这样总和为100%;
    			if(i == nums.length - 1) {
    				Double temp = 0.0;
    				for (int j = 0; j < result.length-1; j++) {
    					temp += Double.valueOf(result[j]);
    				}
    				double value = 100 - temp;
    				// 格式化百分比:解决double类型数字相加减的时候结果与预期不符合
    				String Percentage2 = decimalFormat.format(value);
    				result[i] = String.valueOf(Percentage2);
    			}
    			else {
    				result[i] = Percentage;
    			}
    		}
    		for (int i = 0; i < result.length; i++) {
    			result[i] = result[i] + "%";
    		}
    		return result;
    	}
    	
    	/*
    	 * 计算方式四:
    	 * 备注:保留小数点后一位或者两位调整:new DecimalFormat("##.00%"); // ##.00%
    	 * 
    	 */
    	public static String calculationMethod_4(long a, long b) {
            String result = "";
            double y = a * 1.0;
            double z = b * 1.0;
            if (y == 0 || z == 0) {
                return "0.00%";  // 小数点后保留两位;
            }
            double res = y / z;
            DecimalFormat dec = new DecimalFormat("##.00%"); // ##.00%   小数点后保留两位;
    
            result = dec.format(res);
            // ##.00% 使用这种转换方式,整数位如果是0 则会被删除  即0.35% 会出现 .35%的情况
            char c = result.charAt(0);
            if (String.valueOf(c).equals(".")) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(result);
                return String.valueOf(sb);
            }
            return result;
        }
    	
    }
    
    
    • 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

    测试输出:

    测试方式一:46.7%
    ----------------
    测试方式二:123.45
    ----------------
    测试方式三:27.7%
    测试方式三:29.4%
    测试方式三:15.1%
    测试方式三:10.1%
    测试方式三:17.7%
    ----------------
    测试方式四:5.00%
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    关联规则算法Apriori algorithm详解以及为什么它不适用于所有的推荐系统
    股票分时成交明细接口的数据怎么看?
    考研数据结构与算法(五)数组
    02数据结构与算法刷题之【哈希表】篇
    Hadoop简介
    接口自动化框架搭建(九):接入钉钉消息通知
    C++之类和对象(下)
    代码随想录 - Day37 - 贪心算法
    vue 如果2个/多个过滤器(filter)同时使用并传参数?
    中职计算机应用专业(云计算方向)建设实践
  • 原文地址:https://blog.csdn.net/qq_42139049/article/details/133771495