hyunsik / bytesize

An utility that easily makes bytes size representation and helps its arithmetic operations.
Apache License 2.0
107 stars 31 forks source link

Added a more intuitive helper struct when printing ByteSize #32

Open ChanTsune opened 1 year ago

ChanTsune commented 1 year ago

The Display trait currently implemented in ByteSize only supports decimal prefixes. If we want to use a binary prefix, we need to use ByteSize::to_string_as. I have added helper some struct and methods to use them to ByteSize to make these more intuitive with refer to std::path::Path.

use bytesize::ByteSize;

assert_eq!("100 B", format!("{}", ByteSize::b(100).binary_display()));
assert_eq!("1.8 TiB", format!("{}", ByteSize::tb(2).binary_display()));

assert_eq!("100 B", format!("{}", ByteSize::b(100).decimal_display()));
assert_eq!("2.0 TB", format!("{}", ByteSize::tb(2).decimal_display()));

If you like this PR, it would be my pleasure if you merge it!