OpenLayers OpenLayers

Ticket #1253: format.patch

File format.patch, 3.7 kB (added by tschaub, 1 year ago)

number format function

  • 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(7); 
     143        var format = OpenLayers.Number.format; 
     144        t.eq(format(12345), "12,345", "formatting an integer number works"); 
     145        t.eq(format(12345, 3), "12,345.000", "zero padding an integer works"); 
     146         
     147        var num = 12345.6789 
     148        t.eq(format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works"); 
     149        t.eq(format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works"); 
     150        t.eq(format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works"); 
     151        t.eq(format(num, 0, ""), "12346", "empty thousands separator in function call works"); 
     152        OpenLayers.Number.thousandsSeparator = "."; 
     153        OpenLayers.Number.decimalSeparator = ","; 
     154        t.eq(format(num, 3), "12.345,679", "changing thousands/decimal separator globally works"); 
     155    } 
    140156 
    141157    function test_07_Function_bind(t) { 
    142158        t.plan(12); 
  • lib/OpenLayers/BaseTypes.js

    old new  
    213213 *********************/ 
    214214 
    215215OpenLayers.Number = { 
     216 
    216217    /** 
     218     * Property: OpenLayers.Number.decimalSeparator 
     219     * Decimal separator to use when formatting numbers. 
     220     */ 
     221    decimalSeparator: ".", 
     222     
     223    /** 
     224     * Property: OpenLayers.Number.thousandsSeparator 
     225     * Thousands separator to use when formatting numbers. 
     226     */ 
     227    thousandsSeparator: ",", 
     228     
     229    /** 
    217230     * APIFunction: OpenLayers.Number.limitSigDigs 
    218231     * Limit the number of significant digits on a float. 
    219232     *  
     
    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     *        Default is ",". 
     259     * dsep - {String} Decimal separator. 
     260     *        Default is ".". 
     261     * 
     262     * Returns: 
     263     * {String} A string representing the formatted number. 
     264     */ 
     265    format: function(num, dec, tsep, dsep) { 
     266        dec = (typeof dec != "undefined") ? dec : 0;  
     267        tsep = (typeof tsep != "undefined") ? tsep : 
     268            OpenLayers.Number.thousandsSeparator;  
     269        dsep = (typeof dsep != "undefined") ? dsep : 
     270            OpenLayers.Number.decimalSeparator; 
     271 
     272        if (dec != null) { 
     273            num = parseFloat(num.toFixed(dec)); 
     274        } 
     275 
     276        var parts = num.toString().split("."); 
     277        var integer = parts[0]; 
     278        if (tsep) { 
     279            var thousands = /(-?[0-9]+)([0-9]{3})/;  
     280            while(thousands.test(integer)) {  
     281                integer = integer.replace(thousands, "$1" + tsep + "$2");  
     282            } 
     283        } 
     284         
     285        if (dec == 0) { 
     286            str = integer; 
     287        } else { 
     288            var rem = parts.length > 1 ? parts[1] : "0"; 
     289            if (dec) { 
     290                rem = rem + new Array(dec - rem.length + 1).join("0"); 
     291            } 
     292            str = integer + dsep + rem; 
     293        } 
     294        return str; 
    234295    } 
    235296}; 
    236297