1 package Util;
2
3 import java.io.File;
4 import java.math.BigDecimal;
5
6 public class NumberUtil {
7
8 /**
9 * 根据精度截断浮点数
10 * @param value 需要截断的浮点数
11 * @param scale 精度
12 * @return
13 */
14 public static double round(double value,int scale){
15 BigDecimal db = new BigDecimal(value);
16 db = db.setScale(2,BigDecimal.ROUND_HALF_UP);
17 return db.doubleValue();
18 }
19
20 public static void main(String[] args){
21 File f = new File("D:/study/java/graduate/WebContent/swf/IT_快消_投行_咨询等行业的简历模版.rar");
22 if(f.exists()){
23 //获取文件大小(单位字节),1MB = 1048576字节
24 System.out.println(f.length());
25 if(f.length() < 1048576){
26 //如果小于1MB,则显示字节大小
27 System.out.println(f.length()/1024 + "字节");
28 }else{
29 //如果大于1MB,则显示MB大小,保留小数点后2位
30 System.out.println(NumberUtil.round(f.length()/1048576.0, 2) + "MB");
31 }
32 }
33 }
34 }