iOS Swift 4 Coding

How to show a plus sign before a number in Swift 4?

6 views August 25, 2018 August 26, 2018 bicobro 0

The best way to show a plus sign “+” before a decimal value with Swift 4 is to use the NumberFormatter class. You have to use the property  “positivePrefix”. Assign a string value to this property to show when the value is a positive number.

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal          // Set defaults to the formatter that are common for showing decimal numbers
numberFormatter.positivePrefix = "+"            // Show + sign when the value is a positive number

let myDouble = 1234567.89
let myFormattedDouble = numberFormatter.string(for: myDouble)

# myFormattedDouble = +1234567.89

 

Was this helpful?