Quantcast
decimal point/full number - WoWInterface
Thread Tools Display Modes
03-12-21, 08:54 PM   #1
rulezyx
A Theradrim Guardian
 
rulezyx's Avatar
Join Date: Jan 2020
Posts: 68
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)
  Reply With Quote
03-12-21, 09:33 PM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 556
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)

Last edited by Kanegasi : 03-13-21 at 01:06 PM. Reason: 0 decimal places for less than 1k
  Reply With Quote
03-12-21, 10:32 PM   #3
rulezyx
A Theradrim Guardian
 
rulezyx's Avatar
Join Date: Jan 2020
Posts: 68
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
  Reply With Quote
03-13-21, 08:13 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,831
"%.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)
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
03-13-21, 08:58 PM   #5
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 556
It was originally just %f, but I edited my post after their reply to be %.0f instead.
  Reply With Quote
03-13-21, 11:48 PM   #6
rulezyx
A Theradrim Guardian
 
rulezyx's Avatar
Join Date: Jan 2020
Posts: 68
Its working fine now, thank you both so much
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » decimal point/full number

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