• Mon 2019.08.26

    Now running on sheer insanity, and a will and determination to find an acceptable answer, found a jsFiddle solution that MDN recommends. A Closure that wraps the Math.round() function.

    Interactive

    http://jsfiddle.net/cCX5y/2/


    Tried that out in the Chrome browser with it's own with embedded revealed steps

    1.005     p2
    inside Math.round10(value, exp); val=1.005 exp=-2
    inside decimalAdjust() type=round val=1.005 exp=-2
    inside decimalAdjust() shift val=1.005
    inside decimalAdjust() shift val=101
    inside decimalAdjust() shift back val=101
    inside decimalAdjust() shift back ret=1.01
    inside Math.round10(value, exp); decimalAdjust() ret=1.01
    Math.round10(1.005, -2); = 1.01
    
    
    
    1234.505     p2
    inside Math.round10(value, exp); val=1234.505 exp=-2
    inside decimalAdjust() type=round val=1234.505 exp=-2
    inside decimalAdjust() shift val=1234.505
    inside decimalAdjust() shift val=123451
    inside decimalAdjust() shift back val=123451
    inside decimalAdjust() shift back ret=1234.51
    inside Math.round10(value, exp); decimalAdjust() ret=1234.51
    Math.round10(1234.505, -2); = 1234.51
    
    



    So we can confirm the confirmed MDN rounding of 1.005 now works in the browser.


    Now we have to be on the right track track this time, Right?

    Test using Espruino WebIDE

    1.005    p2
      inside  Math.round10(value, exp);   val=1.005  exp=-2
      inside decimalAdjust() type=round  val=1.005  exp=-2
      inside decimalAdjust() shift  val=1.005
      inside decimalAdjust() shift  val=100
      inside decimalAdjust() shift back  val=100
      inside decimalAdjust() shift back  ret=1
      inside Math.round10(value, exp);  decimalAdjust() ret=1
      Math.round10(1.005, -2);  = 1
    
    
    1234.505    p2
      inside  Math.round10(value, exp);   val=1234.505  exp=-2
      inside decimalAdjust() type=round  val=1234.505  exp=-2
      inside decimalAdjust() shift  val=1234.505
      inside decimalAdjust() shift  val=123450
      inside decimalAdjust() shift back  val=123450
      inside decimalAdjust() shift back  ret=1234.5
      inside Math.round10(value, exp);  decimalAdjust() ret=1234.5
      Math.round10(1234.505, -2);   = 1234.5
    



    This time it appears to be the combination of the toString() function and toFixed()

    So there ya' have it. Even the MDN recommended closure that * IS * the Javascript solution to this (lack of) rounding anomaly, also fails.

    Don't shoot the messenger here. Remember, a month ago all I wanted was a reliable way to shift one bit left. Sheer persistence and digging got us this far.


    2 Attachments

About

Avatar for Robin @Robin started