Math Help - Page 3 - WoWInterface
01-27-12, 09:48 PM   #41
Seerah
Fishing Trainer

Join Date: Oct 2006
Posts: 10,671
 Originally Posted by Grimsin Doesnt flooring cause it to round down? Hmm and lets say it does not... somethings wrong somewhere because my end result is always off by a copper...
Dropping the decimal is the same thing as rounding down.
__________________
"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

01-28-12, 05:10 AM   #42
Xubera
A Cobalt Mageweaver

Join Date: May 2009
Posts: 207
Code:
```local repairCost = x
repairCost = repairCost - discounts
print(format("%dG %2dS %2dC",
repairCost/COPPER_PER_GOLD,
math.mod(repairCost/COPPER_PER_SILVER, 100),
math.mod(repairCost, 100)))```
setting repairCost to 1234567.5444444 (123G, 45S, and 67.5444444 copper) outputs
"123G 45S 67C"

edit:: guess i will step through the code.
assuming repairCost is 1234567.5444444 and discounts is 0.

format("%dG %02dS %02dC" ...
-Basically, for every tag you have in your format string, you need to put a variable in the following parameters. so for every %d, it expects one parameter that can evaluate to an integer. Because I have 3 %d's, then I have 3 parameters following that string. The %02d tells the format command that the maximum amount of digits is 2. The 0 tells the command that if the amount of digits is less than 2, then pad the number with 0s. simply replace the %02d with %2d if you do not want to pad the numbers with a 0.

repairCost/COPPER_PER_GOLD
-1234567.544444 / 10000 = 123.4567544444

math.mod(repairCost/COPPER_PER_SILVER, 100),
-1234567.544444 / 100 = 12345.675444444
-12345.675444444 % 100 = 45.675444444 (modulos is the remainder. so 100 can go into 12345, 123 times equally with a remainder of 45. another example might be 5 % 3 = 2. saying that 3 can be divided into 5 only once equally, and has a remainder of 2.)

math.mod(repairCost, 100)
- same as above...
-1234567.544444 % 100 is 67.544444 because the remainder of 1234567.544444/100 is 67.544444.

And remember that %d asks for a integer, so it drops the decimal automatically.

Last edited by Xubera : 01-28-12 at 05:22 AM. Reason: walkthrough

01-29-12, 11:27 AM   #43
Grimsin
A Molten Giant

Join Date: Sep 2006
Posts: 990
btw i found this function on wowwiki. It worked perfectly.

Code:
```function truncate(number, decimals)
return number - (number % (0.1 ^ decimals))
end```
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]

01-29-12, 10:36 PM   #44
Torhal
A Pyroguard Emberseer

Join Date: Aug 2008
Posts: 1,196
Don't you mean WoWPedia?
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.

01-30-12, 06:43 AM   #45
A Molten Giant

Join Date: Dec 2005
Posts: 792
 Originally Posted by Grimsin btw i found this function on wowwiki. It worked perfectly. Code: ```function truncate(number, decimals) return number - (number % (0.1 ^ decimals)) end```
I think you can even use this function to truncate, just don't ask about performance comparison between these two -the one here is probably milliseconds slower:
Code:
```function truncate(number, decimals)
return format("%."..decimals.."f", number)
end```
 Originally Posted by Torhal Don't you mean WoWPedia?
Probably, not all noticed the change, that the old wowwiki team left and made wowpedia under curse, many still use wowwiki because tbh, the transition was somewhat ninja-like...

01-30-12, 12:27 PM   #46
SDPhantom
A Pyroguard Emberseer

Join Date: Jul 2006
Posts: 1,851
 Originally Posted by Grimsin btw i found this function on wowwiki. It worked perfectly. Code: ```function truncate(number, decimals) return number - (number % (0.1 ^ decimals)) end```
I think you can even use this function to truncate, just don't ask about performance comparison between these two -the one here is probably milliseconds slower:
Code:
```function truncate(number, decimals)
return format("%."..decimals.."f", number)
end```
More of the difference would be if you actually wanted a number returned or a string. Also string.format() rounds numbers to whatever decimal place you specify. It doesn't actually truncate them.

 Originally Posted by Torhal Don't you mean WoWPedia?
Probably, not all noticed the change, that the old wowwiki team left and made wowpedia under curse, many still use wowwiki because tbh, the transition was somewhat ninja-like...
When WoWWiki joined Wikia.com, they had a problem with the site format Wikia was forcing on them, so they split off into WoWPedia. Both sites are still actively maintained.
__________________
 "All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools." -Anders (Dragon Age: Origins - Awakening)

01-30-12, 01:05 PM   #47
A Molten Giant

Join Date: Dec 2005
Posts: 792
 Originally Posted by SDPhantom More of the difference would be if you actually wanted a number returned or a string. Also string.format() rounds numbers to whatever decimal place you specify. It doesn't actually truncate them.
Aye, it rounds, doesn't truncate... silly me!

 Originally Posted by SDPhantom When WoWWiki joined Wikia.com, they had a problem with the site format Wikia was forcing on them, so they split off into WoWPedia. Both sites are still actively maintained.
That's true, but if the management left to wowpedia why did users stay on wowwiki? I mean, they don't allow custom tooltip modules and such and I think you need certain freedom when you wish to tailor a site for a specific game or what ever.

01-30-12, 02:15 PM   #48
SDPhantom
A Pyroguard Emberseer

Join Date: Jul 2006
Posts: 1,851