OpenLayers OpenLayers

Ticket #1253: number_format.diff

File number_format.diff, 3.7 kB (added by ahocevar, 1 year ago)
  • lib/OpenLayers/BaseTypes.js

    old new  
    213213 *********************/ 
    214214 
    215215OpenLayers.Number = { 
     216 
     217    /** 
     218     * APIProperty: OpenLayers.Number.decimalSeparator 
     219     * Decimal separator to use when formatting numbers. 
     220     */ 
     221    decimalSeparator: ".", 
     222     
     223    /** 
     224     * APIProperty: OpenLayers.Number.thousandsSeparator 
     225     * Thousands separator to use when formatting numbers. 
     226     */ 
     227    thousandsSeparator: ",", 
     228     
    216229    /** 
    217230     * APIFunction: OpenLayers.Number.limitSigDigs 
    218231     * Limit the number of significant digits on a float. 
     
    231244            fig = parseFloat(num.toPrecision(sig)); 
    232245        } 
    233246        return fig; 
     247    }, 
     248     
     249    /** 
     250     * APIFunction: OpenLayers.Number.format 
     251     * Formats a number for output. 
     252     *  
     253     * Parameters: 
     254     * num  - {Float} 
     255     * dec  - {Integer} Number of decimal places to round to. 
     256     *        Defaults to 0. Set to null to leave decimal places unchanged. 
     257     * tsep - {String} Thousands separator. 
     258     *        Defaults to OpenLayers.Number.thousandsSeparator 
     259     * dsep - {String} Decimal separator. 
     260     *        Defaults to OpenLayers.Number.decimalSeparator 
     261     */ 
     262    format: function(num, dec, tsep, dsep) { 
     263        dec = (typeof dec != "undefined") ? dec : 0;  
     264        tsep = (typeof tsep != "undefined") ? tsep : 
     265            OpenLayers.Number.thousandsSeparator;  
     266        dsep = (typeof dsep != "undefined") ? dsep : 
     267            OpenLayers.Number.decimalSeparator; 
     268 
     269        if (dec != null) { 
     270            num = Math.round(num*Math.pow(10, dec)) / Math.pow(10, dec); 
     271        } 
     272 
     273        var splittedNum = num.toString().split("."); 
     274        var integer = splittedNum[0]; 
     275        if (tsep) { 
     276            var thousands = /(-?[0-9]+)([0-9]{3})/;  
     277            while(thousands.test(integer)) {  
     278                integer = integer.replace(thousands, "$1" + tsep + "$2");  
     279            } 
     280        } 
     281         
     282        if (dec == 0) { 
     283            return integer; 
     284        } else { 
     285            var rem = splittedNum.length > 0 ? splittedNum[1] : "0"; 
     286            if (dec) { 
     287                rem = rem + new Array(dec - rem.length + 1).join("0"); 
     288            } 
     289            return integer + dsep + rem; 
     290        } 
    234291    } 
    235292}; 
    236293 
  • tests/test_BaseTypes.html

    old new  
    137137        t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine"); 
    138138         
    139139    } 
     140     
     141    function test_Number_format(t) { 
     142        t.plan(6); 
     143         
     144        t.eq(OpenLayers.Number.format(12345), "12,345", "formatting an integer number works"); 
     145         
     146        var num = 12345.6789 
     147        t.eq(OpenLayers.Number.format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works"); 
     148        t.eq(OpenLayers.Number.format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works"); 
     149        t.eq(OpenLayers.Number.format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works"); 
     150        t.eq(OpenLayers.Number.format(num, 0, ""), "12346", "empty thousands separator in function call works"); 
     151        OpenLayers.Number.thousandsSeparator = "."; 
     152        OpenLayers.Number.decimalSeparator = ","; 
     153        t.eq(OpenLayers.Number.format(num, 3), "12.345,679", "changing thousands/decimal separator globally works"); 
     154    } 
    140155 
    141156    function test_07_Function_bind(t) { 
    142157        t.plan(12);