#3298:Prettier formating of percentages

This commit is contained in:
Daniel Lee
2010-06-22 22:30:58 +00:00
parent 7e9ba59807
commit bbe9e9843a
15 changed files with 169 additions and 96 deletions

View File

@@ -116,28 +116,43 @@ function setInnerHTML( e, html )
* @returns float
*/
Math.roundWithPrecision = function(floatnum, precision) {
return Math.round ( floatnum * Math.pow ( 10, precision ) ) / Math.pow ( 10, precision );
return Math.round( floatnum * Math.pow ( 10, precision ) ) / Math.pow( 10, precision );
};
/*
* Given a numerator and denominator, return a ratio string
*/
Math.ratio = function( numerator, denominator )
{
Math.ratio = function( numerator, denominator ) {
var result = Math.floor(100 * numerator / denominator) / 100;
// check for special cases
if (isNaN(result)) result = 0;
if (result=="Infinity") result = "∞";
// Add the decimals if this is an integer
if ((result % 1) == 0)
result = result + '.00';
if(result==Number.POSITIVE_INFINITY || result==Number.NEGATIVE_INFINITY) result = -2;
else if(isNaN(result)) result = -1;
return result;
};
/*
* Truncate a float to a specified number of decimal
* places, stripping trailing zeroes
*
* @param float floatnum
* @param integer precision
* @returns float
*/
Math.truncateWithPrecision = function(floatnum, precision) {
return Math.floor( floatnum * Math.pow ( 10, precision ) ) / Math.pow( 10, precision );
};
/*
* Round a string of a number to a specified number of decimal
* places
*/
Number.prototype.toTruncFixed = function( place ) {
var ret = Math.truncateWithPrecision( this, place );
return ret.toFixed( place );
}
/*
* Trim whitespace from a string
*/