程序中想对数组中一元素p[j]进行四舍五入保留小数点后两位的操作,用:
float a = (float)(nodeDegree[j])/sumOfDegree;
BigDecimal b = new BigDecimal((double)a);
p[j] = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();和
p[j] = (float)Math.round(((float)(nodeDegree[j])/sumOfDegree)*100)/100;发现程序跑的非常慢,请教大虾们这是什么原因呢?
float a = (float)(nodeDegree[j])/sumOfDegree;
BigDecimal b = new BigDecimal((double)a);
p[j] = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();和
p[j] = (float)Math.round(((float)(nodeDegree[j])/sumOfDegree)*100)/100;发现程序跑的非常慢,请教大虾们这是什么原因呢?
大家用Math.round也很慢吗?
还是都是自己写的哦
用了NumberFormat:(程序定义p[j]为float)p[j]=(float)(nodeDegree[j])/sumOfDegree*1f;NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
p[j]=Float.parseFloat(nf.format((double)p[j])); 不做小数点处理很快,加上处理后就非常慢