I’m getting a value via SNMP that I need to divide by 10. How do I display the remainder? I don’t see any options in the manual.
I’ve a similar problem -
In a script I’m setting a user’s data usage pulled from User-Manager and would like to divide it by his monthly data cap in an automated email saying something like “You have used [[$usage / $datacap]*100] % of your monthly allowance to date.”
But all I can get is an integer. According to the manual:-
division
[admin@MikroTik] interface> :put (10s / 3)
3s333.333ms
[admin@MikroTik] interface> :put (5 / 2)
2
[admin@MikroTik] interface>
The first example suggests it can be done, but the second - well, 5 / 2 used to be 2.5 when I went to school.
Can anyone explain, or yet better give me the arcane and only-disclosed-to-MUM-attendees secret of Mikrotik scripting to achieve my end.
Break it down mathematically.
23 / 10 = 2 (in mikrotik) even though the answer is 2.3
It truncates the answer automatically, seeing as both input variables are Integers, but the answer is not an integer, but must also be an integer. The Decimal simply gets chucked out.
So, what to do. You’re now sitting with the answer 2.
Just multiply the Answer with your Divider, and take the figure you started out with, and subtract it from That answer. Voila. There you have your Remainder.
Mathematically speaking:
23 / 10 = 2.3 → Truncated = 2
2 x 10 = 20
23 - 20 = 3
Remainder = 3
Let me know if you need help in putting this down into a script.
-Krige
Numbers in RouterOS are treated as integers, i don’t even think that floating operations are useful here.
Anyway:
…"You have used [[$usage / $datacap]*100] %
…
5 / 2 used to be 2.5 when I went to school…
Well, if you studied in lessons, not just sitting there, then you must know that x/yz is the same as xz/y, right?
So you can easily calculate percentage like this: ( $usage*100/ $datacap ).
Here is a script I wrote to calculate exact decimal numbers to a specific decimal place.
Just fill in dividend, divisor, and decimalplaces and $result will populate with exact decimal result.
# Preforms calculation with decimal points
# dividend / divisor = quotient.decimal = result
:local dividend 3
:local divisor 11
:local decimalplaces 3
# Math Calculation here
:local quotient 0
:local remainder 0
:local result 0
:local decimal 0
:set quotient ($dividend / $divisor)
:if ($quotient = 0) do={
:set dividend [:tonum ($dividend . "0")]
}
:set remainder ($dividend - ($divisor * $quotient))
:if ($remainder > 0) do={
:local tmpremainder [:tonum ($remainder . "0")]
:for x from=1 to=$decimalplaces do={
:local tmpdecimal [:tonum ($tmpremainder / $divisor)]
:set decimal [:tonum ($decimal . $tmpdecimal)]
:set tmpremainder [:tonum (($tmpremainder - ($tmpdecimal * $divisor)) . "0")]
}
:set result ($quotient . "." . $decimal)
} else={
:set result $quotient
}
# END Math Calculation here
:put ($dividend . " / " . $divisor . " = " . $result)
Hope you find this useful,
Thanks very very much dssmiktik you saved my life you are a hero!! ![]()
Awesome script! Saved me a lot of time - thank you.
I’ve made a slight adjustment, as it didn’t seem to account for leading zeros in the remainder.
:local calcDecimalPointFunc do={
# dividend / divisor = quotient.decimal = result
:local dividend $1
:local divisor $2
:local decimalplaces 3
# Math Calculation here
:local quotient 0
:local remainder 0
:local result 0
:local decimal 0
:set quotient ($dividend / $divisor)
:if ($quotient = 0) do={
:set dividend [:tonum ($dividend . "0")]
}
:set remainder ($dividend - ($divisor * $quotient))
:log info $remainder
:if ($remainder > 0) do={
:local tmpremainder [:tonum ($remainder . "0")]
:for x from=1 to=$decimalplaces do={
:local tmpdecimal [:tonum ($tmpremainder / $divisor)]
:set decimal [:tonum ($decimal . $tmpdecimal)]
:set tmpremainder [:tonum (($tmpremainder - ($tmpdecimal * $divisor)) . "0")]
}
:local decimalTens 1
:local leadingZeros
:for y from=1 to=($decimalplaces -1) do={
:set decimalTens ($decimalTens * 10)
:if ($decimal < $decimalTens) do={
:set leadingZeros ($leadingZeros . "0")
}
}
:set result ($quotient . "." . $leadingZeros . $decimal)
:log info $decimal
} else={
:set result $quotient
}
# END Math Calculation here
:return $result
}