Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-55358

Javascript Number.toPrecision() Incorrect

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.7.1
    • 5.7.0
    • None
    • Lenovo ThinkPad Ultrabook, Intel Core i7-6500 CPU @2.50GHz (dual core), Windows 10, QT 5.7
    • 8b7dbfe1e0c0d4b8d0984fb381f1a0bb6b566e76

    Description

      QML's internal javascript interpreter does not adhere to Number.toPrecision's specification, specifically in the instances when the number object whose toPrecision method is called is the object containing an integer value (such as 82, 34, 2, etc) and the precision value passed to the method is greater than the number of digits in the original number

      Javascript's Number.toPrecision method(as documented here should return a string with a given number of significant digits. For instance, when the number is 42, and the precision is 3, then the result should be the string "42.0". However, the method currently produces "42", with no padding zeroes at the end. As mentioned above, this contradicts the specification listed above.

      Example code is below:

      ToPrecisionTest.js
      // Simple function for demonstrating the error in Number.toPrecision
      public String testToPrecision()
      {
          var number = 42;
          console.log(number.toPrecision());//prints "42" to the console (which it should)
          console.log(number.toPrecision(1))//prints "40" to the console (once again, accurate)
          console.log(number.toPrecision(2))//prints "42" to the console (accurate, again)
          console.log(number.toPrecision(3))//prints "42" to the console, which is an error.
          //the function, once the precision supplied passes the number of digits in the source number,
          //should pad the source number with '0's.
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ulherman Ulf Hermann
            joel.sampson.precision.planting Joel Sampson
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes