Dividing hundreds and thousands by a comma
Hi again all!
This isn't really too essential, and I'm maybe just being a bit too picky here, but is there any way to do the following: Convert 123123 to 123,123 ? I created the following function for use on the AUCTION_HOUSE_CLOSED event: Lua Code:
I've discovered though that GetCoinTextureString() returns the number format described above. I've tried my hand at using string.format but I've not had any luck so far. One other completely unrelated question while I'm here. When a variable is expressed within a function (for example "announce" in the function above; I understand it to be local to that function only. What I'm not sure of though is, what if I omitted the 'local'? Would it become a Global and pollute the global namespace, or will it still be local only to that function? Also, what about this example?: Lua Code:
Am I still required to prefix 'announce' with 'local' or should I not be doing that? Thanks in advance for any help you guys could give with this. |
If you take away the "local", then yes - it will be a global. Unless you declare that variable as local in your upvalues (outside of the function, therefore local to your whole Lua file.)
|
Blizzard's build-in UI function BreakUpLargeNumbers() does what you need.
|
Unfortunately, BreakUpLargeNumbers() only works on number values, not strings such as one returned by GetCoinTextureString(). The following code will try to reformat an already formatted string. Note, don't try to use this on strings that contain numbers with a decimal component.
Lua Code:
|
Quote:
So with this example, just so I've got it straight in my head: Lua Code:
The function argument 'announce' will now be a global because it wasn't declared local within the function? |
No. In your example, "announce" is a parameter which is passed in from elsewhere, so it is either already global or local to the scope from whence it originated. Also, the line:
Code:
announce = (announce or false) |
To further clarify, function arguments are treated as local variables and only exist within the scope of the function itself.
|
Quote:
Code:
function comma (str) |
I found this function while browsing the WoWAce forums. I believe the credit goes to Phanx, but I don't think the post explicitly stated that she wrote it.
Lua Code:
Only works for whole numbers apparently. |
Scroll up to my post above, should be #4. Unlike the code posted in the last couple posts, the one I wrote should work specifically on GetCoinTextureString(). This is because the pattern matching works on ALL numbers in the string, not just a random one of the group.
|
I came up with a little more complex code. This should handle decimal numbers and still support reformatting multiple numbers in a string. It works by nesting string:gsub() calls in replacement functions. The base function seeks out all numbers and calls SplitDecimal() to modify them. SplitDecimal()'s entire purpose is for decimal number support and will only grab the integer section of a decimal number, calling AddCommas() on it and leaving the rest alone. AddCommas() is what inserts the locale-dependent separator as stored in LARGE_NUMBER_SEPERATOR. This global is what's used in the Default UI code for BreakUpLargeNumbers().
Lua Code:
|
Quote:
Special thanks to Phantom... i'll give that code a wee try today :) Aanson |
All times are GMT -6. The time now is 03:21 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI