forked from maddyblue/goread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytesize.go
45 lines (40 loc) · 835 Bytes
/
bytesize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 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 appstats
import (
"fmt"
)
type byteSize float64
const (
_ = iota
_KB byteSize = 1 << (10 * iota)
_MB
_GB
_TB
_PB
_EB
_ZB
_YB
)
func (b byteSize) String() string {
switch {
case b >= _YB:
return fmt.Sprintf("%.2fYB", b/_YB)
case b >= _ZB:
return fmt.Sprintf("%.2fZB", b/_ZB)
case b >= _EB:
return fmt.Sprintf("%.2fEB", b/_EB)
case b >= _PB:
return fmt.Sprintf("%.2fPB", b/_PB)
case b >= _TB:
return fmt.Sprintf("%.2fTB", b/_TB)
case b >= _GB:
return fmt.Sprintf("%.2fGB", b/_GB)
case b >= _MB:
return fmt.Sprintf("%.2fMB", b/_MB)
case b >= _KB:
return fmt.Sprintf("%.2fKB", b/_KB)
}
return fmt.Sprintf("%.2fB", b)
}