Math with Percentages?

Here is a precise way of calculating exact values with decimals (works with or without decimal numbers).

To use this in scripts you could create a global variables for dividend, divisor, and result then run this math script, setting the result variable for use in the calling script.

# Preforms calculation with decimal points
# dividend / divisor = quotient.decimal

: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)