Skip to content

Instantly share code, notes, and snippets.

@blindman2k
Created August 22, 2014 18:13
Show Gist options
  • Save blindman2k/20328c64606d40da4458 to your computer and use it in GitHub Desktop.
Save blindman2k/20328c64606d40da4458 to your computer and use it in GitHub Desktop.
A handy utility function for formatting a number (integer or float) with a specified number of decimal places and commas at the thousand places.
function number_format(num, decimals=null, separator=",") {
// Fix the decimals
if (decimals == null) {
if (typeof num == "string") decimals = 0;
else if (typeof num == "integer") decimals = 0;
else if (typeof num == "float") decimals = 2;
else return num;
}
// Check we have a number or convert to one if required
if (typeof num == "string") {
if (decimals == 0) num = num.tointeger();
else num = num.tofloat();
} else if (typeof num != "integer" && typeof num != "float") {
return num;
}
// Format the number
local nums = 0;
if (decimals == 0) {
num = format("%0.0f", num.tofloat());
nums = num.len();
} else {
nums = format("%0.0f", num.tofloat()).len();
num = format("%0.0" + decimals + "f", num.tofloat());
}
// Add in the commas
local newnum = "";
for (local i = 0; i < num.len(); i++) {
local ch = num[i];
newnum += ch.tochar();
if (i >= nums-2) {
// We are at the end of the integer part, dump the rest
newnum += num.slice(i+1);
break;
}
if ((nums-i) % 3 == 1) {
// Time for a comma
newnum += separator;
}
}
return newnum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment