OpenLayers OpenLayers

Changeset 5684

Show
Ignore:
Timestamp:
01/08/08 11:40:54 (1 year ago)
Author:
tschaub
Message:

using OpenLayers.Number.format

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/tschaub/scalebar/lib/OpenLayers/BaseTypes.js

    r5614 r5684  
    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 
     
    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     * 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}; 
  • sandbox/tschaub/scalebar/lib/OpenLayers/Control/ScaleBar.js

    r5677 r5684  
    9898     */ 
    9999    scaleText: "scale 1:", 
    100  
     100     
    101101    /** 
    102102     * Property: thousandsSeparator 
    103      * {String} Thousands separator.  Default is ",".  Note that this is not 
    104      *     an API-property because it should eventually be moved off of this 
    105      *     control to somewhere more useful. 
    106      */ 
    107     thousandsSeparator: ",", 
    108  
    109     /** 
    110      * Property: decimalSeparator 
    111      * {String} Deicmal separator.  Default is ".".  Note that this is not 
    112      *     an API-property because it should eventually be moved off of this 
    113      *     control to somewhere more useful. 
    114      */ 
    115     decimalSeparator: ".", 
     103     * Thousands separator for formatted scale bar measures.  The title 
     104     *     attribute for the scale bar always uses 
     105     *     <OpenLayers.Number.thousandsSeparator> for number formatting.  To 
     106     *     conserve space on measures displayed with markers, the default 
     107     *     thousands separator for formatting is "" (no separator). 
     108     */ 
     109    thousandsSeparator: "", 
    116110 
    117111    /** 
     
    314308        this.scale = (scale != undefined) ? scale : this.map.getScale(); 
    315309        // update the element title and width 
    316         this.element.title = this.scaleText + this.formatNumber(this.scale); 
     310        this.element.title = this.scaleText + OpenLayers.Number.format(this.scale); 
    317311        this.element.style.width = this.maxWidth + 'px'; 
    318312        // check each measurement unit in the display system 
     
    346340            // add major measure 
    347341            if(!this.singleLine) { 
    348                 measure = (di == 0) ? 0 : ((di * this.subdivisions) * 
    349                     this.subProps.length).toFixed(this.subProps.dec); 
     342                measure = (di == 0) ? 0 : 
     343                    OpenLayers.Number.format( 
     344                        (di * this.subdivisions) * this.subProps.length, 
     345                        this.subProps.dec, this.thousandsSeparator 
     346                    ); 
    350347                this.numbersContainer.appendChild(this.createElement( 
    351348                    "NumbersBox", measure, xPos - this.dxNumbersBox 
     
    391388        )); 
    392389        // add final measure 
    393         measure = (numDiv * this.subProps.length).toFixed(this.subProps.dec); 
     390        measure = OpenLayers.Number.format( 
     391            numDiv * this.subProps.length, 
     392            this.subProps.dec, this.thousandsSeparator 
     393        ); 
    394394        if(!this.singleLine) { 
    395395            this.numbersContainer.appendChild(this.createElement( 
     
    585585        return value ? value : 0; 
    586586    }, 
    587      
    588     /** 
    589      * Method: formatNumber 
    590      * Returns a string formatted number with thousands separators truncated 
    591      *     to the given number of decimal places. 
    592      * 
    593      * Parameters: 
    594      * num - {Float} Input number 
    595      * dec - {Integer} Optional number of decimals.  Default is 0. 
    596      * tsep - {String} Thousands separator.  Default is <thousandsSeparator>. 
    597      * dsep - {String} Decimal separator.  Default is <decimalSeparator>. 
    598      * 
    599      * Returns: 
    600      * {String} A formatted number string. 
    601      */ 
    602     formatNumber: function(num, dec, tsep, dsep) { 
    603         dec = (dec != undefined) ? dec : 0; 
    604         tsep = (tsep != undefined) ? tsep : this.decimalSeparator; 
    605         dsep = (dsep != undefined) ? dsep : this.thousandsSeparator; 
    606         var str; 
    607         var integer = Math.round(num).toString(); 
    608         var thousands = /(-?[0-9]+)([0-9]{3})/; 
    609         while(thousands.test(integer)) { 
    610             integer = integer.replace(thousands, "$1" + tsep + "$2"); 
    611         } 
    612         if(dec > 0) { 
    613             var rem = Math.floor(Math.pow(10, dec) * (num - Math.round(num))); 
    614             if(rem == 0) { 
    615                 str = integer; 
    616             } else { 
    617                 str = integer + dcep + rem; 
    618             } 
    619         } else { 
    620             str = integer; 
    621         } 
    622         return str; 
    623     }, 
    624587 
    625588    /**