2012-09-02 13:49:03 -06:00
|
|
|
// cmpout
|
|
|
|
|
2011-08-22 06:46:59 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type ByteSize float64
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota // ignore first value by assigning to blank identifier
|
|
|
|
KB ByteSize = 1 << (10 * iota)
|
|
|
|
MB
|
|
|
|
GB
|
|
|
|
TB
|
|
|
|
PB
|
|
|
|
EB
|
|
|
|
ZB
|
|
|
|
YB
|
|
|
|
)
|
|
|
|
|
|
|
|
func (b ByteSize) String() string {
|
|
|
|
switch {
|
|
|
|
case b >= YB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fYB", b/YB)
|
2011-08-22 06:46:59 -06:00
|
|
|
case b >= ZB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fZB", b/ZB)
|
2011-08-22 06:46:59 -06:00
|
|
|
case b >= EB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fEB", b/EB)
|
2011-08-22 06:46:59 -06:00
|
|
|
case b >= PB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fPB", b/PB)
|
2011-08-22 06:46:59 -06:00
|
|
|
case b >= TB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fTB", b/TB)
|
2011-08-22 06:46:59 -06:00
|
|
|
case b >= GB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fGB", b/GB)
|
2011-08-22 06:46:59 -06:00
|
|
|
case b >= MB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fMB", b/MB)
|
2011-08-22 06:46:59 -06:00
|
|
|
case b >= KB:
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fKB", b/KB)
|
2011-08-22 06:46:59 -06:00
|
|
|
}
|
2012-03-24 18:34:51 -06:00
|
|
|
return fmt.Sprintf("%.2fB", b)
|
2011-08-22 06:46:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println(YB, ByteSize(1e13))
|
|
|
|
}
|