RuneScape mnemonic

By Aion- on Sep 04, 2012

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
}

Comments

Sign in to comment.
Aion-   -  Sep 04, 2012

This is also what I was thinking, however, the standard notation for billions is G instead of b so this is why I named it that way.

Edit-
There was an issue with numbers in the trillions. It now has been fixed. Alias comment was also rewritten.

 Respond  
Sorasyn   -  Sep 04, 2012

Doesn't have to be a "Runescape" mnemonic. Technically it's short hand notation for anything.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.