Skip to content

Instantly share code, notes, and snippets.

@zbee
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbee/e4516feda2821e78e700 to your computer and use it in GitHub Desktop.
Save zbee/e4516feda2821e78e700 to your computer and use it in GitHub Desktop.
World of Warcraft simple currency formatter

#WoW Currency Formatter When using the battle.net API amounts of currency (like how much money you have, or how much a buyout for an auction item is) are displayed as a simple amount of copper (example: 174056); And that's not human readable.

So, I made a little baby function to format into either g s c or the padded version (adds a 0 in front of silver and copper if it's a single digit).

Hope someone needs this eventually :)


##Examples: wowCur(174009, false); = 74g 40s 9c

wowCur(174009, false, true); = 74g 40s 09c

wowCur(174009, "g", true); = 74g

wowCur(174009, "s", true); = 40s

wowCur(174009, "c", true); = 09c

wowCur(14862094667, false, true, true); = 14,862,094g 46s 67c

wowCur(14862094667, false, true); = 14862094g 46s 67c

function wowCur(amount, type = false, pad = false, nfat = false) {
//Currencies
var g = Math.floor(amount / 1e4); //Gold
var s = Math.floor(amount / 100); //Silver
var c = amount; while (c >= 100) { c -= 100; } //Copper
//Optional Padding
if (pad == true) {
p = "00";
g = "" + g;
g = p.substring(0, p.length - g.length) + g;
s = "" + s;
s = p.substring(0, p.length - s.length) + s;
c = "" + c;
c = p.substring(0, p.length - c.length) + c;
}
//Optional formatting
if (nfat == true) {
g = g.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
//Return formatted result
if (type == "g") {
return g."g";
} elseif (type == "s") {
return s."s";
} elseif (type == "c") {
return c."c";
} else {
a = g."g ".s."s ".c."c";
return $a;
}
}
<?php
function wowCur($amount, $type = false, $pad = false, $nfat = false) {
//Currencies
$g = floor($amount / 1e4); //Gold
$s = floor($amount / 100); while ($s >= 100) { $s = ($s >= 100) ? $s - 100 : $s; } //Silver
$c = $amount; while ($c >= 100) { $c -= 100; } //Copper
//Optional Padding
if ($pad) {
$g = sprintf("%02s", $g);
$s = sprintf("%02s", $s);
$c = sprintf("%02s", $c);
}
//Optional formatting
if ($nfat === true) {
$g = number_format($g, 0);
}
//Returning formatted result
if ($type === "g") {
return $g + "g";
} elseif ($type === "s") {
return $s + "s";
} elseif ($type === "c") {
return $c + "c";
} else {
$a = $g."g ".$s."s ".$c."c";
return $a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment