Ticket #1253: number_format.diff
| File number_format.diff, 3.7 kB (added by ahocevar, 1 year ago) |
|---|
-
lib/OpenLayers/BaseTypes.js
old new 213 213 *********************/ 214 214 215 215 OpenLayers.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 216 229 /** 217 230 * APIFunction: OpenLayers.Number.limitSigDigs 218 231 * Limit the number of significant digits on a float. … … 231 244 fig = parseFloat(num.toPrecision(sig)); 232 245 } 233 246 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 format: function(num, dec, tsep, dsep) { 263 dec = (typeof dec != "undefined") ? dec : 0; 264 tsep = (typeof tsep != "undefined") ? tsep : 265 OpenLayers.Number.thousandsSeparator; 266 dsep = (typeof dsep != "undefined") ? dsep : 267 OpenLayers.Number.decimalSeparator; 268 269 if (dec != null) { 270 num = Math.round(num*Math.pow(10, dec)) / Math.pow(10, dec); 271 } 272 273 var splittedNum = num.toString().split("."); 274 var integer = splittedNum[0]; 275 if (tsep) { 276 var thousands = /(-?[0-9]+)([0-9]{3})/; 277 while(thousands.test(integer)) { 278 integer = integer.replace(thousands, "$1" + tsep + "$2"); 279 } 280 } 281 282 if (dec == 0) { 283 return integer; 284 } else { 285 var rem = splittedNum.length > 0 ? splittedNum[1] : "0"; 286 if (dec) { 287 rem = rem + new Array(dec - rem.length + 1).join("0"); 288 } 289 return integer + dsep + rem; 290 } 234 291 } 235 292 }; 236 293 -
tests/test_BaseTypes.html
old new 137 137 t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine"); 138 138 139 139 } 140 141 function test_Number_format(t) { 142 t.plan(6); 143 144 t.eq(OpenLayers.Number.format(12345), "12,345", "formatting an integer number works"); 145 146 var num = 12345.6789 147 t.eq(OpenLayers.Number.format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works"); 148 t.eq(OpenLayers.Number.format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works"); 149 t.eq(OpenLayers.Number.format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works"); 150 t.eq(OpenLayers.Number.format(num, 0, ""), "12346", "empty thousands separator in function call works"); 151 OpenLayers.Number.thousandsSeparator = "."; 152 OpenLayers.Number.decimalSeparator = ","; 153 t.eq(OpenLayers.Number.format(num, 3), "12.345,679", "changing thousands/decimal separator globally works"); 154 } 140 155 141 156 function test_07_Function_bind(t) { 142 157 t.plan(12);
