1、数据大小格式化-工具类

  1. public class DataSizeUtil {
  2. public static String formatFileSize(long size) {
  3. String hrSize = null;
  4. double b = size;
  5. double k = size/1024.0;
  6. double m = ((size/1024.0)/1024.0);
  7. double g = (((size/1024.0)/1024.0)/1024.0);
  8. double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0);
  9. DecimalFormat dec = new DecimalFormat("0.00");
  10. if ( t > 1 ) {
  11. hrSize = dec.format(t).concat(" TB");
  12. } else if ( g > 1 ) {
  13. hrSize = dec.format(g).concat(" GB");
  14. } else if ( m > 1 ) {
  15. hrSize = dec.format(m).concat(" MB");
  16. } else if ( k > 1 ) {
  17. hrSize = dec.format(k).concat(" KB");
  18. } else {
  19. hrSize = dec.format(b).concat(" Bytes");
  20. }
  21. return hrSize;
  22. }
  23. }