AK: Add an option to format numbers with 1000 based units
Instead of only allowing 1024-based units.
This commit is contained in:
parent
ccb6b4f943
commit
0dbb1be81c
Notes:
sideshowbarker
2024-07-17 09:49:33 +09:00
Author: https://github.com/kuzux Commit: https://github.com/SerenityOS/serenity/commit/0dbb1be81c Pull-request: https://github.com/SerenityOS/serenity/pull/16410 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/LucasChollet Reviewed-by: https://github.com/ldm5180
2 changed files with 12 additions and 8 deletions
|
@ -23,12 +23,12 @@ static DeprecatedString number_string_with_one_decimal(u64 number, u64 unit, Str
|
|||
return DeprecatedString::formatted("{}.{} {}", integer_part, decimal_part, suffix);
|
||||
}
|
||||
|
||||
DeprecatedString human_readable_quantity(u64 quantity, StringView unit)
|
||||
DeprecatedString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on, StringView unit)
|
||||
{
|
||||
constexpr u64 size_of_unit = 1024;
|
||||
u64 size_of_unit = based_on == HumanReadableBasedOn::Base2 ? 1024 : 1000;
|
||||
constexpr auto unit_prefixes = AK::Array { "", "K", "M", "G", "T", "P", "E" };
|
||||
auto full_unit_suffix = [&](int index) {
|
||||
auto binary_infix = (size_of_unit == 1024 && index != 0) ? "i"sv : ""sv;
|
||||
auto binary_infix = (based_on == HumanReadableBasedOn::Base2 && index != 0) ? "i"sv : ""sv;
|
||||
return DeprecatedString::formatted("{}{}{}",
|
||||
unit_prefixes[index], binary_infix, unit);
|
||||
};
|
||||
|
@ -51,9 +51,9 @@ DeprecatedString human_readable_quantity(u64 quantity, StringView unit)
|
|||
size_of_current_unit, full_unit_suffix(unit_prefixes.size() - 1));
|
||||
}
|
||||
|
||||
DeprecatedString human_readable_size(u64 size)
|
||||
DeprecatedString human_readable_size(u64 size, HumanReadableBasedOn based_on)
|
||||
{
|
||||
return human_readable_quantity(size, "B"sv);
|
||||
return human_readable_quantity(size, based_on, "B"sv);
|
||||
}
|
||||
|
||||
DeprecatedString human_readable_size_long(u64 size)
|
||||
|
|
|
@ -10,9 +10,13 @@
|
|||
|
||||
namespace AK {
|
||||
|
||||
// TODO: Add an optional base here for binary vs si units
|
||||
DeprecatedString human_readable_size(u64 size);
|
||||
DeprecatedString human_readable_quantity(u64 quantity, StringView unit = "B"sv);
|
||||
enum class HumanReadableBasedOn {
|
||||
Base2,
|
||||
Base10
|
||||
};
|
||||
|
||||
DeprecatedString human_readable_size(u64 size, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2);
|
||||
DeprecatedString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, StringView unit = "B"sv);
|
||||
|
||||
DeprecatedString human_readable_size_long(u64 size);
|
||||
DeprecatedString human_readable_time(i64 time_in_seconds);
|
||||
|
|
Loading…
Add table
Reference in a new issue