Thread Tools Display Modes
01-06-16, 05:30 PM   #1
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
Unit Health Short Value Help

The following code is from the ElvUI math.lua file. I've slightly altered it from showing decimal points to not showing them. However, I want to show decimal points for numbers less than 10 million, and higher than 1 million.

I'm having a strange issue with it. When I target a mob that has 1,467,810 HP, it properly displays 1.5m. However, when I target a mob that has 29,356,200 HP, it displays 3m, when it should display 30m.

Code:
function E:ShortValue(v)
	if v >= 1e9 then
		return ("%.fb"):format(v / 1e9):gsub("%.?+([kmb])$", "%1")
	elseif v >= 1e7 then
		return ("%.fm"):format(v / 1e7):gsub("%.?+([kmb])$", "%1")
	elseif abs(v) >= 1e6 then
		return format("%.1fm", v / 1e6)
	elseif v >= 1e3 or v <= -1e3 then
		return ("%.fk"):format(v / 1e3):gsub("%.?+([kmb])$", "%1")
	else
		return v
	end
end
  Reply With Quote
01-06-16, 05:54 PM   #2
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
I figured it out. I had to change the "1e7" in "format(v / 1e7)" to "1e6"
  Reply With Quote
01-06-16, 11:10 PM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
First of all, to remove the decimal point, you just need to modify the format specifier %.f to either %.0f or %d. Though the way your current modification is implemented, it doesn't cover "k" or "b" numbers.



I've been using a function like this for formatting network speeds and memory. I've modified it to work to your specifications.

Lua Code:
  1. local Suffixes={[0]="","k","m","b"};
  2. local function FormatNumber(val)
  3.     local exp=math.max(math.floor(math.log10(math.abs(val))),0);
  4.     local factor=math.min(math.floor(exp/3),#Suffixes);
  5.     return ("%."..(exp==factor*3 and 1 or 0).."f%s"):format(val/(1000^factor),Suffixes[factor]);
  6. end
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-06-16 at 11:35 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Unit Health Short Value Help


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