Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ VBscript ➜ Comma dividing long numbers

Comma dividing long numbers

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by ErockMahan   (81 posts)  Bio
Date Thu 04 Jan 2007 04:17 PM (UTC)
Message
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?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Thu 04 Jan 2007 09:01 PM (UTC)
Message
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.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by MattMc   USA  (54 posts)  Bio
Date Reply #2 on Sun 07 Jan 2007 05:01 PM (UTC)
Message
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.
Top

Posted by ErockMahan   (81 posts)  Bio
Date Reply #3 on Tue 09 Jan 2007 03:47 PM (UTC)
Message
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?
Top

Posted by MattMc   USA  (54 posts)  Bio
Date Reply #4 on Tue 09 Jan 2007 05:45 PM (UTC)
Message
Yep, you can use the optional "points after the decimal" setting in the function.

myFormatedNumber = FormatNumber(12345678,0)

Hope that helps.

Matt
Top

Posted by MattMc   USA  (54 posts)  Bio
Date Reply #5 on Tue 09 Jan 2007 05:46 PM (UTC)

Amended on Tue 09 Jan 2007 05:47 PM (UTC) by MattMc

Message
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  

Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,917 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.