Quantcast WoWInterface - View Single Post - Dividing hundreds and thousands by a comma
View Single Post
12-19-12, 09:59 PM   #11
A Pyroguard Emberseer
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,732
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:
  1. local ReformatNumberString; do
  2.     local AddCommas(pre,post) return pre..post:reverse():gsub("(%d%d%d)","%1"..LARGE_NUMBER_SEPERATOR):reverse(); end
  3.     local SplitDecimal(str) return str:gsub("^(%d)(%d+)",AddCommas); end
  4.     function ReformatNumberString(str) return str:gsub("[%d%.]+",SplitDecimal); end
  5. end
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote