Skip to content

Commit

Permalink
fix: use IEC data unit (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Jul 22, 2024
1 parent 9b9427a commit 35e7686
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 5 additions & 1 deletion module/util/file.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package util

import (
"math/big"

"github.com/dustin/go-humanize"
)

// ByteCountBinary 把 Length 转换为文件大小, 自适应单位
func ByteCountBinary(b uint64) string {
return humanize.Bytes(b)
// uint64 to bigint
bigInt := new(big.Int).SetUint64(b)
return humanize.BigIBytes(bigInt)
}
8 changes: 4 additions & 4 deletions module/util/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ func TestByteCountBinary(t *testing.T) {
{
name: "KB",
args: args{b: 1024},
want: "1.0 kB",
want: "1.0 KiB",
},
{
name: "MB",
args: args{b: 1024 * 1024 * 3},
want: "3.1 MB",
want: "3.0 MiB",
},
{
name: "GB",
args: args{b: 1024 * 1024 * 1024 * 3},
want: "3.2 GB",
want: "3.0 GiB",
},
{
name: "TB",
args: args{b: 1024 * 1024 * 1024 * 1024 * 3},
want: "3.3 TB",
want: "3.0 TiB",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 35e7686

Please sign in to comment.