Code Monkey home page Code Monkey logo

Comments (5)

zloirock avatar zloirock commented on July 20, 2024 4

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

from proposal-math-extensions.

micnic avatar micnic commented on July 20, 2024 1

@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.

from proposal-math-extensions.

Andrew-Cottrell avatar Andrew-Cottrell commented on July 20, 2024 1

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

from proposal-math-extensions.

micnic avatar micnic commented on July 20, 2024

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

from proposal-math-extensions.

Rudxain avatar Rudxain commented on July 20, 2024

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

from proposal-math-extensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.