Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/format.go

36 lignes
819 o
Go
Brut Vue normale Historique

2017-05-06 14:43:15 +02:00
package util
import (
"fmt"
)
func FormatFilesize(bytes int64) string {
var unit string
var value float64
if bytes >= 1024*1024*1024*1024 {
2017-05-06 21:50:38 +02:00
unit = "TiB"
value = float64(bytes) / (1024*1024*1024*1024)
} else if bytes >= 1024*1024*1024 {
2017-05-06 14:43:15 +02:00
unit = "GiB"
value = float64(bytes) / (1024*1024*1024)
} else if bytes >= 1024*1024 {
2017-05-06 14:43:15 +02:00
unit = "MiB"
value = float64(bytes) / (1024*1024)
} else if bytes >= 1024 {
2017-05-06 14:43:15 +02:00
unit = "KiB"
value = float64(bytes) / (1024)
} else {
unit = "B"
value = float64(bytes)
}
return fmt.Sprintf("%.1f %s", value, unit)
}
2017-05-07 13:51:59 +02:00
func FormatFilesize2(bytes int64) string {
if bytes == 0 { // this is what gorm returns for NULL
return "Unknown"
} else {
return FormatFilesize(bytes)
}
}