Working with Decimals

Here is a script for adding decimals in Mikrotik. If any desire for other decimal operations, let me know and I can post on the Wiki. This is tested on 3.x (shouldn’t be that hard to port over 2.x though)

# Adds decimal numbers
# Set decimals variable to any numbers (decimal or integer) separated by commas
# (ex.  :local decimals ("2.43, 3.99, 395.6, 34, 56, 25, 2.3333, 11")
# Result: 530.3533

:local decimals ("2.43, 3.99, 395.6, 34, 56, 25, 2.3333, 11")




# Math Calculation here

:set decimals [:toarray $decimals]
:local result ""
:local resultint 0
:local resultdec 0

:if ([:len $decimals] > 1) do={

:for d from=0 to=([:len $decimals] - 1) do={

  :local tmpdecimal [:tostr [:pick $decimals $d]]
  :local curint 0
  :local curdec 0
  :local decimalpoint 0
  :for c from=0 to=([:len $tmpdecimal] - 1) do={
    :if ([:pick $tmpdecimal $c ($c + 1)] = ".") do={
      :set curint [:pick $tmpdecimal 0 $c]
      :set curdec [:pick $tmpdecimal ($c + 1) [:len $tmpdecimal]]

      :if ([:len $resultdec] > [:len $curdec]) do={ :for z from=1 to=([:len $resultdec] - [:len $curdec]) do={:set curdec ($curdec . "0") } }
      :if ([:len $curdec] > [:len $resultdec]) do={ :for z from=1 to=([:len $curdec] - [:len $resultdec]) do={:set resultdec ($resultdec . "0") } }

      :local tmpdec ($resultdec + $curdec)
      :if ([:len $tmpdec] > [:len $resultdec]) do={
          :set resultint ($resultint + $curint + [:pick $tmpdec 0 ([:len $tmpdec] - [:len $resultdec])])
          :set resultdec [:pick $tmpdec ([:len $tmpdec] - [:len $resultdec]) [:len $tmpdec]]
      } else={
        :set resultint ($resultint + $curint)
        :set resultdec ($resultdec + $curdec)
      }
      :set decimalpoint 1
    }
  }
  :if ($decimalpoint = 0) do={
    :set resultint ($resultint + $tmpdecimal)
  }
}
  :set result ($resultint . "." . $resultdec)
}
# END Calculation here

:put $result

Here is a script for dividing numbers (integers) that can produce a decimal number.

# Preforms calculation with decimal points
# dividend / divisor = quotient.decimal = result
# (ex. 3 / 11 =  0.2727)

:local dividend 3
:local divisor 11
:local decimalplaces 4


# Math Calculation here
:local quotient 0
:local remainder 0
:local result 0
:local decimal 0

:set quotient ($dividend / $divisor)
: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)

you could just add a link to this topic, without fuc**** flooding in those topics!!! :imp:

you could just add a link to this topic

Do you mean put the link in the Wiki?
The reason I put it here is for easier searching the forums specifically for decimal related calculation questions.

I mean,

http://forum.mikrotik.com/t/math-with-percentages/29573/4
http://forum.mikrotik.com/t/decimals/21737/3
http://forum.mikrotik.com/t/division-and-remainder/9453/5