This alias finds the significand of <$1>, rounds it to [$2] significant figures and appends the corresponding RuneScape mnemonic.
Prefixes of the RuneScape metric system Symbol Factor k 1 000 m 1 000 0000 b 1 000 0000 0000 t 1 000 0000 0000 000
Examples:
$convert(1000) -> 1k
$convert(3452, 2) -> 3.45k
$convert(-45656, 1) -> -45.6k
$convert(32462456, 0) -> 32m
/*
* This alias finds the significand of <$1>, rounds it to [$2] significant figures
* and appends the corresponding RuneScape mnemonic.
*
* Param $1: the number (up to trillions)
* Param $2: the precision - optional, default is 0
*/
alias convert {
if ($1 isnum) {
var %number $abs($1)
var %prefix $iif($1 < 0, -, $null)
var %precision $iif($2 isnum, $2, 0)
if (%number < 1000) {
return %prefix $+ %number
}
var %exp $int($calc($log(%number) / $log(1000)))
var %significand $calc(%number / (1000 ^ %exp))
var %suffix $mid(kmbt, %exp, $iif($calc((4 - %exp) * -1) == 0, 1, $v1))
if (%precision == 0) {
if (%significand == 1 || $right(%significand, 1) == 0) {
return %prefix $+ %significand $+ %suffix
}
return %prefix $+ $mid(%significand, 0, $calc($pos(%significand, ., 1) - 1)) $+ %suffix
}
var %length $len($mid(%significand, $calc($pos(%significand, ., 1) + 1)))
if (%precision > %length) {
%precision = %length
}
return %prefix $+ $mid(%significand, 0, $calc($len(%significand) - (%length - %precision))) $+ %suffix
}
; no param provided
return 0
}