-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Math.floor() should take a second param to specify decimal place like _.floor #11
Comments
_.floor
I would propose to add this parameter to |
Adding additional arguments to existing methods like those will not work because of existing use cases like |
@zloirock, agree, then the best option would be to introduce another method, like |
That 2nd param reminds me of Python's |
FYI, similar library functions in https://github.com/stdlib-js/stdlib
I also implemented a similar function in my library /**
* Rounds a number to the nearest specified power of ten.
* @param {number} number - The number to round.
* @param {number} powerOfTen - An integer.
* @param {(function(number):number)=} mode - The rounding mode to use (default is `Math.round`).
* @return {number} The rounded number.
*/
function roundToDecimal(number, powerOfTen, mode) {
var FRACTION = 0;
var EXPONENT = 1;
/**
* A decimal arithmetic shift.
* @param {number} value - The number to shift.
* @param {number} offset - The exponent offset.
* @return {number} The shifted number.
*/
function shift(value, offset) {
/* This technique is credited to Lam Wei Li. */
var parts = String(value).split("e");
var exponent = Number(parts[EXPONENT] || "0") + offset;
return Number(parts[FRACTION] + "e" + exponent);
}
mode = mode || Math.round;
return shift(mode(shift(number, -powerOfTen)), powerOfTen);
}
roundToDecimal(3456.3456, -1, Math.ceil); // 3456.4
roundToDecimal(3456.3456, -2, Math.floor); // 3456.34
roundToDecimal(3456.3456, 1, Math.round); // 3460
roundToDecimal(3456.3456, 2, Math.trunc); // 3400 |
I was completely floored when I found out that the current implementation of
Math.floor()
doesn't allow for specifying decimal place like_.floor()
in lodash does. It would be nice to be able to pass in a second param toMath.floor()
to specify decimal place for a little more precision.Example
vs.
The text was updated successfully, but these errors were encountered: