Rounding Numbers Failing

Posted on
  • I have added a round function to my code libs to round numbers to a certain number of decimals. It looks roughly as follows;

    const round = (numberToRound,decimalPlaces = 2) => (e => Math.round(numberToRound * e) / e)(Math.pow(10,decimalPlaces ));
    

    expected outcome:

    round(6800971.33999999985) = 6800971.34
    

    This passes all local unit tests compiling in es5, es6 and esnext. It also works fine on a couple of online javascript compilers I tested (codepen, Typescript playground) - and its exact copy of code others have used for doing the same thing.

    If I run this code on the bangle emulator or the bangle2 watch directly I get "NaN" returned.

  • It breaks because Espruino doesn't support default parameter values, round(6800971.33999999985, 2) works though.

  • Thanks - it is slightly concerning that it doesn't cause an error though.

    It seems that in Espruino, a=3 actually returned a reference to a when really it should have returned 3. So (a=2)=3 was possible when it shouldn't be according to the spec - I've just fixed this for cutting edge builds.

  • Thanks for the feature link. Shame no1 linked to it (and I wasn't able to find it) when I was asking about which version of javascript Espruino is based on. Maybe I was looking on bangle documentation only.

    My code snippet is abit miss leading as its from a typescript file not a direct javascript file. I am compiling it down to es5 when building. I should probably check what typescript is doing with default params when the target output is es5.

    The main concern is as Gordon says, not that it isn't supported, but that no error is fired.

  • Shame no1 linked to it (and I wasn't able to find it) when I was asking about which version of javascript Espruino is based on.

    You got that link right here https://forum.espruino.com/conversationsĀ­/388506/#17058616 when you asked :-)

  • :) yes - literally second line.

    Also, if you give us some idea of what search words you'd likely search for on the Espruino site to find the info then I can add those to the keywords list for the page so it comes up for more people.

    Same for any page really - I pretty much never get any PRs for this, but if there's a page that you found that didn't come up when you searched, there's a link at the bottom of the page that goes to GitHub (eg https://github.com/espruino/EspruinoDocsĀ­/blob/master/info/Features.md) and you can add to the KEYWORDS list at the top of the file and do a PR

  • yeh, my mistake. Clearly I am terrible at reading!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Rounding Numbers Failing

Posted by Avatar for charlie @charlie

Actions