Skip to content

Commit

Permalink
added fit function to Number prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
juzna committed Jan 13, 2011
1 parent 33ed965 commit 07fd412
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/prototype/lang/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ Object.extend(Number.prototype, (function() {
return Math.floor(this);
}

/**
* Number#floor() -> Number
*
* Fit number to boundaries
*
* Example:
* 5.fit(1,10) -> 5
* 20.fit(1,10) -> 10
**/
function fit(min, max) {
if(min >= max) return this;
else {
if(this > max) return max;
else if(this < min) return min;
else return this;
}
}

return {
toColorPart: toColorPart,
succ: succ,
Expand All @@ -153,6 +171,7 @@ Object.extend(Number.prototype, (function() {
abs: abs,
round: round,
ceil: ceil,
floor: floor
floor: floor,
fit: fit
};
})());

0 comments on commit 07fd412

Please sign in to comment.