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

git-svn-id: http://svn.openlayers.org/trunk/openlayers@5686 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-01-08 18:22:39 +00:00
parent dc77bd5d5f
commit d864adeee0
2 changed files with 85 additions and 0 deletions

View File

@@ -213,6 +213,19 @@ if (!String.prototype.camelize) {
*********************/
OpenLayers.Number = {
/**
* Property: OpenLayers.Number.decimalSeparator
* Decimal separator to use when formatting numbers.
*/
decimalSeparator: ".",
/**
* Property: 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,60 @@ OpenLayers.Number = {
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.
* Default is ",".
* dsep - {String} Decimal separator.
* Default is ".".
*
* 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(".");
if (parts.length == 1 && dec == null) {
// integer where we do not want to touch the decimals
dec = 0;
}
var integer = parts[0];
if (tsep) {
var thousands = /(-?[0-9]+)([0-9]{3})/;
while(thousands.test(integer)) {
integer = integer.replace(thousands, "$1" + tsep + "$2");
}
}
var str;
if (dec == 0) {
str = integer;
} else {
var rem = parts.length > 1 ? parts[1] : "0";
if (dec != null) {
rem = rem + new Array(dec - rem.length + 1).join("0");
}
str = integer + dsep + rem;
}
return str;
}
};

View File

@@ -137,6 +137,24 @@
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
}
function test_Number_format(t) {
t.plan(9);
var format = OpenLayers.Number.format;
t.eq(format(12345), "12,345", "formatting an integer number works");
t.eq(format(12345, 3), "12,345.000", "zero padding an integer works");
t.eq(format(12345, null, ","), "12,345", "adding thousands separator to an integer works");
t.eq(format(12345, 0, ","), "12,345", "adding thousands separator to an integer with defined 0 decimal places works");
var num = 12345.6789
t.eq(format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works");
t.eq(format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works");
t.eq(format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works");
t.eq(format(num, 0, ""), "12346", "empty thousands separator in function call works");
OpenLayers.Number.thousandsSeparator = ".";
OpenLayers.Number.decimalSeparator = ",";
t.eq(format(num, 3), "12.345,679", "changing thousands/decimal separator globally works");
}
function test_07_Function_bind(t) {
t.plan(12);