WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Changing Text Formatting (https://www.wowinterface.com/forums/showthread.php?t=55374)

cokedrivers 05-06-17 08:41 AM

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

SDPhantom 05-06-17 10:09 PM

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.


All times are GMT -6. The time now is 04:02 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI