2006-04-24 06:58:02 +00:00
|
|
|
/*
|
|
|
|
* Helma License Notice
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Helma License
|
|
|
|
* Version 2.0 (the "License"). You may not use this file except in
|
|
|
|
* compliance with the License. A copy of the License is available at
|
|
|
|
* http://adele.helma.org/download/helma/license.txt
|
|
|
|
*
|
|
|
|
* Copyright 1998-2006 Helma Software. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* $RCSfile: Number.js,v $
|
2006-07-18 08:24:59 +00:00
|
|
|
* $Author: czv $
|
|
|
|
* $Revision: 1.2 $
|
|
|
|
* $Date: 2006/04/24 07:02:17 $
|
2006-04-24 06:58:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* factory to create functions for sorting objects in an array
|
|
|
|
* @param String name of the field each object is compared with
|
|
|
|
* @param Number order (ascending or descending)
|
|
|
|
* @return Function ready for use in Array.prototype.sort
|
|
|
|
*/
|
|
|
|
Number.Sorter = function(field, order) {
|
|
|
|
if (!order)
|
|
|
|
order = 1;
|
|
|
|
return function(a, b) {
|
|
|
|
return (a[field] - b[field]) * order;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Number.Sorter.ASC = 1;
|
|
|
|
Number.Sorter.DESC = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* format a Number to a String
|
|
|
|
* @param String Format pattern
|
|
|
|
* @return String Number formatted to a String
|
|
|
|
* FIXME: this might need some localisation
|
|
|
|
*/
|
|
|
|
Number.prototype.format = function(fmt) {
|
|
|
|
var df = fmt ? new java.text.DecimalFormat(fmt)
|
2006-07-18 08:24:59 +00:00
|
|
|
: new java.text.DecimalFormat("###,##0.##");
|
|
|
|
return df.format(0 + this); // addition with 0 prevents exception
|
2006-04-24 06:58:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the percentage of a Number
|
|
|
|
* according to a given total Number
|
|
|
|
* @param Int Total
|
|
|
|
* @param String Format Pattern
|
|
|
|
* @return Int Percentage
|
|
|
|
*/
|
|
|
|
Number.prototype.toPercent = function(total, fmt) {
|
2006-07-18 08:24:59 +00:00
|
|
|
if (!total)
|
|
|
|
return (0).format(fmt);
|
|
|
|
var p = this / (total / 100);
|
2006-04-24 06:58:02 +00:00
|
|
|
return p.format(fmt);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// prevent any newly added properties from being enumerated
|
|
|
|
for (var i in Number)
|
|
|
|
Number.dontEnum(i);
|
|
|
|
for (var i in Number.prototype)
|
|
|
|
Number.prototype.dontEnum(i);
|