Thread Tools Display Modes
07-18-10, 06:42 PM   #1
akgis
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 29
Lua Long number to short format

Iam not to good with mathematics but I get around coding fine, lately I started to get onto Lua I used to program as a hobby on Pascal, Basic and VB 6.

I have a probleam I wanted to transform a long number like 1500000 into 1.5 Mil or 500000 into 500k, wanted to use this for a little addon I been developing and this would make easy to read large numbers

I know some already wrote code for this I saw this into Skada, Recount, Shadowed Unit Frames. I had dug into said addons to take some ideas but complexity of those addons are to big for me atm

I was hoping if someone could help me with a code snippet or the algoritm in english.

Thanks very much
  Reply With Quote
07-18-10, 07:01 PM   #2
Dainton
A Flamescale Wyrmkin
 
Dainton's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2008
Posts: 115
Try this out and just pass whatever you're trying to shorten through shortnum() (or w/e you want to name it).
Code:
local shortnum = function(v)
	if v <= 9999 then
		return v
	elseif v >= 1000000 then
		return format("%.1f mil", v/1000000)
	elseif v >= 10000 then
		return format("%.1fk", v/1000)
	end
end
  Reply With Quote
07-18-10, 07:07 PM   #3
akgis
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 29
Thanks alot Dainton. That code seems to work fine but egingell @wowace hooked me up with a similar code I already implemented

Code:
local function ReadableNumber(num, places)
    local ret
    local placeValue = ("%%.%df"):format(places or 0)
    if not num then
        return 0
    elseif num >= 1000000000000 then
        ret = placeValue:format(num / 1000000000000) .. " Tril" -- trillion
    elseif num >= 1000000000 then
        ret = placeValue:format(num / 1000000000) .. " Bil" -- billion
    elseif num >= 1000000 then
        ret = placeValue:format(num / 1000000) .. " Mil" -- million
    elseif num >= 1000 then
        ret = placeValue:format(num / 1000) .. "k" -- thousand
    else
        ret = num -- hundreds
    end
    return ret
end
So its fixed thanks alot anyway Dainton
  Reply With Quote
07-19-10, 08:21 AM   #4
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
Also, if all those zeros get messy you can always use 1e12 (1 and 12 zeros)
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Lua Long number to short format


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