java小技巧
int型除法保留两位小数/求百分比
int openCount = temp.getIsOpenCount(); int passCount = temp.getIsPassCount(); double k = (double)passCount/openCount*100; java.math.BigDecimal big = new java.math.BigDecimal(k); String l =big.setScale(2,java.math.BigDecimal.ROUND_HALF_UP).doubleValue() +"%";
float保留两位小数
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。//1 float f = 34.232323; BigDecimal b = new BigDecimal(f); float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue(); // b.setScale(2, BigDecimal.ROUND_HALF_UP) 表明四舍五入,保留两位小数 //2 float scale = 34.236323; DecimalFormat fnum = new DecimalFormat("##0.00"); String dd=fnum.format(scale); System.out.println(dd); //3 float a = 123.2334f; float b = (float)(Math.round(a*100))/100;
BigDecimal保留两位小数
//金额保留小数后两位 BigDecimal b = invoiceTemp.getInvoicePrice(); BigDecimal setScale = b.setScale(2,BigDecimal.ROUND_DOWN); invoiceTemp.setInvoicePrice(setScale);
参数定义
ROUND_CEILING 向正无穷方向舍入
ROUND_DOWN 向零方向舍入
ROUND_FLOOR 向负无穷方向舍入
ROUND_HALF_DOWN 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5
ROUND_HALF_EVEN 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP ,如果是偶数,使用ROUND_HALF_DOWN
ROUND_HALF_UP 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位小数结果为1.6
ROUND_UNNECESSARY 计算结果是精确的,不需要舍入模式
ROUND_UP 向远离0的方向舍入
Action文件mapping.findForward()动态传参至Struts-config.xml
//后台方法 return mapping.findForward(outSystemSign); //配置文件无需判断,直接接收,直接写明映射内容即可 <forward name="qgtzsbcjh" path="/WEB-INF/jsp/outsystem/kpflowchart/qgtzsbcjh.jsp"/> <forward name="gusuwx" path="/WEB-INF/jsp/outsystem/kpflowchart/gusuwx.jsp"/>
isBlank、isEmpty、isNull
org.apache.commons.lang.StringUtils类提供了String的常用操作,最常用判空如下
- StringUtils.isEmpty(String str)
//判断某字符串是否为空,标准是 str==null 或 str.length()==0 System.out.println(StringUtils.isEmpty(null)); //true System.out.println(StringUtils.isEmpty("")); //true System.out.println(StringUtils.isEmpty(" ")); //false System.out.println(StringUtils.isEmpty("dd")); //false
- StringUtils.isNotEmpty(String str) 等价于 !isEmpty(String str),判断是否不为空也不为null,在要替换字段为空串时,应使用isNotNull,isNotEmpty会自动跳过
- StringUtils.isBlank(String str),StringUtils.isNotBlank(String str) 等价于 !isBlank(String str)
//判断某字符串是否为空或长度为0或由空白符(whitespace) 构成System.out.println(StringUtils.isBlank(null)); //true System.out.println(StringUtils.isBlank("")); //true System.out.println(StringUtils.isBlank(" ")); //true System.out.println(StringUtils.isBlank("dd")); //false

更多精彩