File DianDiYunVideo = new File(Environment.getExternalStorageDirectory(),"文件名字");
try {
double folderSize = getFolderSize(DianDiYunVideo);
java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
offline_value.setText(df.format(folderSize)+"MB");
System.out.println("这里保留小数点两位:"+df.format(folderSize));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static double getFolderSize(java.io.File file)throws Exception{
double size = 0;
java.io.File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()){
size = size + getFolderSize(fileList[i]);
} else{
System.out.println("文件的名字是:"+fileList[i].getName());
size = size + fileList[i].length();
}
}
return size/1048576;
}
一
Long是长整型,怎么有小数,是double吧
java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
double d=3.14159;
System.out.println(df.format(d));
二
java.math.BigDecimal
BigDecimal bd = new BigDecimal("3.14159265");
bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
三
class Test1{
public static void main(String[] args){
double ret = convert(3.14159);
System.out.println(ret);
}
static double convert(double value){
long l1 = Math.round(value*100); //四舍五入
double ret = l1/100.0; //注意:使用 100.0 而不是 100
return ret;
}
}
四
double d = 13.4324;
d=((int)(d*100))/100;
你可以通过这个链接引用该篇文章:http://jxcn.bokee.com/tb.b?diaryId=14529336