[OpenLayers-Commits] r5684 - in sandbox/tschaub/scalebar/lib/OpenLayers: . Control
commits at openlayers.org
commits at openlayers.org
Tue Jan 8 11:40:54 EST 2008
Author: tschaub
Date: 2008-01-08 11:40:54 -0500 (Tue, 08 Jan 2008)
New Revision: 5684
Modified:
sandbox/tschaub/scalebar/lib/OpenLayers/BaseTypes.js
sandbox/tschaub/scalebar/lib/OpenLayers/Control/ScaleBar.js
Log:
using OpenLayers.Number.format
Modified: sandbox/tschaub/scalebar/lib/OpenLayers/BaseTypes.js
===================================================================
--- sandbox/tschaub/scalebar/lib/OpenLayers/BaseTypes.js 2008-01-08 15:22:55 UTC (rev 5683)
+++ sandbox/tschaub/scalebar/lib/OpenLayers/BaseTypes.js 2008-01-08 16:40:54 UTC (rev 5684)
@@ -213,7 +213,20 @@
*********************/
OpenLayers.Number = {
+
/**
+ * APIProperty: OpenLayers.Number.decimalSeparator
+ * Decimal separator to use when formatting numbers.
+ */
+ decimalSeparator: ".",
+
+ /**
+ * APIProperty: OpenLayers.Number.thousandsSeparator
+ * Thousands separator to use when formatting numbers.
+ */
+ thousandsSeparator: ",",
+
+ /**
* APIFunction: OpenLayers.Number.limitSigDigs
* Limit the number of significant digits on a float.
*
@@ -231,6 +244,54 @@
fig = parseFloat(num.toPrecision(sig));
}
return fig;
+ },
+
+ /**
+ * APIFunction: OpenLayers.Number.format
+ * Formats a number for output.
+ *
+ * Parameters:
+ * num - {Float}
+ * dec - {Integer} Number of decimal places to round to.
+ * Defaults to 0. Set to null to leave decimal places unchanged.
+ * tsep - {String} Thousands separator.
+ * Defaults to OpenLayers.Number.thousandsSeparator
+ * dsep - {String} Decimal separator.
+ * Defaults to OpenLayers.Number.decimalSeparator
+ *
+ * Returns:
+ * {String} A string representing the formatted number.
+ */
+ format: function(num, dec, tsep, dsep) {
+ dec = (typeof dec != "undefined") ? dec : 0;
+ tsep = (typeof tsep != "undefined") ? tsep :
+ OpenLayers.Number.thousandsSeparator;
+ dsep = (typeof dsep != "undefined") ? dsep :
+ OpenLayers.Number.decimalSeparator;
+
+ if (dec != null) {
+ num = parseFloat(num.toFixed(dec));
+ }
+
+ var parts = num.toString().split(".");
+ var integer = parts[0];
+ if (tsep) {
+ var thousands = /(-?[0-9]+)([0-9]{3})/;
+ while(thousands.test(integer)) {
+ integer = integer.replace(thousands, "$1" + tsep + "$2");
+ }
+ }
+
+ if (dec == 0) {
+ str = integer;
+ } else {
+ var rem = parts.length > 1 ? parts[1] : "0";
+ if (dec) {
+ rem = rem + new Array(dec - rem.length + 1).join("0");
+ }
+ str = integer + dsep + rem;
+ }
+ return str;
}
};
Modified: sandbox/tschaub/scalebar/lib/OpenLayers/Control/ScaleBar.js
===================================================================
--- sandbox/tschaub/scalebar/lib/OpenLayers/Control/ScaleBar.js 2008-01-08 15:22:55 UTC (rev 5683)
+++ sandbox/tschaub/scalebar/lib/OpenLayers/Control/ScaleBar.js 2008-01-08 16:40:54 UTC (rev 5684)
@@ -97,24 +97,18 @@
* element. Default is "scale 1:".
*/
scaleText: "scale 1:",
-
+
/**
* Property: thousandsSeparator
- * {String} Thousands separator. Default is ",". Note that this is not
- * an API-property because it should eventually be moved off of this
- * control to somewhere more useful.
+ * Thousands separator for formatted scale bar measures. The title
+ * attribute for the scale bar always uses
+ * <OpenLayers.Number.thousandsSeparator> for number formatting. To
+ * conserve space on measures displayed with markers, the default
+ * thousands separator for formatting is "" (no separator).
*/
- thousandsSeparator: ",",
+ thousandsSeparator: "",
/**
- * Property: decimalSeparator
- * {String} Deicmal separator. Default is ".". Note that this is not
- * an API-property because it should eventually be moved off of this
- * control to somewhere more useful.
- */
- decimalSeparator: ".",
-
- /**
* Property: measurementProperties
* {Object} Holds display units, abbreviations, and conversion to inches
* (since we're using dpi) per measurement sytem.
@@ -313,7 +307,7 @@
}
this.scale = (scale != undefined) ? scale : this.map.getScale();
// update the element title and width
- this.element.title = this.scaleText + this.formatNumber(this.scale);
+ this.element.title = this.scaleText + OpenLayers.Number.format(this.scale);
this.element.style.width = this.maxWidth + 'px';
// check each measurement unit in the display system
var comp = this.getComp();
@@ -345,8 +339,11 @@
));
// add major measure
if(!this.singleLine) {
- measure = (di == 0) ? 0 : ((di * this.subdivisions) *
- this.subProps.length).toFixed(this.subProps.dec);
+ measure = (di == 0) ? 0 :
+ OpenLayers.Number.format(
+ (di * this.subdivisions) * this.subProps.length,
+ this.subProps.dec, this.thousandsSeparator
+ );
this.numbersContainer.appendChild(this.createElement(
"NumbersBox", measure, xPos - this.dxNumbersBox
));
@@ -390,7 +387,10 @@
"MarkerMajor", " ", xPos - this.dxMarkerMajor
));
// add final measure
- measure = (numDiv * this.subProps.length).toFixed(this.subProps.dec);
+ measure = OpenLayers.Number.format(
+ numDiv * this.subProps.length,
+ this.subProps.dec, this.thousandsSeparator
+ );
if(!this.singleLine) {
this.numbersContainer.appendChild(this.createElement(
"NumbersBox", measure, xPos - this.dxNumbersBox
@@ -584,43 +584,6 @@
// if the key was not found, the equivalent value is zero
return value ? value : 0;
},
-
- /**
- * Method: formatNumber
- * Returns a string formatted number with thousands separators truncated
- * to the given number of decimal places.
- *
- * Parameters:
- * num - {Float} Input number
- * dec - {Integer} Optional number of decimals. Default is 0.
- * tsep - {String} Thousands separator. Default is <thousandsSeparator>.
- * dsep - {String} Decimal separator. Default is <decimalSeparator>.
- *
- * Returns:
- * {String} A formatted number string.
- */
- formatNumber: function(num, dec, tsep, dsep) {
- dec = (dec != undefined) ? dec : 0;
- tsep = (tsep != undefined) ? tsep : this.decimalSeparator;
- dsep = (dsep != undefined) ? dsep : this.thousandsSeparator;
- var str;
- var integer = Math.round(num).toString();
- var thousands = /(-?[0-9]+)([0-9]{3})/;
- while(thousands.test(integer)) {
- integer = integer.replace(thousands, "$1" + tsep + "$2");
- }
- if(dec > 0) {
- var rem = Math.floor(Math.pow(10, dec) * (num - Math.round(num)));
- if(rem == 0) {
- str = integer;
- } else {
- str = integer + dcep + rem;
- }
- } else {
- str = integer;
- }
- return str;
- },
/**
* Method: getHandsomeNumber
More information about the Commits
mailing list