www.ClassicTW.com
http://classictw.com/

A helpful subroutine to format all numbers
http://classictw.com/viewtopic.php?f=15&t=34829
Page 1 of 1

Author:  Adept [ Mon Apr 11, 2016 7:43 pm ]
Post subject:  A helpful subroutine to format all numbers

This AIO subroutine processes integers, decimals, and abbreviated sums (e.g., 10T, 125M, 1,000B) and formats them with commas. It handles any length integer or decimal. Please post/note any code improvements. TIA.


* See below revision 1.01.


Code:
:COMMAFY
#  Subroutine -- COMMAFY function for boundless sums, including decimals and game abbreviated sums.  Use placeholder:  $_COMMAFY
stripText $_COMMAFY ","
stripText $_COMMAFY " "
isNumber $ck_sum $_COMMAFY
if ($ck_sum <> 1)
  getWordPos $_COMMAFY $ck_T "T"
  getWordPos $_COMMAFY $ck_M "M"
  getWordPos $_COMMAFY $ck_B "B"
  if ($ck_T > 0)
   striptext $_COMMAFY "T"
   setVar $_COMMAFY ($_COMMAFY*1000)
  elseif ($ck_M > 0)
   striptext $_COMMAFY "M"
   setVar $_COMMAFY ($_COMMAFY*1000000)
  elseif ($ck_B > 0)
   striptext $_COMMAFY "B"
   setVar $_COMMAFY ($_COMMAFY*1000000000)
  else
#  The string does not hold a valid number set.
   echo #27 "[1;31m***  ¯  Error: '" $_COMMAFY "' is NaN!  Exiting Commafy Subroutine...**" #27 "[0m"
   return
  end
end
getLength $_COMMAFY $sum_length
#  Processing for handling decimals.
setVar $deci_holder ""
getWordPos $_COMMAFY $deci "."
if ($deci > 0)
  setVar $cut_deci $sum_length-$deci
  cutText $_COMMAFY $deci_holder $deci ($cut_deci+1)
  cutText $_COMMAFY $_COMMAFY 1 ($deci-1)
  getLength $_COMMAFY $sum_length
end
if ($sum_length > 3)
  setVar $commafy ""
  setVar $loops ($sum_length/3)
  setVar $front ($sum_length-$loops*3)
  setVar $left ($sum_length-2)
  while ($loops > 0)
   cutText $_COMMAFY $pos $left 3
   setVar $comma ""
   if ($loops > 1)
    setVar $comma ","
   end
   mergeText $comma & $pos $commafy $commafy
   subtract $left 3
   subtract $loops 1
  end
#  Include the remaininng front porition of the sum and if applicable the decimal portion.
  setVar $pos ""
  if ($front > 0)
   cutText $_COMMAFY $pos 1 $front
   setVar $pos $pos & ","
  end
  mergeText $pos $commafy & $deci_holder $_COMMAFY
else
  mergeText $_COMMAFY $deci_holder $_COMMAFY
end
return

#  END COMMAFY

Author:  Adept [ Tue Apr 12, 2016 11:32 am ]
Post subject:  Re: A helpful subroutine to format all number formats

...And here is a competing subroutine, which abbreviates integers, e.g., 1K, 2.5M, 256B, 850.6T.

Code:
:SUMMIFY
#  Subroutine -- Converts integers into abbreviated sums, rounding to 1 tenths.  Use placeholder:  $_SUMMIFY
stripText $_SUMMIFY ","
stripText $_SUMMIFY " "
isNumber $ck_sum $_SUMMIFY
if ($ck_sum = 1)
  getLength $_SUMMIFY $sum_length
  setPrecision 3
  if ($sum_length < 4)
#  Do nothing...
  elseif ($sum_length > 3) AND ($sum_length < 7)
#  1,000  - 999,999 -- K/Thous.
   setVar $_SUMMIFY ($_SUMMIFY/1000)
   round $_SUMMIFY 1
   mergeText $_SUMMIFY "K" $_SUMMIFY
  elseif ($sum_length > 6) AND ($sum_length < 10)
#  1,000,000 - 999,999,999 -- M/Mill.
   setVar $_SUMMIFY ($_SUMMIFY/1000000)
   round $_SUMMIFY 1
   mergeText $_SUMMIFY "M" $_SUMMIFY
  elseif ($sum_length > 9) AND ($sum_length < 13)
#  1,000,000,000 - 999,999,999,999 -- B/Bill.
   setVar $_SUMMIFY ($_SUMMIFY/1000000000)
   round $_SUMMIFY 1
   mergeText $_SUMMIFY "B" $_SUMMIFY
  elseif ($sum_length > 12) AND ($sum_length < 16)
