1. #1

    Pitbull4/LUA text: Dropping decimals with Percent(cur,max)?

    Hello,

    I have a specialised question with LUA text and Pitbull4. I have set up my powerbar to this:

    Code:
    local cur,max = Power(unit),MaxPower(unit)
    if max > 0 then
      return "%s/%s - %s%%",VeryShort(cur,true),VeryShort(max,true),Percent(cur,max)
    end
    which will concurr to
    90k/90k - 100%.
    However, when going below 100% it will show the first decimal place, alas it reads 94.6%. But I only want it to show (in this example) 95%. This is easily accomplished with HP by adding VeryShort, however it won't work with Percent. Anyone got some ideas how to fix it?

    Thanks in advance,
    Luminee

    Edit: Actually, another viable option would be to have it show 96.0% instead of 96%, because it would stop flashing (it shows 95.1, 95.2, 95.3, etc., but not 96.0)..
    Last edited by Luminee; 2011-04-05 at 09:53 PM.

  2. #2
    Code:
    local cur,max = Power(unit),MaxPower(unit)
    if max > 0 then
      return "%s/%s - %s%%",VeryShort(cur,true),VeryShort(max,true),string.sub(min / max * 100),1,4
    end
    Try it, you might want to laborate with the last digit there, perhaps changing the 4 to a 3, or even 2. I can't test it right now but it should work.

  3. #3
    Thanks for the reply, but unfortunately your code results in an {err}. I tried to play around a bit with brackets without luck.

    Does lua know what min means? I tried replacing it with cur or cur,true, but then nothing at all would be visible.

    Any more ideas?

    Edit: Cool, i just replaced %s with %d and it rounds up now. Hoorah!

    Code:
    local cur,max = Power(unit),MaxPower(unit)
    if max > 0 then
      return "%s/%s - %d%%",VeryShort(cur,true),VeryShort(max,true),Percent(cur,max)
    end
    Last edited by Luminee; 2011-04-06 at 01:16 PM.

  4. #4
    Try this, may have a performance hit, but shouldn't be too bad

    Code:
    local cur,max = Power(unit),MaxPower(unit)
    if max > 0 then
      local p = 100*cur/max
      if p%1 >= 0.5 then p = math.ceil(p) else p = math.floor(p) end
      return "%s/%s - %s%%",VeryShort(cur,true),VeryShort(max,true),p
    end


    ---------- Post added 2011-04-06 at 03:15 PM ----------

    This is what i think gollie meant, but it has a flaw, as in the sub() function will return 4 (or 3 or 2) characters, which means that if you have 94.6%, it'll return "94.6", if you use 3, it'll return "94.", if you use 2, it'll return "94". But if you have 5.66% (for example), it'll still return "5.". So it'd need some logic to pick the right number of decimals, with a special case for 100%.

    Code:
    local cur,max = Power(unit),MaxPower(unit)
    if max > 0 then
      return "%s/%s - %s%%",VeryShort(cur,true),VeryShort(max,true),string.sub((cur / max * 100),1,4)
    end
    Last edited by Chaltione; 2011-04-06 at 01:18 PM. Reason: Percent() => actual formula

  5. #5
    As I edited above I was able to fix the issue by replacing %s with %d. It seems %d rounds up by default.

    Thank you for your input though!

  6. #6
    God, I'm so sorry, I didn't even check the variable names. I sorta copy pasted it from another addon. I'm glad you managed to figure it out though.

    And of course Chaltione is correct, I really need to work on my LUA :P
    Last edited by gollie; 2011-04-06 at 02:41 PM.

  7. #7
    I hope you are aware that %d is used for integer value, and the casting from floating to int will just strip the decimals, which means you are rounding down all percentages, even if it's .99.

  8. #8
    Well, I guess it serves my purpose right. I just need a hint of percentages for when to use mana cooldowns. I really don't care if it's 75,9 or 75.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •