From 41263f0938f91738b4c148680abdb00632ed0e2b Mon Sep 17 00:00:00 2001 From: Vishal Rao Date: Wed, 16 Sep 2026 18:32:49 +0530 Subject: [PATCH] Fix format_network_speed util function calculation We separate the formatting of values and units, using the IEC_UNITS flag for the former and excluding it for the latter. This would cause the function to sometimes show the next higher unit for values close to the conversion borderline for IEC versus non-IEC unit values. For example, if the provided network speed parameter is 1020 Mbit/s the non-IEC formatting would pick "Gbit/s" as the unit because it's over the 1000-base mark. --- src/Utils.vala | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Utils.vala b/src/Utils.vala index e1bb59859..47f5dd2db 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -6,6 +6,8 @@ namespace Monitor.Utils { const int BITS_IN_BYTES = 8; const int MHZ_IN_GHZ = 1000; + const int IEC_UNIT_BASE = 1024; + const int NON_IEC_UNIT_BASE = 1000; const string NOT_AVAILABLE = (_("N/A")); const string NO_DATA = "\u2014"; @@ -67,11 +69,14 @@ public class Monitor.Utils.Strings { } public static string format_network_speed (uint64 speed_in_bytes_per_second) { + var speed_for_iec_units = speed_in_bytes_per_second * BITS_IN_BYTES; + var speed_adjusted_for_non_iec_units = speed_for_iec_units * NON_IEC_UNIT_BASE / IEC_UNIT_BASE; + ///TRANSLATORS: The first param is the numeric value (as string) of network speed. ///The second param with the appended "/s" is the network speed unit such as "Mb/s" for megabits per second. return _("%s %s/s").printf ( - format_size (speed_in_bytes_per_second * BITS_IN_BYTES, BITS | IEC_UNITS | ONLY_VALUE), - format_size (speed_in_bytes_per_second * BITS_IN_BYTES, BITS | ONLY_UNIT) + format_size (speed_for_iec_units, BITS | IEC_UNITS | ONLY_VALUE), + format_size (speed_adjusted_for_non_iec_units, BITS | ONLY_UNIT) ); } }