博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 报表的计算公式
阅读量:7115 次
发布时间:2019-06-28

本文共 4823 字,大约阅读时间需要 16 分钟。

hot3.png

package com.util;

import java.text.DecimalFormat; 

import java.text.NumberFormat; 
import java.text.SimpleDateFormat; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern;
public class ReportUtil {
    /**
     * 格式化日期
     * obj 日期对象
     * format 格式化字符串
     *
     */ 
    public static String formatDate(Object obj, String format) {
        if (obj == null) 
            return ""; 
        String s = String.valueOf(obj); 
        if (format == null || "".equals(format.trim())) { 
            format = "yyyy-MM-dd"; 
        } 
        try { 
            SimpleDateFormat dateFormat = new SimpleDateFormat(format); 
            s = dateFormat.format(obj); 
        } catch (Exception e) { 
        } 
        return s; 
    } 
    /**
     * 格式化数字
     * obj 数字对象
     * format 格式化字符串
     * @return
     */ 
    public static String formatNumber(Object obj, String format) { 
        if (obj == null) 
            return ""; 

        String s = String.valueOf(obj); 

        if (format == null || "".equals(format.trim())) { 
            format = "#.00"; 
        } 
        try { 
            if (obj instanceof Double || obj instanceof Float) { 
                if (format.contains("%")) { 
                    NumberFormat numberFormat = NumberFormat.getPercentInstance(); 
                    s = numberFormat.format(obj); 
                } else { 
                    DecimalFormat decimalFormat = new DecimalFormat(format); 
                    s = decimalFormat.format(obj); 
                } 
            } else { 
                NumberFormat numberFormat = NumberFormat.getInstance(); 
                s = numberFormat.format(obj); 
            } 
        } catch (Exception e) { 
        } 
        return s; 
    } 
    /**
     * 计算字符串四则运算表达式
     * @param string
     * @return
     */
    public static String computeString(String string) { 
        String regexCheck = "[\\(\\)\\d\\+\\-\\*/\\.]*";// 是否是合法的表达式 
        if (!Pattern.matches(regexCheck, string)) 
            return string; 
        Matcher matcher = null; 
        String temp = ""; 
        int index = -1; 
        String regex = "\\([\\d\\.\\+\\-\\*/]+\\)";// 提取括号表达式 
        string = string.replaceAll("\\s", "");// 去除空格 
        try { 
            Pattern pattern = Pattern.compile(regex); 
            // 循环计算所有括号里的表达式 
            while (pattern.matcher(string).find()) { 
                matcher = pattern.matcher(string); 
                while (matcher.find()) { 

                    temp = matcher.group(); 

                   index = string.indexOf(temp); 

                    string = string.substring(0, index) 

                            + computeStirngNoBracket(temp) 

                            + string.substring(index + temp.length()); 

                } 
            } 
            // 最后计算总的表达式结果 
            string = computeStirngNoBracket(string); 
        } catch (NumberFormatException e) { 
            return e.getMessage(); 
        } 
        return string; 
    } 
    /**
     * 计算不包含括号的表达式
     * @param string
     * @return
     */ 
    private static String computeStirngNoBracket(String string) { 
       string = string.replaceAll("(^\\()|(\\)$)", ""); 
        String regexMultiAndDivision = "[\\d\\.]+(\\*|\\/)[\\d\\.]+"; 
        String regexAdditionAndSubtraction = "(^\\-)?[\\d\\.]+(\\+|\\-)[\\d\\.]+"; 
        String temp = ""; 
        int index = -1;  
        // 解析乘除法 
        Pattern pattern = Pattern.compile(regexMultiAndDivision); 
        Matcher matcher = null; 
        while (pattern.matcher(string).find()) { 
            matcher = pattern.matcher(string); 
            if (matcher.find()) { 
                temp = matcher.group(); 
                index = string.indexOf(temp); 
                string = string.substring(0, index) + doMultiAndDivision(temp) 
                        + string.substring(index + temp.length()); 
            } 
        } 
        // 解析加减法
        pattern = Pattern.compile(regexAdditionAndSubtraction); 
        while (pattern.matcher(string).find()) { 
            matcher = pattern.matcher(string); 
            if (matcher.find()) { 
                temp = matcher.group(); 
                index = string.indexOf(temp); 
                if (temp.startsWith("-")) { 

                    string = string.substring(0, index) 

                            + doNegativeOperation(temp) 

                            + string.substring(index + temp.length()); 

                } else { 

                    string = string.substring(0, index) 

                          + doAdditionAndSubtraction(temp) 

                            + string.substring(index + temp.length()); 

                } 

            } 

        } 

        return string; 
    } 
    /**
     * 执行乘除法
     * @param string
     * @return
     */ 
    private static String doMultiAndDivision(String string) { 
        String value = ""; 
        double d1 = 0; 
        double d2 = 0; 
        String[] temp = null; 
        if (string.contains("*")) { 
            temp = string.split("\\*"); 
        } else { 
            temp = string.split("/"); 
        } 
        if (temp.length < 2) 
            return string; 
        d1 = Double.valueOf(temp[0]); 
        d2 = Double.valueOf(temp[1]); 
        if (string.contains("*")) { 
            value = String.valueOf(d1 * d2); 
        } else { 
            if(d2==0){
                value="0";
            }else{
                value = String.valueOf(d1 / d2); 
            }
            
        } 
        return value; 
    } 
    /**
     * 执行加减法
     * @param string
     * @return
     */ 
    private static String doAdditionAndSubtraction(String string){ 
        double d1 = 0; 
        double d2 = 0; 
        String[] temp = null; 
        String value = ""; 
        if (string.contains("+")) { 
            temp = string.split("\\+"); 
        } else { 
            temp = string.split("\\-"); 
        } 
        if (temp.length < 2) 
            return string; 
        d1 = Double.valueOf(temp[0]); 
        d2 = Double.valueOf(temp[1]); 
        if (string.contains("+")) { 
            value = String.valueOf(d1 + d2); 
        } else { 
            value = String.valueOf(d1 - d2); 
        } 
        return value; 
    } 
    /**
     * 执行负数运算
     * @param string
     * @return
     */ 
    private static String doNegativeOperation(String string){ 
        String temp = string.substring(1); 
        if (temp.contains("+")) { 
            temp = temp.replace("+", "-"); 
        } else { 
            temp = temp.replace("-", "+");
        } 
        temp = doAdditionAndSubtraction(temp); 
        if (temp.startsWith("-")) { 
            temp = temp.substring(1); 
        } else { 
            temp = "-" + temp; 
        } 
        return temp; 
    } 
    public static void main(String[] args) { 
        String s = "(1+5+6)*3+1*9+2+3/0"; 
        s = computeString(s); 
        System.out.println((int)Double.parseDouble(s)); 
    } 
}

转载于:https://my.oschina.net/zhougui/blog/758432

你可能感兴趣的文章
小学生信息技术课的有效教学
查看>>
天堂与地狱的区别
查看>>
java io小实例
查看>>
127小时
查看>>
Windows Server 2008 R2 SP1中的具体改进
查看>>
Autoit 自动化安装软件
查看>>
shell 脚本-----循环数组
查看>>
Merge into 详细介绍
查看>>
MySQL Server参数优化 - innodb_file_per_table(独立表空间)
查看>>
ubuntu中文出现乱码
查看>>
Linux系统命令之tcpdump
查看>>
第 八 天 : 复 习 中 ( 一 )
查看>>
UVA 11404 五 Palindromic Subsequence
查看>>
关 于 解 压 缩 的 类 习 题
查看>>
[C]字符串排序之-冒泡法
查看>>
浅析企业门户的价值
查看>>
我的友情链接
查看>>
PHP导出MySQL数据到Excel
查看>>
Javascript的console.log()用法
查看>>
小程序里json字符串转json对象需注意的地方
查看>>