/**
 * Format a number
 *
 * @param {Number} num        The number to format
 * @param {Number} precision  Precision
 * @param {String} decimal    Decimal place operator
 * @param {String} thousand   Thousand place operator
 * @returns {String} Formatted number
 */
function formatNumber(num, precision, decimal, thousand) {
    "use strict";

    var sign, intVal, remain, final;

    precision = isNaN(precision = Math.abs(precision)) ? 2 : precision;
    decimal = decimal === undefined ? "." : decimal;
    thousand = thousand === undefined ? "," : thousand;

    sign = num < 0 ? "-" : "";
    intVal = parseInt(num = Math.abs(+num || 0).toFixed(precision), 10) + "";
    remain = (remain = intVal.length) > 3 ? remain % 3 : 0;

	final = sign;

    //the "prefix" numbers, before the first thousand operator
    final += remain ? intVal.substr(0, remain) + thousand : ""; 

    //place thousand operators every 3rd char
    final += intVal.substr(remain).replace(/(\d{3})(?=\d)/g, "$1" + thousand); 

    // set the correct decimal place operator + precision
    final += precision ? decimal + Math.abs(num - intVal).toFixed(precision).slice(2) : ""; 
    
    return final; 
}