Comma dividing long numbers

Posted by ErockMahan on Thu 04 Jan 2007 04:17 PM — 6 posts, 28,031 views.

#0
I would think this to be something relatively easy, but I'm having issues working it out. Here's what I'm trying to do:

I have built a trigger that will calculate how much experience I've collected over the day. It's really simple, and I save it under the variable "xptoday."

That works fine.

What I'm trying to do now is to create a seperate trigger that will divide that number into a readable number with commas. Meaning: 123456789 experience points for the day would be displayed as 123,456,789. The idea I had to work this, I think, should work beautifully, if I can get it to work at all:


total = getvariable("xptoday")
setvariable "mil",0
setvariable "thou",0


while total > 999999
mil = world.getvariable (mil)+1
total = total - 1000000
wend


while total > 999
thou = world.getvariable (thou)+1
total = total - 1000
wend

hund = total




To translate, if the number is larger than one million, it will subtract one million from it, add one to the "million" variable, then try again. By the time it is less than one million, I should have the million value stored in the "million" variable, with the remaining total stored in the variable "total". The process is then repeated for the thousands. (I know that this might take a long time, and I could break it down further if necessary, but I'd like to make this much work.)

In the end, I will have it display (in the output, if you like) the million value, followed by a comma, the thousands value, again followed by a comma, topped off by the remaining number "hund" or "total" (they'll have the same value).

The idea seems reasonable enough, but something is wrong with my code. Any guesses what?
USA #1
Setting mil isn't the same thing as calling setvariable. One is a variable in VBScript, and the other is a MUSHclient variable. You've mixed them, and you only update the VBScript variable, not the MUSHclient variable. You should pick one or the other; probably just the VBScript variable.
USA #2
VBScript has a function called FormatNumber, such as follows:

myNumber = FormatNumber(158257197)
world.note myNumber

This would output 158,257,197.

I hope this helps.
#3
I thank you both for your suggestions; it turns out that I WAS confusing variable types, but once I stopped doing that, everything worked out great.

Until, that is, my computer started getting low on memory. It seems that powerful "for" loops like that can be quite taxing on poor computers like mine. I suspected as much, so I'm now making use of the FormatNumber feature, which is significantly nicer, and easier on the computer.

As it is displayed in this post, though, my client is adding two decimal places (.00) after everything it calculates. Any idea how to remove it?
USA #4
Yep, you can use the optional "points after the decimal" setting in the function.

myFormatedNumber = FormatNumber(12345678,0)

Hope that helps.

Matt
USA #5
Here's the full syntax for FormatNumber, from w3schools.com:

Syntax
FormatNumber(Expression[,NumDigAfterDec[,
IncLeadingDig[,UseParForNegNum[,GroupDig]]]]) 

Parameter Description 
Expression (Required). The expression to be formatted

NumDigAfterDec (Optional). Indicates how many places to the right of the decimal are displayed. Default is -1 (the computer's regional settings are used) 

IncLeadingDig (Optional). Indicates whether or not a leading zero is displayed for fractional values:
-2 = TristateUseDefault - Use the computer's regional settings 
-1 = TristateTrue - True 
0 = TristateFalse - False 
 
UseParForNegNum (Optional). Indicates whether or not to place negative values within parentheses:
-2 = TristateUseDefault - Use the computer's regional settings 
-1 = TristateTrue - True 
0 = TristateFalse - False 
 
GroupDig (Optional). Indicates whether or not numbers are grouped using the group delimiter specified in the computer's regional settings:
-2 = TristateUseDefault - Use the computer's regional settings 
-1 = TristateTrue - True 
0 = TristateFalse - False