Hi,
Working on an oUF and I would like to add commas into my numbers, like my health is 42,080 instead of 42080.
I just can't even think of how to search for this or ... all my searches come up with nothing. Would it be very difficult?
Thanks!

Thanks, Dridzt!
I'm not sure of the etiquette - should I ask them before using this bit of code?

In case anyone else is struggling with this, here's code (that I used for a healthbar) that shows commas if my target is under 1 million health, but rounds if he's over. It adds also a green m for millions.

When showing my own health, I don't like it rounded off because I like to see the full numbers when changing out gear. But numbers are far more legible with the commas.

local function numformat(number)
if number < 0 or number == 0 or not number then
return 0
elseif number > 0 and number < 1000000 then
local t = {}
thousands = ','
decimal = '.'
local int = math.floor(number)
local rest = number % 1
if int == 0 then
t[#t+1] = 0
else
local digits = math.log10(int)
local segments = math.floor(digits / 3)
t[#t+1] = math.floor(int / 1000^segments)
for i = segments-1, 0, -1 do
t[#t+1] = thousands
t[#t+1] = ("%03d"):format(math.floor(int / 1000^i) % 1000)
end
end
if rest ~= 0 then
t[#t+1] = decimal
rest = math.floor(rest * 10^6)
while rest % 10 == 0 do
rest = rest / 10
end
t[#t+1] = rest
end
local s = table.concat(t)
wipe(t)
return s
elseif number >= 1000000 and number < 1000000000 then
return format("%.1f|cff93E74F%s|r", number * 0.000001, "m")
elseif number >= 1000000000 then
return format("%.1f|cff93E74F%s|r", number * 0.000000001, "bil")
else
return number
end
end

Note that 93E74F is the color value for the "m" and "bil", and can be changed.

And to call it:

local num1 = numformat(12345)
local num2 = numformat(12345678)
MyTextField:SetText(num1.."/"..num2)

You actually want the "not number" part of the if statement first because if the number is nil, you will get an lua error saying you can't compare nil with a number (nil <= 0). Also <= is the proper form for less than or equal. So that first if statement should just be:

if not number or number <= 0 then

Being picky here, but you don't need to specify the "number > 0" in the first elseif because there's no way it'll get to that statement if the number isn't > 0 :)

Locale["enUS"] = {
...
-- number formatting
digitGrouping = 3,
groupingSeparator = ",",
decimalSeparator = ".",
...
}
-- Meanwhile in another file:
local function numformat(number)
local digitGrouping = Locale["enUS"].digitGrouping
local groupingSeparator = Locale["enUS"].groupingSeparator
local decimalSeparator = Locale["enUS"].decimalSeparator
if number < 0 or number == 0 or not number then
return 0
elseif number > 0 and number < 1000000 then
local t = {}
local int = math.floor(number)
local rest = number % 1
if int == 0 then
t[#t+1] = 0
else
local digits = math.log10(int)
local segments = math.floor(digits / digitGrouping)
local groups = 10 ^ digitGrouping
t[#t+1] = math.floor(int / groups^segments)
for i = segments-1, 0, -1 do
t[#t+1] = groupingSeparator
t[#t+1] = ("%0" .. digitGrouping .. "d"):format(math.floor(int / groups^i) % groups)
end
end
if rest ~= 0 then
t[#t+1] = decimalSeparator
rest = math.floor(rest * 10^6)
while rest % 10 == 0 do
rest = rest / 10
end
t[#t+1] = rest
end
local s = table.concat(t)
wipe(t)
return s
elseif number >= 1000000 and number < 1000000000 then
return format("%.1f|cff93E74F%s|r", number * 0.000001, "m")
elseif number >= 1000000000 then
return format("%.1f|cff93E74F%s|r", number * 0.000000001, "bil")
else
return number
end
end

b = string.match(string.format("%.1f",1/5),"([^0-9])")
a = b=="." and "," or "."

very offtopic: I'm confused by this programming logic/syntax
This is what my logic thinking says it is:

a = true and "something" or "somethingElse"

I saw this kind of stuff before, but never understood it.
Is this from Lua specifically and is there any doc/info about this "logic"?
I mean, normally there is something like:

That's exactly what it means, Ketho. Youre "logic thinking" is correct.

a = b == "." and "," or "."

If a is a decimal, we want b to be a comma, but if b is a comma, then we want a to be a decimal. The number "one million point zero five" would be represented either as "1,000,000.05" or "1.000.000,05" depending on the locale, but no locale uses the same character for both purposes, so if "a" is one, then "b" must be the other.

That said, I've been using the following function to group digits, which is much simpler than some of the solutions posted in this thread:

local function GroupDigits(num)
if not num then return 0 end
if abs(num) < 10000 then return num end
local neg = num < 0 and "-" or ""
local left, mid, right = tostring(abs(num)):match("^([^%d]*%d)(%d*)(.-)$")
return ("%s%s%s%s"):format(neg, left, mid:reverse():gsub("(%d%d%d)", "%1,"):reverse(), right)
end

You could add localization to it thusly:

local decimal = ("%.1f"):format(1/5):match("([^0-9])")
local thousands = decimal == "." and "," or "."
local function GroupDigits(num)
if not num then return 0 end
if abs(num) < 10000 then return num end
local neg = num < 0 and "-" or ""
local left, mid, right = tostring(abs(num)):match("^([^%d]*%d)(%d*)(.-)$")
return ("%s%s%s%s"):format(neg, left, mid:reverse():gsub("(%d%d%d)", "%1" .. thousands):reverse(), right)
end

It only works with whole numbers, but if you're dealing with unit health or pretty much any other numeric value returned by the WoW API, that's all you're getting anyway.

b = string.match(string.format("%.1f",1/5),"([^0-9])")
a = b=="." and "," or "."

very offtopic: I'm confused by this programming logic/syntax
This is what my logic thinking says it is:

a = true and "something" or "somethingElse"

I saw this kind of stuff before, but never understood it.
Is this from Lua specifically and is there any doc/info about this "logic"?
I mean, normally there is something like:

if (a and b) or c then
--doStuff
end

yeah, the boolean stuff in lua is a bit quirky, but pretty nice. i'm with you that it seems odd because i'm used to such logic operations as being either true of false. in lua, they actually retain their value/type.

"a = 5 or 6" results in a = 5
"a = 5 and 6" results in a = 6

Is this from Lua specifically and is there any doc/info about this "logic"?

Apologies for maintaining the derailment but I enjoy this stuff ...

This is Lua's approach to a much older programming concept, the ternary operator which dates back to at least ALGOL (yes, it's older than LISP). Most programmers are more familiar with it when it shows up in C as the "?:" operator. As you already found out, easiest way to deal with it is pretend it's an if-then-else shortcut ...

"if a == true then x = b else x = c" Lua: x = a and b or c C: x = a ? b : c LISP: (setf x (if (a) b c)) ALGOL: x := if a then b else c

Apologies for maintaining the derailment but I enjoy this stuff ...

This is Lua's approach to a much older programming concept, the ternary operator which dates back to at least ALGOL (yes, it's older than LISP). Most programmers are more familiar with it when it shows up in C as the "?:" operator. As you already found out, easiest way to deal with it is pretend it's an if-then-else shortcut ...

"if a == true then x = b else x = c" Lua: x = a and b or c C: x = a ? b : c LISP: (setf x (if (a) b c)) ALGOL: x := if a then b else c

Technically, it's not a ternary operator because you can have n of them; however, it is usually used as a ternary operator.

a = b or c or d or e or f or g or 906 -- will assign the first non-nil/false value in the list it finds to [I]a[/I] or 906 if it finds none.

Basically anything that you can put in an if .. then statement.

Most programmers are more familiar with it when it shows up in C as the "?:" operator. As you already found out, easiest way to deal with it is pretend it's an if-then-else shortcut ...

"if a == true then x = b else x = c" Lua: x = a and b or c C: x = a ? b : c

Be warned: The equivalence falls down when 'b' is false.

If a is a decimal, we want b to be a comma, but if b is a comma, then we want a to be a decimal. The number "one million point zero five" would be represented either as "1,000,000.05" or "1.000.000,05" depending on the locale, but no locale uses the same character for both purposes, so if "a" is one, then "b" must be the other.
[...]

yeah, the boolean stuff in lua is a bit quirky, but pretty nice. i'm with you that it seems odd because i'm used to such logic operations as being either true of false. in lua, they actually retain their value/type.

"a = 5 or 6" results in a = 5
"a = 5 and 6" results in a = 6

[...]
This is Lua's approach to a much older programming concept, the ternary operator which dates back to at least ALGOL (yes, it's older than LISP). Most programmers are more familiar with it when it shows up in C as the "?:" operator. As you already found out, easiest way to deal with it is pretend it's an if-then-else shortcut ...

"if a == true then x = b else x = c" Lua: x = a and b or c
[...]

Besides the linked wiki page, I tried to look it up on the reference manual without success .. found the lua-users wiki page though =)

Thanks for explaining about the "ternary/conditional operator"
and sorry for about continuing derailing the thread :)

Rollback Post to RevisionRollBack

To post a comment, please login or register a new account.

Working on an oUF and I would like to add commas into my numbers, like my health is 42,080 instead of 42080.

I just can't even think of how to search for this or ... all my searches come up with nothing. Would it be very difficult?

Thanks!

12.3k instead of 12,345

of course. i guess localization is still an issue,

File: ScriptEnv.lua

Function: SeparateDigits(number, thousands_separator, decimal_separator)

No need to re-invent the wheel :)

It will take input in the form of (99993341, ',','.') and spit it out as

"99,993,341"

There's a host of other useful formatting functions in there.

I'm not sure of the etiquette - should I ask them before using this bit of code?

In case anyone else is struggling with this, here's code (that I used for a healthbar) that shows commas if my target is under 1 million health, but rounds if he's over. It adds also a green m for millions.

When showing my own health, I don't like it rounded off because I like to see the full numbers when changing out gear. But numbers are far more legible with the commas.

Note that 93E74F is the color value for the "m" and "bil", and can be changed.

And to call it:

be the same thing? It means "if number is less than or equal to 0 or not number then"...

if not number or number <= 0 then

Being picky here, but you don't need to specify the "number > 0" in the first elseif because there's no way it'll get to that statement if the number isn't > 0 :)

very offtopic: I'm confused by this programming logic/syntaxThis is what my logic thinking says it is:

I saw this kind of stuff before, but never understood it.

Is this from Lua specifically and is there any doc/info about this "logic"?

I mean, normally there is something like:

If a is a decimal, we want b to be a comma, but if b is a comma, then we want a to be a decimal. The number "one million point zero five" would be represented either as "1,000,000.05" or "1.000.000,05" depending on the locale, but no locale uses the same character for both purposes, so if "a" is one, then "b" must be the other.

That said, I've been using the following function to group digits, which is much simpler than some of the solutions posted in this thread:

You could add localization to it thusly:

It only works with whole numbers, but if you're dealing with unit health or pretty much any other numeric value returned by the WoW API, that's all you're getting anyway.

yeah, the boolean stuff in lua is a bit quirky, but pretty nice. i'm with you that it seems odd because i'm used to such logic operations as being either true of false. in lua, they actually retain their value/type.

"a = 5 or 6" results in a = 5

"a = 5 and 6" results in a = 6

For formatting output:

http://www.lua.org/manual/5.1/manual.html#pdf-string.format

http://en.wikipedia.org/wiki/Printf#printf_format_placeholders

For matching patterns:

http://www.lua.org/manual/5.1/manual.html#5.4.1

Apologies for maintaining the derailment but I enjoy this stuff...This is Lua's approach to a much older programming concept, the ternary operator which dates back to at least ALGOL (yes, it's older than LISP). Most programmers are more familiar with it when it shows up in C as the "?:" operator. As you already found out, easiest way to deal with it is pretend it's an if-then-else shortcut ...

"if a == true then x = b else x = c"Lua: x = a and b or cC: x = a ? b : cLISP: (setf x (if (a) b c))ALGOL: x := if a then b else cTechnically, it's not a ternary operator because you can have

nof them; however, it isusuallyused as a ternary operator.Basically anything that you can put in an if .. then statement.

Be warned: The equivalence falls down when 'b' is false.

Besides the linked wiki page, I tried to look it up on the reference manual without success .. found the lua-users wiki page though =)

Thanks for explaining about the "ternary/conditional operator"

and sorry for about continuing derailing the thread :)