#  1,000,000,000,000 - 999,999,999,999,999 -- T/Trill.
   setVar $_SUMMIFY ($_SUMMIFY/1000000000000)
   round $_SUMMIFY 1
   mergeText $_SUMMIFY "T" $_SUMMIFY
  else
#  The string is out of bounds.
   echo #27 "[1;31m***  ¯  Error: '" $_SUMMIFY "' is out of bounds!  Exiting Summify Subroutine...**" #27 "[0m"
   setPrecision 0
   return
  end
  setPrecision 0
  stripText $_SUMMIFY ".0"
  getLength $_SUMMIFY $sum_length
else
#  The string does not hold a valid number set.
  echo #27 "[1;31m***  ¯  Error: '" $_SUMMIFY "' is NaN!  Exiting Summify Subroutine...**" #27 "[0m"
  return
end

#  END SUMMIFY

Author:  Adept [ Wed May 04, 2016 9:49 am ]
Post subject:  Re: A helpful subroutine to format all numbers

Now this is rather funny, I just came across the format command, which can provide commas to numbers (among other functions), so adding commas can be accomplished much more simply by using:

* NOTE: This will not work when compiled into a .cts file.

Code:
format $the_sum $_COMMAFY NUMBER
#  The following would depend if you wanted to maintain decimal accuracy or not, though you would need to know the decimals if not .00 and once the commas are added it is no longer treated numerically by TWX:
stripText $_COMMAFY ".00"


Easy peasy!

Author:  Adept [ Tue Jun 14, 2016 11:40 am ]
Post subject:  Re: A helpful subroutine to format all numbers

Here is a small revision taking into account negative numbers, which was overlooked before (all seems to be working well after a bit of testing):

Code:
:COMMAFY
#  Subroutine -- COMMAFY function for boundless sums, including decimals and game abbreviated sums.  Use placeholder:  $_COMMAFY
stripText $_COMMAFY ","
stripText $_COMMAFY " "
getWordPos $_COMMAFY $neg "-"
if ($neg > 0)
setVar $neg 1
stripText $_COMMAFY "-"
end
isNumber $ck_sum $_COMMAFY
if ($ck_sum <> 1)
  getWordPos $_COMMAFY $ck_T "T"
  getWordPos $_COMMAFY $ck_M "M"
  getWordPos $_COMMAFY $ck_B "B"
  if ($ck_T > 0)
   striptext $_COMMAFY "T"
   setVar $_COMMAFY ($_COMMAFY*1000)
  elseif ($ck_M > 0)
   striptext $_COMMAFY "M"
   setVar $_COMMAFY ($_COMMAFY*1000000)
  elseif ($ck_B > 0)
   striptext $_COMMAFY "B"
   setVar $_COMMAFY ($_COMMAFY*1000000000)
  else
#  The string does not hold a valid number set.
   echo #27 "[1;31m**  ¯  Error: '" $_COMMAFY "' is NaN!  Exiting Commafy Subroutine...**" #27 "[0m"
   return
  end
end
getLength $_COMMAFY $sum_length
#  Processing decimals.
setVar $deci_holder ""
getWordPos $_COMMAFY $deci "."
if ($deci > 0)
  setVar $cut_deci $sum_length-$deci
  cutText $_COMMAFY $deci_holder $deci ($cut_deci+1)
  cutText $_COMMAFY $_COMMAFY 1 ($deci-1)
  getLength $_COMMAFY $sum_length
end
setVar $pos ""
if ($sum_length > 3)
  setVar $commafy ""
  setVar $loops ($sum_length/3)
  setVar $front ($sum_length-$loops*3)
  setVar $left ($sum_length-2)
  while ($loops > 0)
   cutText $_COMMAFY $pos $left 3
   setVar $comma ""
   if ($loops > 1)
    setVar $comma ","
   end
   mergeText $comma & $pos $commafy $commafy
   subtract $left 3
   subtract $loops 1
  end
#  Include the remaininng front porition of the sum and if applicable the decimal portion.
  setVar $pos ""
  if ($front > 0)
   cutText $_COMMAFY $pos 1 $front
   setVar $pos $pos & ","
  end
  if ($neg = 1)
   setVar $pos "-" & $pos
  end
  mergeText $pos $commafy & $deci_holder $_COMMAFY
else
  if ($neg = 1)
   setVar $pos "-"
  end
   mergeText $pos $_COMMAFY & $deci_holder $_COMMAFY
end
return

#  END COMMAFY

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/