Skip to main content
added 83 characters in body
Source Link
mxcl
  • 27k
  • 12
  • 102
  • 100

Using Swift 5’s fancy new extendible interpolation:

extension DefaultStringInterpolation { mutating func appendInterpolation(pad value: Int, toWidth width: Int, using paddingCharacter: Character = "0") { appendInterpolation(String(format: "%\(paddingCharacter)\(width)d", value)) } } let pieCount = 3 print("I ate \(pad: pieCount, toWidth: 3, using: "0") pies") // => `I ate 003 pies` print("I ate \(pad: 1205, toWidth: 3, using: "0") pies") // => `I ate 1205 pies` 

Using Swift 5’s fancy new extendible interpolation:

extension DefaultStringInterpolation { mutating func appendInterpolation(pad value: Int, toWidth width: Int, using paddingCharacter: Character = "0") { appendInterpolation(String(format: "%\(paddingCharacter)\(width)d", value)) } } print("I ate \(pad: pieCount, toWidth: 3, using: "0") pies") // => `I ate 003 pies` 

Using Swift 5’s fancy new extendible interpolation:

extension DefaultStringInterpolation { mutating func appendInterpolation(pad value: Int, toWidth width: Int, using paddingCharacter: Character = "0") { appendInterpolation(String(format: "%\(paddingCharacter)\(width)d", value)) } } let pieCount = 3 print("I ate \(pad: pieCount, toWidth: 3, using: "0") pies") // => `I ate 003 pies` print("I ate \(pad: 1205, toWidth: 3, using: "0") pies") // => `I ate 1205 pies` 
added 80 characters in body
Source Link
mxcl
  • 27k
  • 12
  • 102
  • 100

Using Swift 5’s fancy new extendible interpolation:

private extension DefaultStringInterpolation { mutating func appendInterpolation(_pad value: Int, paddingtoWidth width: Int, using paddingCharacter: Character = "0") { appendInterpolation(String(format: "%0\"%\(paddingpaddingCharacter)\(width)d", value)) } } print("I ate \(pad: pieCount, paddingtoWidth: 3, using: "0") pies") // => `I ate 003 pies` 

Using Swift 5’s fancy new extendible interpolation:

private extension DefaultStringInterpolation { mutating func appendInterpolation(_ value: Int, padding: Int) { appendInterpolation(String(format: "%0\(padding)d", value)) } } print("I ate \(pieCount, padding: 3) pies") // => `I ate 003 pies` 

Using Swift 5’s fancy new extendible interpolation:

extension DefaultStringInterpolation { mutating func appendInterpolation(pad value: Int, toWidth width: Int, using paddingCharacter: Character = "0") { appendInterpolation(String(format: "%\(paddingCharacter)\(width)d", value)) } } print("I ate \(pad: pieCount, toWidth: 3, using: "0") pies") // => `I ate 003 pies` 
Source Link
mxcl
  • 27k
  • 12
  • 102
  • 100

Using Swift 5’s fancy new extendible interpolation:

private extension DefaultStringInterpolation { mutating func appendInterpolation(_ value: Int, padding: Int) { appendInterpolation(String(format: "%0\(padding)d", value)) } } print("I ate \(pieCount, padding: 3) pies") // => `I ate 003 pies`