Thread Tools Display Modes
05-06-17, 08:41 AM   #1
cokedrivers
A Rage Talon Dragon Guard
 
cokedrivers's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 325
Changing Text Formatting

As the title says I want to change some text, I have 2 codes but they do not match as things are going on.

I would like tyo use the Frist Code for everything.

First Code (This code is being used for my Nameplates):
Code:
local FormatValue = function(number)
	if number < 1e3 then
		return floor(number)
	elseif number >= 1e12 then
		return string.format("%.3fT", number/1e12)
	elseif number >= 1e9 then
		return string.format("%.3fB", number/1e9)
	elseif number >= 1e6 then
		return string.format("%.2fM", number/1e6)
	elseif number >= 1e3 then
		return string.format("%.1fK", number/1e3)
	end
end
Second Code (This code is being used for my UnitFrames):
Code:
		local shorts = {
			{ 1e10, 1e9, "%.0fB" }, --  10b+ as  12B
			{  1e9, 1e9, "%.1fB" }, --   1b+ as 8.3B
			{  1e7, 1e6, "%.0fM" }, --  10m+ as  14M
			{  1e6, 1e6, "%.1fM" }, --   1m+ as 7.4M
			{  1e5, 1e3, "%.0fK" }, -- 100k+ as 840K
			{  1e3, 1e3, "%.1fK" }, --   1k+ as 2.5K
			{    0,   1,    "%d" }, -- < 1k  as  974
		}
		for i = 1, #shorts do
			shorts[i][4] = shorts[i][3] .. " (%.0f%%)"
		end

		hooksecurefunc("TextStatusBar_UpdateTextStringWithValues", function(statusBar, textString, value, valueMin, valueMax)
			if value == 0 then
				return textString:SetText("")
			end

			local style = GetCVar("statusTextDisplay")
			if style == "PERCENT" then
				return textString:SetFormattedText("%.0f%%", value / valueMax * 100)
			end
			for i = 1, #shorts do
				local t = shorts[i]
				if value >= t[1] then
					if style == "BOTH" then
						return textString:SetFormattedText(t[4], value / t[2], value / valueMax * 100)
					else
						if value < valueMax then
							for j = 1, #shorts do
								local v = shorts[j]
								if valueMax >= v[1] then
									return textString:SetFormattedText(t[3] .. " / " .. v[3], value / t[2], valueMax / v[2])
								end
							end
						end
						return textString:SetFormattedText(t[3], value / t[2])
					end
				end
			end
		end)
Hopefully someone can help me with the code.

Thanks
Coke

Last edited by cokedrivers : 05-06-17 at 09:10 AM.
  Reply With Quote
05-06-17, 10:09 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Here's what I've started doing with my number displays.
Lua Code:
  1. local math_abs=math.abs;
  2. local math_floor=math.floor;
  3. local math_log10=math.log10;
  4. local math_max=math.max;
  5. local tostring=tostring;
  6.  
  7. local NumberCaps={"K","M","B","T"};
  8. local function AbbreviateNumber(val)
  9.     local exp=math_max(0,math_floor(math_log10(math_abs(val))));
  10.     if exp<3 then return tostring(math_floor(val)); end
  11.  
  12.     local factor=math_floor(exp/3);
  13.     local precision=math_max(0,2-exp%3);
  14.     return ((val<0 and "-" or "").."%0."..precision.."f%s"):format(val/1000^factor,NumberCaps[factor] or "e"..(factor*3));
  15. end

It dynamically adjusts the precision based on if the number is in the 1's, 10's, or 100's. It'll revert to scientific notation if the abbreviation accessed is out of the defined scope. You can add more in the NumberCaps table.
__________________
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)

Last edited by SDPhantom : 05-06-17 at 10:18 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Changing Text Formatting

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off