fork download
  1.  
  2. class HumanReadable {
  3.  
  4. public static String humanReadableByteCount(final long bytes,final boolean si)
  5. {
  6. final int unit=si ? 1000 : 1024;
  7. if(bytes<unit)
  8. return bytes+" B";
  9. double result=bytes;
  10. final String unitsToUse=si ? "kMGTPE" : "KMGTPE";
  11. int i=0;
  12. final int unitsCount=unitsToUse.length();
  13. for(;i<unitsCount-1;++i)
  14. {
  15. result/=unit;
  16. if(result<unit)
  17. break;
  18. }
  19. final StringBuilder sb=new StringBuilder(9);
  20. sb.append(String.format("%.1f ",result));
  21. sb.append(unitsToUse.charAt(i));
  22. if(si)
  23. sb.append('B');
  24. else sb.append('i').append('B');
  25. final String resultStr=sb.toString();
  26. return resultStr;
  27. }
  28.  
  29. public static void main(String[] args) {
  30. System.out.println(humanReadableByteCount(999999999999999935L, true));
  31. System.out.println(humanReadableByteCount(999999999999999936L, true));
  32. }
  33.  
  34. }
  35.  
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
1000.0 PB
1000.0 EB