Formatting number as percentage
Just set the multiplier to 1
if your numbers are already in percentage.
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMaximumFractionDigits:0];
[numberFormatter setMultiplier:@1];
Swift 4/5
Create NSNumber
extension just like this
extension NSNumber {
func getPercentage() -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 0 // You can set what you want
return formatter.string(from: self)!
}
}
Get percentage
let confidence = 0.68300 as NSNumber
print(confidence.getPercentage())
Output
68%
Its presuming that the number is in the range of 0-1, as it is a percent
so 0.905 would get you 90.5%.
Let's start with the basics:
- 1 = 100%
- 1/2 = 0.5 = 50%
"Percent" means "out of 100", so 12% means 12/100. You don't "multiply by 100" to get a percentage value, you multiply by 100% (which is the same as multiplying by 1, which is the same as not doing anything).
Not everyone uses base 10 (though most "modern" languages do), and not everyone uses 100 as a denominator. See, for example, perMillSymbol (or kCFNumberFormatterPerMillSymbol). There's no "permill" format, but it's possible that it's automatically used for locales which don't use percentages.
See also: PER MILLE SIGN (‰) and PER TEN THOUSAND SIGN (‱).