github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/thirdparty/unit/unit.go (about)

     1  package unit
     2  
     3  import "fmt"
     4  
     5  type Information int64
     6  
     7  const (
     8  	_  Information = iota // ignore first value by assigning to blank identifier
     9  	KB             = 1 << (10 * iota)
    10  	MB
    11  	GB
    12  	TB
    13  	PB
    14  	EB
    15  )
    16  
    17  func (i Information) String() string {
    18  
    19  	tmp := int64(i)
    20  
    21  	// default
    22  	var d = tmp
    23  	symbol := "B"
    24  
    25  	switch {
    26  	case i > EB:
    27  		d = tmp / EB
    28  		symbol = "EB"
    29  	case i > PB:
    30  		d = tmp / PB
    31  		symbol = "PB"
    32  	case i > TB:
    33  		d = tmp / TB
    34  		symbol = "TB"
    35  	case i > GB:
    36  		d = tmp / GB
    37  		symbol = "GB"
    38  	case i > MB:
    39  		d = tmp / MB
    40  		symbol = "MB"
    41  	case i > KB:
    42  		d = tmp / KB
    43  		symbol = "KB"
    44  	}
    45  	return fmt.Sprintf("%d %s", d, symbol)
    46  }