aboutsummaryrefslogtreecommitdiffstats
path: root/global-functions.rsc
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2023-11-15 11:36:47 +0100
committerGravatar Christian Hesse <mail@eworm.de>2023-11-15 14:34:40 +0100
commit890cd6c58d3de7e91a95c4593243d30f1b6234af (patch)
treeb5a2a54cd6b93a8a43ab80a12b8ac4b98455f902 /global-functions.rsc
parenta4b2da8087160ec2cf2ac12183c63b5609596f3d (diff)
global-functions: introduce $HumanReadableNum
Diffstat (limited to 'global-functions.rsc')
-rw-r--r--global-functions.rsc31
1 files changed, 31 insertions, 0 deletions
diff --git a/global-functions.rsc b/global-functions.rsc
index f0446bd..090269d 100644
--- a/global-functions.rsc
+++ b/global-functions.rsc
@@ -38,6 +38,7 @@
:global GetRandomNumber;
:global Grep;
:global HexToNum;
+:global HumanReadableNum;
:global IfThenElse;
:global IsDefaultRouteReachable;
:global IsDNSResolving;
@@ -465,6 +466,36 @@
:return $Return;
}
+# return human readable number
+:global HumanReadableNum do={
+ :local Input [ :tonum $1 ];
+ :local Base [ :tonum $2 ];
+
+ :global EitherOr;
+
+ :local Prefix "kMGTPE";
+ :local Pow 1;
+
+ :set Base [ $EitherOr $Base 1024 ];
+
+ :if ($Input < $Base) do={
+ :return $Input;
+ }
+
+ :for I from=0 to=[ :len $Prefix ] do={
+ :set Pow ($Pow * $Base);
+ :if ($Input / $Base < $Pow) do={
+ :set Prefix [ :pick $Prefix $I ];
+ :local Tmp1 ($Input * 100 / $Pow);
+ :local Tmp2 ($Tmp1 / 100);
+ :if ($Tmp2 >= 100) do={
+ :return ($Tmp2 . $Prefix);
+ }
+ :return ($Tmp2 . "." . [ :pick $Tmp1 [ :len $Tmp2 ] ([ :len $Tmp1 ] - [ :len $Tmp2 ] + 1) ] . $Prefix);
+ }
+ }
+}
+
# mimic conditional/ternary operator (condition ? consequent : alternative)
:set IfThenElse do={
:if ([ :tostr $1 ] = "true" || [ :tobool $1 ] = true) do={