Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-27144

Number.prototype.zeroPad() returns erroneous output if target width is not greater than stringified number's length

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor - P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Component/s: Shell
    • Labels:
    • Backwards Compatibility:
      Fully Compatible
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      > x=40
      40
      > x.zeroPad(4)
      0040
      > x.zeroPad(2)
      {
      	"0" : "4",
      	"1" : "0",
      	"ltrim" : function trimLeft() {
          [native code]
      },
      	"rtrim" : function trimRight() {
          [native code]
      },
      	"pad" : function (length, right, chr) {
          if (typeof chr == 'undefined')
              chr = ' ';
          var str = this;
          for (var i = length - str.length; i > 0; i--) {
              if (right) {
                  str = str + chr;
              } else {
                  str = chr + str;
              }
          }
          return str;
      }
      }
      

      Show
      > x=40 40 > x.zeroPad(4) 0040 > x.zeroPad(2) { "0" : "4", "1" : "0", "ltrim" : function trimLeft() { [native code] }, "rtrim" : function trimRight() { [native code] }, "pad" : function (length, right, chr) { if (typeof chr == 'undefined') chr = ' '; var str = this; for (var i = length - str.length; i > 0; i--) { if (right) { str = str + chr; } else { str = chr + str; } } return str; } }

      Description

      Padding on a number should pad up to the width specified or return the stringified number if no padding is required. Doing so would match the behavior of String.prototype.padStart().

      The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string will be returned as it is.

        Attachments

          Activity

            People

            Assignee:
            backlog-server-stm Backlog - Server Tooling and Methods (STM)
            Reporter:
            jonathan.abrahams Jonathan Abrahams
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: