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

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Shell
    • Server Tooling & Methods
    • ALL
    • 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; } }

      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.

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

              Created:
              Updated:
              Resolved: