; /******************************************************************
;  * num2word v1.0 by dave (dave@vape.net)                          *
;  * -------------------------------------------------------------- *
;  *   usage: $num2word(integer)                                    *
;  * example: $num2word(2624682729872198721252151)                  *
;  *   notes: supports up to 66 digits. the number can be negative. *
;  ******************************************************************/

; unit to word..
alias -l _num2word_u { return $gettok(one.two.three.four.five.six.seven.eight.nine,$1,46) }

; tens to word..
alias -l _num2word_t { return $gettok(ten.twenty.thirty.forty.fifty.sixty.seventy.eighty.ninety,$1,46) }

; gotta love the teens!
alias -l _num2word_teens { return $gettok(eleven.twelve.thirteen.fourteen.fifteen.sixteen.seventeen.eighteen.nineteen,$1,46) }

; number notation for those BIG numbers..
alias -l _num2word_large {
  return $gettok(thousand.m.b.tr.quadr.quint.sext.sept.oct.non.dec.undec.duodec.tredec.quatuordec.quindec.sexdec.septendec.octodec.novemdec.vigint,$1,46) $+ $iif($1 > 1,illion)
  ;  above notation provided by http://www.sisweb.com/math/general/numnotation.htm
}

; returns word value of numbers from 1 to 999
alias -l _num2word {
  if ($1 == $null) { return $null }
  if ($1 !isnum) { return $null }
  if ($1 > 999) { return too big! }
  var %parm = $str(0,$calc(3 - $len($1))) $+ $1
  var %h = $mid(%parm,1,1)
  var %t = $mid(%parm,2,1)
  var %u = $mid(%parm,3,1)
  if (%u > 0) { var %tu = $_num2word_u(%u) }
  if (%t > 0) { var %tt = $_num2word_t(%t) }
  if (%h > 0) { var %th = $_num2word_u(%h) hundred }

  if ((%t == 0) && (%h == 0)) { return %tu }
  if ((%h == 0) && (%u == 0)) { return %tt }
  if ((%u == 0) && (%t == 0)) { return %th }
  if ((%h == 0) && (%t >= 2)) { return %tt $+ - $+ %tu }
  if ((%h == 0) && (%t == 1)) { return $_num2word_teens(%u) }
  if ((%t == 0) && (%h >  0)) { return %th and %tu }
  if ((%h >  0) && (%u == 0)) { return %th and %tt }
  if ((%h >  0) && (%t >= 2)) { return %th and %tt $+ - $+ %tu }
  if ((%h >  0) && (%t == 1)) { return %th and $_num2word_teens(%u) }
  ; re: the above: it's better to be safe than sorry, right?!

  return how the hell?
}

; the main guy, returns word format for numbers from -66digit to +66digit

; note: if you're using this to send to an irc server.. the packet limit
; for ircd is 512 bytes, so you might have to split it up, if the number
; gets too big. also, mirc has its own limit. beware! :P

alias num2word {
  var %number = $1
  var %text

  if ($int(%number) == 0) { return zero }
  if ($len(%number) > 66) { return too damn big! }
  if ($left(%number,1) == -) { var %neg = true | var %number = $right(%number,-1) }
  var %cutoff = $calc($len(%number) / 3)
  if ($int(%cutoff) != %cutoff) { var %cutoff = $int($calc(%cutoff + 1)) }
  var %loop = -1

  :loop

  inc %loop
  if (%loop >= %cutoff) {
    if (%neg != true) { return %text }
    else { return negative %text }
  }
  if (%loop == 0) { var %current = $right(%number,3) }
  else { var %current = $right($left(%number,$calc(%loop * -3)),3) }
  if (%current == 000) { goto loop }
  var %current_text = $_num2word(%current)
  if (%loop == 0) {
    var %text = %current_text
    if ((%current < 100) && (%cutoff > 1)) { var %text = and %current_text }
  }
  else {
    if (%text) { %text = %current_text $_num2word_large(%loop) $+ , %text }
    else { var %text = %current_text $_num2word_large(%loop) }
  }

  goto loop
}

