NSNumberFormatter PercentStyle decimal places

Fred Faust picture Fred Faust · May 22, 2015 · Viewed 13.3k times · Source

I'm using Swift

let myDouble = 8.5 as Double

let percentFormatter            = NSNumberFormatter()
percentFormatter.numberStyle    = NSNumberFormatterStyle.PercentStyle
percentFormatter.multiplier     = 1.00

let myString = percentFormatter.stringFromNumber(myDouble)!

println(myString)

Outputs 8% and not 8.5%, how would I get it to output 8.5%? (But only up to 2 decimal places)

Answer

zisoft picture zisoft · May 22, 2015

To set the number of fraction digits use:

percentFormatter.minimumFractionDigits = 1
percentFormatter.maximumFractionDigits = 1

Set minimum and maximum to your needs. Should be self-explanatory.