WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   decimal point/full number (https://www.wowinterface.com/forums/showthread.php?t=58631)

rulezyx 03-12-21 08:54 PM

decimal point/full number
 
Hello,

I got a code for my raidframes to show health with one decimal point.

Example:

44100 = 44.1k

When the health is under 1k:

910 = 0.9k

But I want it to display the full number when under a thousand so 910 instead of 0.9k.

Can someone help me with this code:
Code:

hooksecurefunc("CompactUnitFrame_UpdateStatusText",function(self)
if self.optionTable.healthText=="health" and tonumber(self.statusText:GetText() or "") then
local hp = UnitHealth(self.displayedUnit)
local maxhp = UnitHealthMax(self.displayedUnit)
self.statusText:SetText(format("%d%%    %.1fk",hp*100/maxhp,hp/1000))
self.statusText:SetTextColor(0.9,0.9,0.9,1)
if not self.smallStatus then
self.statusText:SetFont("Fonts\\ARIALN.TTF", 14, "THINOUTLINE")
self.smallStatus = false
end
end
end)


Kanegasi 03-12-21 09:33 PM

Here you go. I also future-proofed it by formatting higher numbers, if you ever run into that.

Lua Code:
  1. local function FormatValue(val)
  2.     if val<1000 then return ("%.0f"):format(val)
  3.     elseif val<1000000 then return ("%.1fk"):format(val/1000)
  4.     elseif val<1000000000 then return ("%.1fm"):format(val/1000000)
  5.     elseif val<1000000000000 then return ("%.1fb"):format(val/1000000000)
  6.     else return ("%.1ft"):format(val/1000000000000) end
  7. end
  8.  
  9. hooksecurefunc("CompactUnitFrame_UpdateStatusText",function(self)
  10.     if self.optionTable.healthText=="health" and tonumber(self.statusText:GetText() or "") then
  11.         local hp=UnitHealth(self.displayedUnit)
  12.         local maxhp=UnitHealthMax(self.displayedUnit)
  13.         self.statusText:SetText(format("%d%%     %s",hp*100/maxhp,FormatValue(hp)))
  14.         self.statusText:SetTextColor(0.9,0.9,0.9,1)
  15.         if not self.smallStatus then
  16.             self.statusText:SetFont("Fonts\\ARIALN.TTF",14,"THINOUTLINE")
  17.             self.smallStatus=false
  18.         end
  19.     end
  20. end)

rulezyx 03-12-21 10:32 PM

Thank you so much for the fast reply.:)

I used/ripped similar codes for my weakauras but I still rly have no exact clue how they work (lua format/decimal values).

I tested it out and it showed 658 as 658.000000

I changed %f in line 2 to %s and it seems to work now but I dont know if it interferes with the code itself :rolleyes:

Seerah 03-13-21 08:13 PM

"%.1f" means to format the number as a string with just one floating decimal point. "%.0f" means to format the number with 0 decimal places. I have no idea why you are getting many trailing zeros.

Alternately, you can replace that line with this:
Lua Code:
  1. if val<1000 then return math.floor(val)

Kanegasi 03-13-21 08:58 PM

It was originally just %f, but I edited my post after their reply to be %.0f instead.

rulezyx 03-13-21 11:48 PM

Its working fine now, thank you both so much :)


All times are GMT -6. The time now is 12:35 PM.

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