OpenLayers OpenLayers

Changeset 5686

Show
Ignore:
Timestamp:
01/08/08 13:22:39 (8 months ago)
Author:
tschaub
Message:

Adding OpenLayers.Number.format for string formatted numbers. Thanks for initiating this Andreas. Nice pairing with you. r=me (closes #1253)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/BaseTypes.js

    r5614 r5686  
    214214 
    215215OpenLayers.Number = { 
     216 
     217    /** 
     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     
    216229    /** 
    217230     * APIFunction: OpenLayers.Number.limitSigDigs 
     
    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        if (parts.length == 1 && dec == null) { 
     278            // integer where we do not want to touch the decimals 
     279            dec = 0; 
     280        } 
     281         
     282        var integer = parts[0]; 
     283        if (tsep) { 
     284            var thousands = /(-?[0-9]+)([0-9]{3})/;  
     285            while(thousands.test(integer)) {  
     286                integer = integer.replace(thousands, "$1" + tsep + "$2");  
     287            } 
     288        } 
     289         
     290        var str; 
     291        if (dec == 0) { 
     292            str = integer; 
     293        } else { 
     294            var rem = parts.length > 1 ? parts[1] : "0"; 
     295            if (dec != null) { 
     296                rem = rem + new Array(dec - rem.length + 1).join("0"); 
     297            } 
     298            str = integer + dsep + rem; 
     299        } 
     300        return str; 
    234301    } 
    235302}; 
  • trunk/openlayers/tests/test_BaseTypes.html

    r5532 r5686  
    137137        t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine"); 
    138138         
     139    } 
     140     
     141    function test_Number_format(t) { 
     142        t.plan(9); 
     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        t.eq(format(12345, null, ","), "12,345", "adding thousands separator to an integer works"); 
     147        t.eq(format(12345, 0, ","), "12,345", "adding thousands separator to an integer with defined 0 decimal places works"); 
     148 
     149        var num = 12345.6789 
     150        t.eq(format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works"); 
     151        t.eq(format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works"); 
     152        t.eq(format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works"); 
     153        t.eq(format(num, 0, ""), "12346", "empty thousands separator in function call works"); 
     154        OpenLayers.Number.thousandsSeparator = "."; 
     155        OpenLayers.Number.decimalSeparator = ","; 
     156        t.eq(format(num, 3), "12.345,679", "changing thousands/decimal separator globally works"); 
    139157    } 
    140158