github.com/jaypipes/ghw@v0.21.1/pkg/unitutil/unit.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package unitutil
     8  
     9  var (
    10  	KB int64 = 1024
    11  	MB       = KB * 1024
    12  	GB       = MB * 1024
    13  	TB       = GB * 1024
    14  	PB       = TB * 1024
    15  	EB       = PB * 1024
    16  )
    17  
    18  // AmountString returns a string representation of the amount with an amount
    19  // suffix corresponding to the nearest kibibit.
    20  //
    21  // For example, AmountString(1022) == "1022). AmountString(1024) == "1KB", etc
    22  func AmountString(size int64) (int64, string) {
    23  	switch {
    24  	case size < MB:
    25  		return KB, "KB"
    26  	case size < GB:
    27  		return MB, "MB"
    28  	case size < TB:
    29  		return GB, "GB"
    30  	case size < PB:
    31  		return TB, "TB"
    32  	case size < EB:
    33  		return PB, "PB"
    34  	default:
    35  		return EB, "EB"
    36  	}
    37  }