开发者

Formatting number as percentage

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!?


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];


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


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%


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