github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/internal/gfmt/si.go (about)

     1  package gfmt
     2  
     3  import "fmt"
     4  
     5  // Si formats a number in a short si format
     6  func Si(n uint64) string {
     7  	suff := siRaw
     8  	x := float64(n)
     9  	for ; x >= 1024; x = x / 1024 {
    10  		suff++
    11  	}
    12  
    13  	return fmt.Sprintf("%.2f%s", x, suff.String())
    14  }
    15  
    16  type siSuff uint
    17  
    18  const (
    19  	siRaw siSuff = iota
    20  	siKilo
    21  	siMega
    22  	siGiga
    23  	siTera
    24  )
    25  
    26  func (s siSuff) String() string {
    27  	switch s {
    28  	case siRaw:
    29  		return ""
    30  	case siKilo:
    31  		return "K"
    32  	case siMega:
    33  		return "M"
    34  	case siGiga:
    35  		return "G"
    36  	case siTera:
    37  		return "T"
    38  	default:
    39  		return "T"
    40  	}
    41  }