mirror of
https://github.com/transmission/transmission.git
synced 2026-04-25 03:10:31 +01:00
#3298:Prettier formating of percentages
This commit is contained in:
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user