[javascript] How to format numbers?

Let me also throw my solution in here. I've commented each line for ease of reading and also provided some examples, so it may look big.

_x000D_
_x000D_
function format(number) {_x000D_
_x000D_
    var decimalSeparator = ".";_x000D_
    var thousandSeparator = ",";_x000D_
_x000D_
    // make sure we have a string_x000D_
    var result = String(number);_x000D_
_x000D_
    // split the number in the integer and decimals, if any_x000D_
    var parts = result.split(decimalSeparator);_x000D_
_x000D_
    // if we don't have decimals, add .00_x000D_
    if (!parts[1]) {_x000D_
      parts[1] = "00";_x000D_
    }_x000D_
  _x000D_
    // reverse the string (1719 becomes 9171)_x000D_
    result = parts[0].split("").reverse().join("");_x000D_
_x000D_
    // add thousand separator each 3 characters, except at the end of the string_x000D_
    result = result.replace(/(\d{3}(?!$))/g, "$1" + thousandSeparator);_x000D_
_x000D_
    // reverse back the integer and replace the original integer_x000D_
    parts[0] = result.split("").reverse().join("");_x000D_
_x000D_
    // recombine integer with decimals_x000D_
    return parts.join(decimalSeparator);_x000D_
}_x000D_
_x000D_
document.write("10 => " + format(10) + "<br/>");_x000D_
document.write("100 => " + format(100) + "<br/>");_x000D_
document.write("1000 => " + format(1000) + "<br/>");_x000D_
document.write("10000 => " + format(10000) + "<br/>");_x000D_
document.write("100000 => " + format(100000) + "<br/>");_x000D_
document.write("100000.22 => " + format(100000.22) + "<br/>");
_x000D_
_x000D_
_x000D_