/* 
 * More info at: http://phpjs.org
 * 
 * This is version: unknown
 * php.js is copyright 2009 Kevin van Zonneveld.
 * 
 * Portions copyright Kevin van Zonneveld (http://kevin.vanzonneveld.net),
 * Brett Zamir, Onno Marsman, Michael White (http://getsprink.com), Waldo
 * Malqui Silva, Paulo Ricardo F. Santos, Jack, Jonas Raoni Soares Silva
 * (http://www.jsfromhell.com), Philip Peterson, Legaev Andrey, Ates Goral
 * (http://magnetiq.com), Martijn Wieringa, Nate, Enrique Gonzalez, Philippe
 * Baumann, Webtoolkit.info (http://www.webtoolkit.info/), Jani Hartikainen,
 * Ash Searle (http://hexmen.com/blog/), Carlos R. L. Rodrigues
 * (http://www.jsfromhell.com), Johnny Mast (http://www.phpvrouwen.nl), GeekFG
 * (http://geekfg.blogspot.com), d3x, Erkekjetter, marrtins, mdsjack
 * (http://www.mdsjack.bo.it), Karol Kowalski, Alex, Arpad Ray
 * (mailto:arpad@php.net), Steven Levithan (http://blog.stevenlevithan.com),
 * Mirek Slugen, Public Domain (http://www.json.org/json2.js), Marc Palau,
 * Tyler Akins (http://rumkin.com), AJ, Sakimori, Alfonso Jimenez
 * (http://www.alfonsojimenez.com), Thunder.m, gorthaur, Aman Gupta, Steve
 * Hilder, Der Simon (http://innerdom.sourceforge.net/), Francesco, echo is
 * bad, LH, Paul, J A R, Steve Clay, David James, noname, T. Wild, Douglas
 * Crockford (http://javascript.crockford.com), Hyam Singer
 * (http://www.impact-computing.com/), kenneth, ger, john
 * (http://www.jd-tech.net), T0bsn, XoraX (http://www.xorax.info), Gilbert,
 * duncan, Bayron Guevara, Lincoln Ramsay, Subhasis Deb, Andrea Giammarchi
 * (http://webreflection.blogspot.com), sankai, Sanjoy Roy, 0m3r, Felix
 * Geisendoerfer (http://www.debuggable.com/felix), djmix, Linuxworld, Tim
 * Wiel, Bryan Elliott, David Randall, Ozh, Brad Touesnard, MeEtc
 * (http://yass.meetcweb.com), Thiago Mata (http://thiagomata.blog.com), Jon
 * Hohle, Pyerre, Peter-Paul Koch (http://www.quirksmode.org/js/beat.html),
 * marc andreu, Slawomir Kaniecki, nobbler, rezna, Pul, Mick@el, Kirk
 * Strobeck, Pierre-Luc Paour, Martin Pool, Luke Godfrey, Anton Ongson,
 * Andreas, Garagoth, Christian Doebler, penutbutterjelly, Simon Willison
 * (http://simonwillison.net), Gabriel Paderni, Kristof Coomans (SCK-CEN
 * Belgian Nucleair Research Centre), Saulo Vallory, sowberry, hitwork, Yves
 * Sucaet, johnrembo, Nick Callen, ejsanders, Norman "zEh" Fuchs, dptr1988,
 * Caio Ariede (http://caioariede.com), Valentina De Rosa, T.Wild, metjay,
 * Pedro Tainha (http://www.pedrotainha.com), DxGx, Alexander Ermolaev
 * (http://snippets.dzone.com/user/AlexanderErmolaev), Andrej Pavlovic, Dino,
 * Manish, Cord, Matt Bradley, Robin, taith, David, Victor, stensi, Arno,
 * Nathan, Mateusz "loonquawl" Zalega, ReverseSyntax, Jalal Berrami, Francois,
 * Scott Cariss, FremyCompany, Tod Gentille, Luke Smith
 * (http://lucassmith.name), Diogo Resende, Cagri Ekin, booeyOH, Leslie Hoare,
 * Ben Bryan, Howard Yeend, Allan Jensen (http://www.winternet.no),
 * FGFEmperor, baris ozdil, gabriel paderni, Yannoo, Benjamin Lupton, Atli
 * Þór, jakes
 * 
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */ 


function number_format( number, decimals, dec_point, thousands_sep ) {
    // Formats a number with grouped thousands  
    // 
    // version: 902.223
    // discuss at: http://phpjs.org/functions/number_format

    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    var n = number, prec = decimals, dec = dec_point, sep = thousands_sep;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    sep = sep == undefined ? ',' : sep;

    var s = n.toFixed(prec),
        abs = Math.abs(n).toFixed(prec),
        _, i;

    if (abs > 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');

        s = _.join(dec || '.');
    } else {
        s = abs.replace('.', dec_point);
    }

    return s;
}

