github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/humanize/humanize.go (about) 1 // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package humanize 6 7 import ( 8 "fmt" 9 "math" 10 ) 11 12 var sizes = []string{"B", "K", "M", "G", "T", "P", "E"} 13 14 func logn(n, b float64) float64 { 15 return math.Log(n) / math.Log(b) 16 } 17 18 // Uint64 produces a human readable representation of the value. 19 func Uint64(s uint64) string { 20 const base = 1024 21 22 if s < 10 { 23 return fmt.Sprintf("%d B", s) 24 } 25 e := math.Floor(logn(float64(s), base)) 26 suffix := sizes[int(e)] 27 val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10 28 f := "%.0f %s" 29 if val < 10 { 30 f = "%.1f %s" 31 } 32 33 return fmt.Sprintf(f, val, suffix) 34 }