Skip to content

Commit

Permalink
Fix: Support 32 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlif committed Jun 27, 2022
1 parent 3582171 commit b7cb507
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions humanize/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ func (h *Humanizer) Language() language.Tag {
}

const (
kb = 1 << 10
mb = 1 << 20
gb = 1 << 30
tb = 1 << 40
pb = 1 << 50
kb = int64(1) << 10
mb = int64(1) << 20
gb = int64(1) << 30
tb = int64(1) << 40
pb = int64(1) << 50
)

// FilesizeFormat format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102 bytes, etc.).
//
// Valid inputs are byte arrays or any numeric value.
// For all other inputs, a string is returned with an error message in fmt style.
func (h *Humanizer) FilesizeFormat(v interface{}) string {
var count int
var count int64
switch val := v.(type) {
case []byte:
count = len(val)
count = int64(len(val))
default:
value, err := util.ToNumber(v)
if err != nil {
return formatErrorMessage(v)
}
count = int(value)
count = int64(value)
}

isNegative := count < 0
Expand All @@ -55,19 +55,19 @@ func (h *Humanizer) FilesizeFormat(v interface{}) string {
if count < kb {
result = h.loc.NGetf("%[1]d byte", "%[1]d bytes", count, count)
} else if count < mb {
formatted := h.loc.Print("%v", number.Decimal(float64(count)/kb, number.MaxFractionDigits(1)))
formatted := h.loc.Print("%v", number.Decimal(float64(count)/float64(kb), number.MaxFractionDigits(1)))
result = h.loc.Getf("%s KB", formatted)
} else if count < gb {
formatted := h.loc.Print("%v", number.Decimal(float64(count)/mb, number.MaxFractionDigits(1)))
formatted := h.loc.Print("%v", number.Decimal(float64(count)/float64(mb), number.MaxFractionDigits(1)))
result = h.loc.Getf("%s MB", formatted)
} else if count < tb {
formatted := h.loc.Print("%v", number.Decimal(float64(count)/gb, number.MaxFractionDigits(1)))
formatted := h.loc.Print("%v", number.Decimal(float64(count)/float64(gb), number.MaxFractionDigits(1)))
result = h.loc.Getf("%s GB", formatted)
} else if count < pb {
formatted := h.loc.Print("%v", number.Decimal(float64(count)/tb, number.MaxFractionDigits(1)))
formatted := h.loc.Print("%v", number.Decimal(float64(count)/float64(tb), number.MaxFractionDigits(1)))
result = h.loc.Getf("%s TB", formatted)
} else {
formatted := h.loc.Print("%v", number.Decimal(float64(count)/pb, number.MaxFractionDigits(1)))
formatted := h.loc.Print("%v", number.Decimal(float64(count)/float64(pb), number.MaxFractionDigits(1)))
result = h.loc.Getf("%s PB", formatted)
}

Expand Down

0 comments on commit b7cb507

Please sign in to comment.