View Single Post
Unread 12-19-12, 09:59 PM   #11
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,067
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
__________________
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
SDPhantom is offline   Reply With Quote