blob: b45961114dd3bd81d0d3e2f587bf3ad67561a8df [file] [log] [blame]
Rob Pike18b21c72011-08-22 22:46:59 +10001// 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
5package main
6
7import "fmt"
8
9type ByteSize float64
10
11const (
12 _ = iota // ignore first value by assigning to blank identifier
13 KB ByteSize = 1 << (10 * iota)
14 MB
15 GB
16 TB
17 PB
18 EB
19 ZB
20 YB
21)
22
23func (b ByteSize) String() string {
24 switch {
25 case b >= YB:
Rob Pike40747952012-03-25 11:34:51 +110026 return fmt.Sprintf("%.2fYB", b/YB)
Rob Pike18b21c72011-08-22 22:46:59 +100027 case b >= ZB:
Rob Pike40747952012-03-25 11:34:51 +110028 return fmt.Sprintf("%.2fZB", b/ZB)
Rob Pike18b21c72011-08-22 22:46:59 +100029 case b >= EB:
Rob Pike40747952012-03-25 11:34:51 +110030 return fmt.Sprintf("%.2fEB", b/EB)
Rob Pike18b21c72011-08-22 22:46:59 +100031 case b >= PB:
Rob Pike40747952012-03-25 11:34:51 +110032 return fmt.Sprintf("%.2fPB", b/PB)
Rob Pike18b21c72011-08-22 22:46:59 +100033 case b >= TB:
Rob Pike40747952012-03-25 11:34:51 +110034 return fmt.Sprintf("%.2fTB", b/TB)
Rob Pike18b21c72011-08-22 22:46:59 +100035 case b >= GB:
Rob Pike40747952012-03-25 11:34:51 +110036 return fmt.Sprintf("%.2fGB", b/GB)
Rob Pike18b21c72011-08-22 22:46:59 +100037 case b >= MB:
Rob Pike40747952012-03-25 11:34:51 +110038 return fmt.Sprintf("%.2fMB", b/MB)
Rob Pike18b21c72011-08-22 22:46:59 +100039 case b >= KB:
Rob Pike40747952012-03-25 11:34:51 +110040 return fmt.Sprintf("%.2fKB", b/KB)
Rob Pike18b21c72011-08-22 22:46:59 +100041 }
Rob Pike40747952012-03-25 11:34:51 +110042 return fmt.Sprintf("%.2fB", b)
Rob Pike18b21c72011-08-22 22:46:59 +100043}
44
45func main() {
46 fmt.Println(YB, ByteSize(1e13))
47}