mirror of
https://git.eworm.de/cgit/routeros-scripts
synced 2025-07-03 06:34:27 +02:00
global-functions: introduce $HumanReadableNum
This commit is contained in:
parent
a4b2da8087
commit
890cd6c58d
1 changed files with 31 additions and 0 deletions
|
@ -38,6 +38,7 @@
|
||||||
:global GetRandomNumber;
|
:global GetRandomNumber;
|
||||||
:global Grep;
|
:global Grep;
|
||||||
:global HexToNum;
|
:global HexToNum;
|
||||||
|
:global HumanReadableNum;
|
||||||
:global IfThenElse;
|
:global IfThenElse;
|
||||||
:global IsDefaultRouteReachable;
|
:global IsDefaultRouteReachable;
|
||||||
:global IsDNSResolving;
|
:global IsDNSResolving;
|
||||||
|
@ -465,6 +466,36 @@
|
||||||
:return $Return;
|
: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)
|
# mimic conditional/ternary operator (condition ? consequent : alternative)
|
||||||
:set IfThenElse do={
|
:set IfThenElse do={
|
||||||
:if ([ :tostr $1 ] = "true" || [ :tobool $1 ] = true) do={
|
:if ([ :tostr $1 ] = "true" || [ :tobool $1 ] = true) do={
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue