Thread Tools Display Modes
10-03-10, 01:57 AM   #1
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
formatting issue

I've run into a formatting snag that I hope someone can help with.

lua Code:
  1. function C2C_Convert(c)
  2.     local g = floor(c/10000)
  3.     local s = floor(mod(c/100, 100))
  4.     c = floor(mod(c, 100))
  5.     if c == 0 then c = 00 end
  6.     return g.."g ".. s.."s ".. c.."c"
  7. end
  8.  
  9. function C2C_TotalGold()
  10.     local total = 0
  11.     for k,_ in pairs(pve) do
  12.         if (k ~= "Triumph") and (k ~= "Frost") then -- skip Triumph/Frost since they convert to HP
  13.             local gold = gsub(_G["C2C_"..k.."Result"]:GetText(), "([%a%s]+)", "")  -- nuke the g/s/c tags
  14.             total = total + tonumber(gold)
  15.         end
  16.     end
  17.     C2C_GoldTotal:SetText("Total Gold: ".. C2C_Convert(total))
  18. end

This works as intended, but with one small hitch. When trying to add up gold from badges worth 5.5g, I get:


I know the issue lies in that the gsub takes the text "5g 50s 0c" and formats it to 5500, ending up with missing a zero for the copper. I've tried some formats in the return, but haven't been able to make it show 2 digits instead of 1 if it's a 0...

lua Code:
  1. return g.."g ".. format("%2d", s).."s ".. format("%2d", c).."c"

... and I can't just hard code "00"/eliminate copper because the conversion rate of the silly Justice badges being worth 1g 83s 30c.

Suggestions? (if it's something simple that I should've thought of, feel free to slap me upside the head while you're at it )
  Reply With Quote
10-03-10, 04:33 AM   #2
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
try changing

if c == 0 then c = 00 end

to

if c == 0 then c = "00" end

c = 00 is a number, and is stored as 0 because 00 is really just 0

c= "00" formats it as a string, keeping both 0s in tact

edit::

lua Code:
  1. local gold = gsub(_G["C2C_"..k.."Result"]:GetText(), "([%a%s]+)", "")  -- nuke the g/s/c tags
  2.             total = total + tonumber(gold)
what i see from here is if your fontstring is
5g 50s 00c then your gsub should return 55000

so lets say gold is 55000, now lets run it through your function

[highlight=lua]function C2C_Convert(c) --c is 55000
local g = floor(c/10000) --g is 5
local s = floor(mod(c/100, 100)) --s is 50
c = floor(mod(c, 100)) --c is 0
if c == 0 then c = 00 end --if is true, so set c = 00, and then Lua automatically rounds it to 0, c is 0
return g.."g ".. s.."s ".. c.."c" --returning "5g 50s 0c"
end[highlight]

if we just change

c = "00" instead, we get the return value of "5g 50s 00c" which is the expected value
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>

Last edited by Xubera : 10-03-10 at 04:41 AM.
  Reply With Quote
10-03-10, 09:03 PM   #3
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
Changeline 6 from

return g.."g ".. s.."s ".. c.."c"

to

return format("%dg %02ds %02dc", g, s, c)
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » formatting issue


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