Skip to content
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

Open
JackieCalapristi opened this issue Mar 19, 2018 · 5 comments

Comments

@JackieCalapristi
Copy link

JackieCalapristi commented Mar 19, 2018

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 to Math.floor() to specify decimal place for a little more precision.

Example

Math.floor(.046);
// => 0

vs.

_.floor(0.046, 2);
// => 0.04
@JackieCalapristi JackieCalapristi changed the title Math.floor() should take a second param to specify decimal place like _.floor Math.floor() should take a second param to specify decimal place like _.floor Mar 19, 2018
@micnic
Copy link

micnic commented Jul 20, 2018

I would propose to add this parameter to Math.trunc() instead.

@zloirock
Copy link
Contributor

Adding additional arguments to existing methods like those will not work because of existing use cases like array.map(Math.floor).

@micnic
Copy link

micnic commented Jul 20, 2018

@zloirock, agree, then the best option would be to introduce another method, like Math.toFixed(value, decimals) that would return a number value, naming can be different, not to be confused with Number.prototype.toFixed() that returns a string.

@Rudxain
Copy link

Rudxain commented May 19, 2022

That 2nd param reminds me of Python's round. The only issue I see is that it assumes a constant base of 10, excluding all other bases, so a 3rd base/radix param should be added to make it future-proof, and more versatile in the present. A reason to support different bases is that both Number.prototype.toString and parseInt support it

@Andrew-Cottrell
Copy link

Andrew-Cottrell commented May 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants