WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Cataclysm Beta (https://www.wowinterface.com/forums/forumdisplay.php?f=82)
-   -   Lua help UnitDebuff (https://www.wowinterface.com/forums/showthread.php?t=35192)

Lane 09-28-10 07:04 AM

Lua help UnitDebuff
 
I'm trying to calculate the time remaining for a specific debuff. I'm not getting any errors but I think my lack of lua Math and Rounding is causing a nonsensical return.

Given:
Code:

local function getSpellTime(spell)
        local name, _, _, _, _, dur, exp, caster = UnitDebuff("target", spell)
        if name ~= nil and caster == "player" and name == spell then
                return exp - GetTime();
        end
end

My return value is usually something like 2... or 4...

Any idea what I need to get whole second times?

Lane 09-28-10 07:34 AM

So I realized part of my problem was having a narrow frame to display the value. I solved that part so it is now working except for the zillion decimal places showing up. How do I just drop the decimal values?

yj589794 09-28-10 08:54 AM

If you want to round up the value you can use math.ceil, or to round down the value you can use math.floor (http://lua-users.org/wiki/MathLibraryTutorial).

If you want to print a formatted value, but retain the actual value in the parameter, you can use string.format (http://www.lua.org/pil/20.html).

Ailae 09-28-10 08:58 AM

Or something like:

Code:

FontString:SetFormattedText("%.0f", floor(timeleft))
http://wowprogramming.com/docs/widge...tFormattedText

Lane 09-28-10 09:13 AM

Quote:

Originally Posted by Ailae (Post 207576)
Or something like:

Code:

FontString:SetFormattedText("%.0f", floor(timeleft))
http://wowprogramming.com/docs/widge...tFormattedText

I don't know C at all so I started to go this direction but couldn't figure out what any of those options mean.

What does %.0f mean in english?

Lane 09-28-10 09:15 AM

Also, to clarify I don't want to round at all I just want to drop the decimal place.

From: 20.34546825638
To: 20

From: 20.67483
To: 20

Ailae 09-28-10 09:24 AM

Quote:

Originally Posted by Lane (Post 207578)
What does %.0f mean in english?

%.0f means that it will format the number you're inputting to a float with 0 decimals. So if you hand it 24.1241 you'd get 24 out.

You can try it ingame with
Code:

/dump string.format("%.0f", 24.1241)
which would yield..

Code:

Dump: value=string.format("%.0f", 24.1241)
[1]=24

If you'd want one decimal, you would use 1 instead of 0.

Lane 09-28-10 11:14 AM

Cool I'll try that tonight. While I was doing research on this I found some printf resources that said "%d" would give me the same results. Would that work in Lua too?

Ailae 09-28-10 11:22 AM

It would, but then it doesn't round the value.

With %.0f 21.7 becomes 22.
With %d 21.7 becomes 21.

Lane 09-28-10 11:55 AM

Place holder for when I get home:

Code:

local function getSpellTime(spell)
        local name, _, _, _, _, dur, exp, caster = UnitDebuff("target", spell)
        if name ~= nil and caster == "player" and name == spell then
            --return math.ceil(exp - GetTime());
            return string.format("%d", exp - GetTime());
        end
end


abija 10-10-10 03:35 AM

You can try
Code:

local function getSpellTime(spell)
        local name, _, _, _, _, dur, exp = UnitDebuff("target", spell, nil, "PLAYER")
        if name then
            return string.format("%d", exp - GetTime())
        end
end


Aznamir 10-10-10 08:26 AM

In my mod I use
Code:

function PallyPower:FormatTime(time)
        if not time or time < 0 or time == 9999 then
                return ""
        end
        local mins = floor(time / 60)
        local secs = time - (mins * 60)
        return sformat("%d:%02d", mins, secs)
end



All times are GMT -6. The time now is 07:08 PM.

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