Welcome to the Builder Academy

Question Trigger Of The Day - Adding Commas To Numbers

More
06 May 2013 03:54 - 06 May 2013 03:55 #2139 by Fizban
The following script does something which sounds rather simple, but it's actually a fairly complicated script.

Specifically, you say a number, it echoes the same number back at you, but it adds commas in the appropriate spots.
Code:
1) Name : Add Commas To Numbers 2) Intended for : Rooms 3) Trigger types: Speech 4) Numeric Arg : 100 5) Arguments : * 6) Commands: set trim %speech.trim% *Remove whitespace from beginning and end of speech if %trim% / %trim% == 1 *Check if what they typed was a number set length %trim.strlen% *Count how many characters are in the number they typed eval thirdOfLength %length% /3 eval recalculatedLength %thirdOfLength% * 3 if %recalculatedLength% == %length% set divisibleByThree 1 end if %length% > 3 if %length% < 6 set commasToAdd 1 else if %divisibleByThree% eval commasToAdd %thirdOfLength% - 1 else eval commasToAdd %thirdOfLength% end end else set commasToAdd 0 end set i 0 set x 0 set curchar %length% while %i% < %length% set output %trim.charat(%curchar%)%%output% *Rebuild the number starting with the last digit eval i %i% + 1 *Raise the value of %i% by 1 eval curchar %curchar% - 1 eval x %x% + 1 if %x% == 3 if %commasAdded% < %commasToAdd% && %length% > 3 *Check if it has been three digits since you added the last comma *also ensure you are not adding a comma if it is only three digits * or else it will print things like: ,473 set output ,%output% *Add a comma end set x 0 *Reset the counter that keeps track of how many digits since the last comma eval commasAdded %commasAdded% + 1 end done %echo% %output% *echo the number after adding commas to it end

Here's an example of it in use:

> say 1
You say, '1'
1

> say 12
You say, '12'
12

> say 123
You say, '123'
123

> say 1234
You say, '1234'
1,234

> say 123456
You say, '123456'
123,456

> say 12345678910111213141516171819
You say, '12345678910111213141516171819'
12,345,678,910,111,213,141,516,171,819

Last edit: 06 May 2013 03:55 by Fizban.

Please Log in or Create an account to join the conversation.

Time to create page: 0.187 seconds