Money Variations

By Fumph on Jun 23, 2013

Input something along the lines of: 1m; 2m; 400k; 326.7k; 3.67million, 500800
Get in return: $1,000,000; $2,000,000; $400,000; $326,700, $3,670,000; $500,800

alias -l money.variations {
  var %a $1
  if ($remove(%a,$chr(44),$chr(46),$chr(36)) isnum) { return $chr(36) $+ $bytes($util.sanitizeNum(%a),b) }
  if ($regex(%a,/^\$?[0-9\.\,]+k/i)) { return $chr(36) $+ $bytes($calc(1000 * $util.sanitizeNum($remove(%a,k),dec)),b) }
  if ($regex(%a,/^\$?[0-9\.\,]+m/i)) { return $chr(36) $+ $bytes($calc(1000000 * $util.sanitizeNum($remove(%a,m),dec)),b) }
  if ($regex(%a,/^\$?[0-9\.\,]+million/i)) { return $chr(36) $+ $bytes($calc(1000000 * $util.sanitizeNum($remove(%a,m),dec)),b) }
  return %a
}
; This santization script was taken from somewhere, I modified it a bit to support decimals. I don't take credit for the 
; bulk of it!
alias -l util.sanitizeNum {
  var %a = 0, %b = $1
  while (%a < 255) { 
    if (%a !isnum 48-57 && %a !isnum $iif($2 == dec,44-46,48)) { var %b = $remove(%b, $chr(%a)) } 
    inc %a 
  }
  return %b
}

Useful for games, giveaway script, or really anything that needs a bit of relaxation.

Use:

echo -a $money.variation(4.5million)
echo -a $money.variation(32000)

Comments

Sign in to comment.
IllogicTC   -  Jun 30, 2013

I would find this more interesting to use in your example for a game or something if it wasn't forcing the $ on the beginning. Maybe have some variable to set what character(s) you want pre- or suffixed to the number? Like you could have the $12,345 by default, or you could make it use the Euro symbol, or say 12,345GP for those good ol' medieval style games.

It would also be interesting to be able to make this output WITHOUT commas. Let's say someone uses a command called !givegold Ted, which gives user Ted some amount of their gold in a game script. This snippet would allow the someone to go !givegold Ted 2k, and have it automatically give 2000 gold to Ted, instead of having to type it out.

Just some thoughts.

Yawhatnever  -  Jun 30, 2013

The output format is by design; you're just suggesting a different script.

IllogicTC  -  Jun 30, 2013

I still find limited use in forcing a dollar sign to the front. There's plenty of IRC servers that are frequented by people from a country that don't use the dollar as local currency.

Yawhatnever  -  Jun 30, 2013

They can remove the character.

Fumph  -  Jul 01, 2013

This is a nice idea, and I would happily give it a go (Overwhelmed with other projects though). Yawhatnever is right though, you can easily edit it for your needs, rather than adding parts that would only take up space and possibly speed! If you don't want the $ and the commas, take out: $chr(36) $+ $bytes(*,b)

Sign in to comment

Yawhatnever   -  Jun 23, 2013

Nice little snippet. A few things to note:

It doesn't actually work. You have a comma inside the expression which is causing the match to fail because mIRC sees it as an argument separator. You should save the expression to a %var and then use $regex(%a, %var). Escaping dots and commas inside a character class also isn't necessary. e.g. [.\,] == [.,]

Inside $util.sanitizeNum() this is kind of difficult to read:

if (%a !isnum 48-57 && %a !isnum $iif($2 == dec,44-46,48)) {

For starters, you've used the ascii values instead of the literal characters. The conditional is also a bit confusing, because you've essentially got something like if (%a !isnum 48... && ... !isnum 48). Not to mention looping through 255 times is going to be really slow if it's used frequently (such as inside another loop).

I'm also not really sure it accomplishes the goal of sanitizing a number. If the 'dec' argument is given, it simply leaves all commas and dots regardless of the number system used. mIRC won't properly parse and calc a numbering system using dots as separators (e.g. 4.000.000) nor will it be able to do $calc(4,000,000). Commas are seen as argument separators, and even if it's inside a variable it won't be calculated properly. I'm not sure if the sanitizeNum alias was meant to work with dot separator numbering systems or not, but either way the commas should always be stripped for mIRC.

You can do the same thing with a simple $regsubex() replace:

alias util.sanitizeNum {
  var %expression $iif($2 == dec, /[^\d.]/g, /[^\d]/g)
  return $regsubex($1, %expression, $null)
}

which removes everything that's not a digit, or if 'dec' is specified everything that's not a digit or a dot.

$money.variations() uses the same slightly changed command three times, so it could be shortened. It also uses $regex() to check the format before anything is done, so there's really no reason to use $util.sanitizeNum() at all because you already know your numbers are in a specific format. This means you could simply $remove() the commas.

Line 6 also removes "m" when it should be removing "million", although using $util.sanitizeNum() makes all of the $remove() and all of line 6 redundant because it would strip all of the characters anyway.

alias money.variation return $regsubex($remove($1, $chr(44)), /^\$?([\d.]+)(?:(k)|([mb])(?:illion)?|(t)(?:rillion)?)?$/Si, $ $+ $bytes($calc(\1 * 10 ^ (3 * $pos(kmbt, \2))), b))
Fumph  -  Jun 24, 2013

I see what you're saying and I'll take all of that into consideration! Thank you for your input, I'll try and come out with an even better version soon!

Sign in to comment

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.