-1

Whatever language you choose, you may encounter a rounding problem. In fact, this is due to the limit of the required number of bits needed to get the right number avec a calculation.

Simple example:

enter image description here

In javascript (like any other language), you may want to use a function to round the result after even a simple sum (with decimal) :

enter image description here

enter image description here

Unfortunately it's not always working as we expect.

enter image description here

That's why I created my own function which is based on the string. I read every char and make the round by myself so I'm not limited by the number of bit.

round(number, decimalsLength) {

    // If the number does contains a scientific notation, we use a more traditionnal calculation method
    if(number.toString().includes("e"))
        number = parseFloat(number.toFixed(4));

    // Split the integer part and the decimal part
    var number = (typeof number === "number") ? number : (number != null) ? parseFloat(number) : 0; // Return NaN for [null; {}]
    var decimalsLength = decimalsLength || 0;
    var array = (number+"").split(".");
    var left = array[0];
    var right = "1"+(array[1] || 0); // We add 1 to keep the left 0

    // Round only if we have more decimal than needed
    if(right.length-1 >= decimalsLength) {

        // Round up
        if(parseInt(right.substr(decimalsLength+1, 1)) >= 5)
            right = parseInt(right.substr(0, decimalsLength+1))+1;

        // If we moved to the upper integer
        if(right.toString().substr(0, 1) == 2) { // If the added 1 has became a 2
            var noDecimal = parseInt(right.toString().substr(1, decimalsLength)) == 0; // Return an integer if we no longer have any decimal
            left = (parseInt(left) + (number > 0 ? 1 : -1)).toString();
            return parseFloat(left + (noDecimal ?"":".") + (noDecimal?"":right.toString().substr(1, decimalsLength)));
        }
    }

    // Else, we don't need to round
    var noDecimal = parseInt(right.toString().substr(1, decimalsLength)) == 0;
    return parseFloat(left + (noDecimal?"":".") + (noDecimal?"":right.toString().substr(1, decimalsLength)));
}

I wanted to share this with you in case it can help, but also to have a feedback and why not improve the function if you find it useful.

  • 3
    Please visit the [help], take the [tour] to see what and [ask]. If you want, you can ask a specific question and answer it yourself, but this is not Github... – mplungjan Aug 21 '23 at 12:54
  • 2
    This is not how stackoverflow works... – epascarello Aug 21 '23 at 12:56
  • 2
    Try posting this question @ https://codereview.stackexchange.com/ – KooiInc Aug 21 '23 at 12:59
  • 1
    If you want to give an answer to your own question, read [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – evolutionxbox Aug 21 '23 at 13:02
  • 2
    Before you post at [codereview.se], be sure to read [A guide to Code Review for Stack Overflow users](//codereview.meta.stackexchange.com/a/5778), as some things are done differently over there - e.g. question titles should simply say what the code *is for*, as the question is always, "How can I improve this?". Be sure that the code works correctly; include your unit tests if possible. You'll likely get some suggestions on making it more efficient, easier to read, and better tested. – Toby Speight Aug 21 '23 at 13:03
  • Thanks for taking your time to give advices about how to post. It's not so easy for a non-english person. I read all your links and posted on Code Review. Answer our own question may also be usefull someday. – Sébastien Josserand Aug 21 '23 at 20:48

0 Answers0