Formatting number as percentage
Asked Answered
P

7

23

I don't understand how NSNumberFormatterPercentStyle works!

Example:

NSNumber *number = [NSNumber numberWithFloat:90.5];
NSNumberFormatter *percentageFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[percentageFormatter setNumberStyle:NSNumberFormatterPercentStyle];

NSString *strNumber = [percentageFormatter stringFromNumber:numbericString];
NSLog (strNumber);  // Output: 9,050%

I simply want to show "90.5%" (respecting NSLocal). What is NSNumberFormatterPercentStyle doing with 90.5, and why? And, how can i get my desired result!?

Preoccupation answered 11/8, 2010 at 15:53 Comment(0)
C
29

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%.

Charwoman answered 11/8, 2010 at 15:57 Comment(0)
P
70

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];
Purgation answered 4/9, 2013 at 19:59 Comment(1)
if the OP comes back around and updates it, i'm fine with that. my answer and the other related ones were 3 years before this one. (i'm also presuming that setMaximumFractionDigits would round to 91% and not get the OP exactly what they wanted) (was setMultiplier available in ios in 2010?)Charwoman
C
29

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%.

Charwoman answered 11/8, 2010 at 15:57 Comment(0)
T
4

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 (‱).

Tristan answered 11/8, 2010 at 16:39 Comment(0)
C
4

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%

Cinemascope answered 11/9, 2019 at 6:42 Comment(0)
A
3

I do not know that language, but based on your code sample and output, I would guess that the percentage formatter multiplies by 100 and then puts the % sign on so that 1 becomes 100%. What you want is to use .905, which should become 90.5%.

Arbour answered 11/8, 2010 at 15:58 Comment(0)
B
3

The "percent style" formatter expects a number of 0.905 if you want to end up with 90.5%. It interprets 90.5 as out of 1, so it's 9050% of 1.

Benediction answered 11/8, 2010 at 15:58 Comment(0)
F
0

My clean solution in Swift:

create extension for NumberFormatter, that you can use wherever you want:

extension NumberFormatter {
    static var percentage: NumberFormatter {
        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .percent
        numberFormatter.maximumFractionDigits = .zero
        return numberFormatter
    }
}

Also, create extension for f.e. Double:

extension Double {
    var percentageValue: String? {
        return NumberFormatter.percentage.string(from: self as NSNumber)
    }
}

Then, using this:

let yourValue = 0.905
let percentageValue = yourValue.percentageValue

Output:

90.5%
Faux answered 8/9, 2022 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.