bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/conf/bytesize.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package conf
     6  
     7  import (
     8  	"fmt"
     9  )
    10  
    11  type ByteSize float64
    12  
    13  const (
    14  	_           = iota
    15  	KB ByteSize = 1 << (10 * iota)
    16  	MB
    17  	GB
    18  	TB
    19  	PB
    20  	EB
    21  	ZB
    22  	YB
    23  )
    24  
    25  func (b ByteSize) String() string {
    26  	switch {
    27  	case b >= YB:
    28  		return fmt.Sprintf("%.2fYB", b/YB)
    29  	case b >= ZB:
    30  		return fmt.Sprintf("%.2fZB", b/ZB)
    31  	case b >= EB:
    32  		return fmt.Sprintf("%.2fEB", b/EB)
    33  	case b >= PB:
    34  		return fmt.Sprintf("%.2fPB", b/PB)
    35  	case b >= TB:
    36  		return fmt.Sprintf("%.2fTB", b/TB)
    37  	case b >= GB:
    38  		return fmt.Sprintf("%.2fGB", b/GB)
    39  	case b >= MB:
    40  		return fmt.Sprintf("%.2fMB", b/MB)
    41  	case b >= KB:
    42  		return fmt.Sprintf("%.2fKB", b/KB)
    43  	}
    44  	return fmt.Sprintf("%.2fB", b)
    45  }