妙书屋 > 我的1979 > 15、骂战

15、骂战


请稍后阅读,章节错误

        packagecom.z

        002

        003

        importjava.text.DecimalFormat;

        004

        importjava.text.NumberFormat;

        005

        importjava.text.SimpleDateFormat;

        006

        importjava.util.regex.Matcher;

        007

        importjava.util.regex.Pattern;

        008

        009

        publicclassReportUtil{

        010

        011

        /**

        012

        *格式化日期

        013

        *@paramobj日期对象

        014

        *@paramformat格式化字符串

        015

        *@return

        016

        */

        017

        publicstaticStringformatDate(Objectobj,Stringformat){

        018

        if(obj==null)

        019

        return““;

        020

        021

        Strings=String.valueOf(obj);

        022

        if(format==null||““.equals(format.trim())){

        023

        format=“yyyy-MM-dd“;

        024

        }

        025

        try{

        026

        SimpleDateFormatdateFormat=newSimpleDateFormat(format);

        027

        s=dateFormat.format(obj);

        028

        }catch(Exceptione){

        029

        }

        030

        returns;

        031

        }

        032

        033

        /**

        034

        *格式化数字

        035

        *@paramobj数字对象

        036

        *@paramformat格式化字符串

        037

        *@return

        038

        */

        039

        publicstaticStringformatNumber(Objectobj,Stringformat){

        040

        if(obj==null)

        041

        return““;

        042

        043

        Strings=String.valueOf(obj);

        044

        if(format==null||““.equals(format.trim())){

        045

        format=“#.00“;

        046

        }

        047

        try{

        048

        if(objinstanceofDouble||objinstanceofFloat){

        049

        if(format.contains(“%“)){

        050

        NumberFormatnumberFormat=NumberFormat.getPercentInstance();

        051

        s=numberFormat.format(obj);

        052

        }else{

        053

        DecimalFormatdecimalFormat=newDecimalFormat(format);

        054

        s=decimalFormat.format(obj);

        055

        }

        056

        }else{

        057

        NumberFormatnumberFormat=NumberFormat.getInstance();

        058

        s=numberFormat.format(obj);

        059

        }

        060

        }catch(Exceptione){

        061

        }

        062

        returns;

        063

        }

        064

        065

        /**

        066

        *计算字符串四则运算表达式

        067

        *@paramstring

        068

        *@return

        069

        */

        070

        publicstaticStringcomputeString(Stringstring){

        071

        StringregexCheck=“[\\(\\)\\d\\+\\-\\*/\\.]*“;//是否是合法的表达式

        072

        073

        if(!Pattern.matches(regexCheck,string))

        074

        returnstring;

        075

        076

        Matchermatcher=null;

        077

        Stringtemp=““;

        078

        intindex=-1;

        079

        Stringregex=“\\([\\d\\.\\+\\-\\*/]+\\)“;//提取括号表达式

        080

        string=string.replaceAll(“\\s“,““);//去除空格

        081

        try{

        082

        Patternpattern=Pattern.compile(regex);

        083

        //循环计算所有括号里的表达式

        084

        while(pattern.matcher(string).find()){

        085

        matcher=pattern.matcher(string);

        086

        while(matcher.find()){

        087

        temp=matcher.group();

        088

        index=string.indexOf(temp);

        089

        string=string.substring(0,index)

        090

        +computeStirngNoBracket(temp)

        091

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

        092

        }

        093

        }

        094

        //最后计算总的表达式结果

        095

        string=computeStirngNoBracket(string);

        096

        }catch(NumberFormatExceptione){

        097

        returne.getMessage();

        (未完待续。)


  https://www.msvvu.cc/1/1596/729938.html


请记住本书首发域名:www.msvvu.cc。妙书屋手机版阅读网址:m.msvvu.cc