MCPcopy Create free account
hub / github.com/labstack/gommon / FormatBinary

Method FormatBinary

bytes/bytes.go:55–85  ·  view source on GitHub ↗

FormatBinary formats bytes integer to human readable string according to IEC 60027. For example, 31323 bytes will return 30.59KB.

(value int64)

Source from the content-addressed store, hash-verified

53// FormatBinary formats bytes integer to human readable string according to IEC 60027.
54// For example, 31323 bytes will return 30.59KB.
55func (*Bytes) FormatBinary(value int64) string {
56 multiple := ""
57 val := float64(value)
58
59 switch {
60 case value >= EiB:
61 val /= EiB
62 multiple = "EiB"
63 case value >= PiB:
64 val /= PiB
65 multiple = "PiB"
66 case value >= TiB:
67 val /= TiB
68 multiple = "TiB"
69 case value >= GiB:
70 val /= GiB
71 multiple = "GiB"
72 case value >= MiB:
73 val /= MiB
74 multiple = "MiB"
75 case value >= KiB:
76 val /= KiB
77 multiple = "KiB"
78 case value == 0:
79 return "0"
80 default:
81 return strconv.FormatInt(value, 10) + "B"
82 }
83
84 return fmt.Sprintf("%.2f%s", val, multiple)
85}
86
87// FormatDecimal formats bytes integer to human readable string according to SI international system of units.
88// For example, 31323 bytes will return 31.32KB.

Callers 2

FormatMethod · 0.95
FormatBinaryFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected