packagecom.dwtedx.income.utility;importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/**
* Created by dwtedx(qinyl)(http://dwtedx.com) on 16/9/2.
* Company DD博客
* Description java字符串加法运算
*/publicclassReportUtil{/**
* 格式化数字
* @param obj 数字对象
* @param format 格式化字符串
* @return
*/publicstaticStringformatNumber(Objectobj,Stringformat){if(obj==null)return"";Strings=String.valueOf(obj);if(format==null||"".equals(format.trim())){format="#.00";}try{if(objinstanceofDouble||objinstanceofFloat){if(format.contains("%")){NumberFormatnumberFormat=NumberFormat.getPercentInstance();s=numberFormat.format(obj);}else{DecimalFormatdecimalFormat=newDecimalFormat(format);s=decimalFormat.format(obj);}}else{NumberFormatnumberFormat=NumberFormat.getInstance();s=numberFormat.format(obj);}}catch(Exceptione){}returns;}/**
* 计算字符串四则运算表达式
* @param string
* @return
*/publicstaticStringcomputeString(Stringstring){StringregexCheck="[\(\)\d\ \-\*/\.]*";// 是否是合法的表达式if(!Pattern.matches(regexCheck,string))returnstring;Matchermatcher=null;Stringtemp="";intindex=-1;Stringregex="\([\d\.\ \-\*/] \)";// 提取括号表达式string=string.replaceAll("\s","");// 去除空格try{Patternpattern=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(NumberFormatExceptione){returne.getMessage();}returnstring;}/**
* 计算不包含括号的表达式
* @param string
* @return
*/privatestaticStringcomputeStirngNoBracket(Stringstring){string=string.replaceAll("(^\()|(\)$)","");StringregexMultiAndDivision="[\d\.] (\*|\/)[\d\.] ";StringregexAdditionAndSubtraction="(^\-)?[\d\.] (\ |\-)[\d\.] ";Stringtemp="";intindex=-1;// 解析乘除法Patternpattern=Pattern.compile(regexMultiAndDivision);Matchermatcher=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());}}}returnstring;}/**
* 执行乘除法
* @param string
* @return
*/privatestaticStringdoMultiAndDivision(Stringstring){Stringvalue="";doubled1=0;doubled2=0;String[]temp=null;if(string.contains("*")){temp=string.split("\*");}else{temp=string.split("/");}if(temp.length<2)returnstring;d1=Double.valueOf(temp[0]);d2=Double.valueOf(temp[1]);if(string.contains("*")){value=String.valueOf(d1*d2);}else{value=String.valueOf(d1/d2);}returnvalue;}/**
* 执行加减法
* @param string
* @return
*/privatestaticStringdoAdditionAndSubtraction(Stringstring){doubled1=0;doubled2=0;String[]temp=null;Stringvalue="";if(string.contains(" ")){temp=string.split("\ ");}else{temp=string.split("\-");}if(temp.length<2)returnstring;d1=Double.valueOf(temp[0]);d2=Double.valueOf(temp[1]);if(string.contains(" ")){value=String.valueOf(d1 d2);}else{value=String.valueOf(d1-d2);}returnvalue;}/**
* 执行负数运算
* @param string
* @return
*/privatestaticStringdoNegativeOperation(Stringstring){Stringtemp=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;}returntemp;}}