mIRC Homepage
Posted By: afterlife123 Formatting a number - 03/08/07 02:54 PM
Does anyone have/know the code to format a number out of and then back into a number in 1000's?

E.G Say you have an ever changing number E.G:
75,602
1,503,599
1,505

Would anyone be able to help me out in to change the number (ever changing) into E.G

75602
1503599
1505

:?

If you can also supply the code to be able to format the number back into a number with a , in the number would be helpful :P
Posted By: starbucks_mafia Re: Formatting a number - 03/08/07 03:34 PM
Here's a couple of simple aliases to format and un-format numbers:

Code:
alias num_format return $bytes($1, b) $+ $iif($int($1) != $1, $+(.,$gettok($1,2,46)), $null)

alias num_unformat return $remove($1-, $chr(44), $chr(32))


Just put those in the Remotes section of the script editor (Alt+R).

eg.
$num_format(67475) returns 67,475
$num_unformat(43,644) returns 43644
Posted By: RusselB Re: Formatting a number - 03/08/07 04:22 PM
Alternative suggestion to the first alias

Code:
alias num_format return $bytes($1,bd)


The d parameter will keep the decimals, if there are any.
Posted By: afterlife123 Re: Formatting a number - 03/08/07 04:57 PM
Cheers <3 blush
Posted By: LostShadow Re: Formatting a number - 03/08/07 06:08 PM
Thanks to RusselB for giving me another $comma() identifier.

Now I have 6. (But not all of them work efficiently.)

Code:
comma {
  var %format1 = $1
  var %format3 = $calc($len($1) + 1)
  while (%format3 > 1) {
    dec %format3
    if ($len(%format6) == 3) {
      var %format5 = $chr(44) $+ %format6 $+ %format5
      unset %format6
    }
    var %format6 = $mid(%format1,%format3,1) $+ %format6
  }
  return %format6 $+ %format5
}
comma2 {
  tokenize 46 $1 
  var %i = 1,%d,%n 
  while ($mid($1,- $+ %i,1) isnum) { 
    var %d = $ifmatch,%n = $+($iif(3 // %i,$chr(44)),%d,%n) 
    inc %i 
  } 
  return $iif($chr(44) $+ * iswm %n,$mid(%n,2),%n) $+ $iif($2,. $+ $2)
}
comma3 {
  var %z 
  return $(,,$regsub($1,/(^\d+?(?=(?>(?:\d{3})+)(?=\.|$))|\G\d{3}(?=\d))/g,\1 $+ $chr(44),%z)) %z
}
comma4 {
  var %a, %b = $regsub($ticks,$$1,/\G([+-]?\d+?)(?=(?:\d{3})++(?=\.\d++$|$))/g,\1 $+ $chr(44),%a)
  return %a
}
comma5 {
  if ($1 !isnum) return
  tokenize 46 $1
  return $+($iif(-* iswm $1,-),$bytes($abs($1),b),$iif($0 == 2,.),$2)
}

comma6 {
  return $bytes($1,bd)
}


As for removing commas, $remove(text,$chr(44)) removes all cases of $chr(44) in text. Text would likely be $1- for your case.

Thanks.

-Neal.
Posted By: SladeKraven Re: Formatting a number - 03/08/07 06:18 PM
Quote:

Thanks to RusselB for giving me another $comma() identifier.


See most people usually collect stamps and coins and then there's you who collect comma aliases. Way to be unique. grin
Posted By: Riamus2 Re: Formatting a number - 03/08/07 06:20 PM
As shown, those are possible aliases to do what you need. Of course, if you want to do it without an alias (it's actually shorter code anyhow), you can just do:

If you're using decimals:
$bytes(number_without_commas,bd)

If you're not using decimals:
$bytes(number_without_commas,b)

To remove commas:
$remove(number_with_commas,$chr(44))

Obviously, you'd put either a variable or a $1 (or similar $N identifier) into where those numbers are.

Examples:
%num = 3246357

//echo -a $bytes(%num,b)
3,246,357

%num = 324754.45

//echo -a $bytes(%num,bd)
324,754.45

%num = 45,875,245

//echo -a $remove(%num,$chr(44))
45875245
Posted By: RusselB Re: Formatting a number - 04/08/07 12:01 AM
One that I like using is

Code:
return $iif($remove($1-,$chr(32),$chr(44)) isnum,$bytes($v1,bd),$1- isn't recognized as a number)

© mIRC Discussion Forums