mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-09 04:22:20 +00:00
Roll human_size over to MB when KB rounds to 1024.0 (#5186)
This commit is contained in:
1 parent
06911895c6
commit
b0482759e4
1 file changed
+15
-1
@@ -40,7 +40,8 @@ pub(crate) enum Nav {
|
||||
|
||||
pub fn human_size(n: usize) -> String {
|
||||
match n {
|
||||
n if n >= 1 << 20 => format!("{:.1} MB", n as f64 / (1u64 << 20) as f64),
|
||||
// A size that rounds to "1024.0 KB" reads as the next unit.
|
||||
n if n as f64 / 1024.0 >= 1023.95 => format!("{:.1} MB", n as f64 / (1u64 << 20) as f64),
|
||||
n if n >= 1 << 10 => format!("{:.1} KB", n as f64 / (1u64 << 10) as f64),
|
||||
n => byte_count(n),
|
||||
}
|
||||
@@ -50,3 +51,16 @@ pub fn human_size(n: usize) -> String {
|
||||
pub fn byte_count(n: usize) -> String {
|
||||
crate::i18n::trn(crate::i18n::current(), n as u64, "{n} byte", "{n} bytes")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod human_size_tests {
|
||||
use super::human_size;
|
||||
|
||||
#[test]
|
||||
fn rounds_up_into_next_unit() {
|
||||
assert_eq!(human_size(1_048_524), "1023.9 KB");
|
||||
assert_eq!(human_size(1_048_525), "1.0 MB");
|
||||
assert_eq!(human_size(1_048_575), "1.0 MB");
|
||||
assert_eq!(human_size(1_048_576), "1.0 MB");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user