SHARE
TWEET

siutil

mikebabcock Jan 24th, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @SuppressLint("DefaultLocale")
  2. public class SISize {
  3.     private long sizeinbytes = 0L;
  4.     private static final double BASEVAL = 1.1;
  5.    
  6.     public SISize(final long newsize) {
  7.         setBytes(newsize);
  8.     }
  9.    
  10.     void setBytes(final long newsize) {
  11.         sizeinbytes = newsize;
  12.     }
  13.    
  14.     void setKBytes(final long newsize) {
  15.         setBytes(newsize * 1024);
  16.     }
  17.    
  18.     private String toDecimalString(final double val) {
  19.         return String.format("%.2f", val);
  20.     }
  21.    
  22.     public String toString() {
  23.         StringBuilder sizestr = new StringBuilder();
  24.        
  25.         if (sizeinbytes < BASEVAL * 1024) {
  26.             sizestr.append(toDecimalString(sizeinbytes));
  27.             sizestr.append("B");
  28.         } else if (sizeinbytes < BASEVAL * 1024 * 1024) {
  29.             sizestr.append(toDecimalString(sizeinbytes/1024.0));
  30.             sizestr.append("KiB");
  31.         } else { // if (sizeinbytes < BASEVAL * 1024 * 1024 * 1024) {
  32.             sizestr.append(toDecimalString(sizeinbytes/(1024.0*1024.0)));
  33.             sizestr.append("MiB");
  34.         }
  35.        
  36.         return sizestr.toString();
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top