nsnumberformatter

Swift 3 and NumberFormatter (.currency) == ¤?

强颜欢笑 提交于 2019-12-21 11:07:28
问题 Xcode 8.0 (8A218a) GM Target : iOS 10 (Swift 3) Consider the following code: let number = NSDecimalNumber(decimal: 22.4) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .currency numberFormatter.locale = Locale.current let result = numberFormatter.string(from: number) print(result!) The result is: ¤22.40 (I have no idea what ¤ means.) But if I initialize the locale such as: numberFormatter.locale = Locale(identifier: "en_US") The result will be: $22.40 ... which is what

Convert NSNumber to NSString with NSNumberFormatter

戏子无情 提交于 2019-12-21 04:28:39
问题 I am converting an NSNumber to an NSString assuming in this example that the selected key in "theindex" is 1000000 NSNumber *firstNumber = [tempDict objectForKey:@"theindex"]; NSString *convertNumber = [firstNumber stringValue]; Returning the NSString "1000000" I would like the string's value to be "1,000,000". I am not concerned with localization, but understand from other questions that NSNumberFormatter should be implemented. I am not sure how to accomplish this? 回答1: Here is an example

NSLocale currency symbol, show before or after amount value

假装没事ソ 提交于 2019-12-21 04:03:41
问题 I am using StoreKit to implement an in app purchase store in my application. I have a custom design and it means that the value of the price should be white and large, and the currency symbol smaller, darker and aligned to the top of the price value. I can get the currency symbol without any problems by using the NSLocale in SKproduct 's priceLocale property, and the value of the price in the price property. My problem is knowing when I should put the currency symbol before the price and when

How/where do I use NSNumberFormatter?

折月煮酒 提交于 2019-12-20 07:48:05
问题 I'm new to code and after reviewing a few answers still need a hand with this. In my code: func labelInformation(){ numLabels.text = newLabel.text } Current result: 228500.23 Desired result: 228,500.23 How/where do I use NSNumberFormatter? 回答1: Try like this: let inputValue = 228500.23 let numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = .CurrencyStyle numberFormatter.currencySymbol = "" let outputString = numberFormatter.stringFromNumber(inputValue) ?? "0.00" print

NSNumberFormatter : Show 'k' instead of ',000' in large numbers?

↘锁芯ラ 提交于 2019-12-19 02:37:06
问题 I'd like to change my large numbers from 100,000 to $100K if this is possible. This is what I have so far: let valueFormatter = NSNumberFormatter() valueFormatter.locale = NSLocale.currentLocale() valueFormatter.numberStyle = .CurrencyStyle valueFormatter.maximumFractionDigits = 0 My Question Using NSNumberFormatter, how can I output $100K rather than $100,000? My original question: This is what I have so far: self.lineChartView.leftAxis.valueFormatter = NSNumberFormatter() self.lineChartView

NSNumberformatter add extra zero

蹲街弑〆低调 提交于 2019-12-18 02:51:01
问题 I'm looking for a way to display "1" as "01", so basically everything below 10 should have a leading 0. What would be the best way to do this? I know I can just use a simple if structure to do this check, but this should be possible with NSNumberformatter right? 回答1: If you just want an NSString, you can simply do this: NSString *myNumber = [NSString stringWithFormat:@"%02d", number]; The %02d is from C. %nd means there must be at least n characters in the string and if there are less, pad it

Limit a double to two decimal places without trailing zeros

夙愿已清 提交于 2019-12-17 16:04:44
问题 I read this and that. I want this exactly: 1.4324 => "1.43" 9.4000 => "9.4" 43.000 => "43" 9.4 => "9.40" (wrong) 43.000 => "43.00" (wrong) In both questions the answers points to NSNumberFormatter . So it should be easy to achieve, but not for me. - (void)viewDidLoad { [super viewDidLoad]; UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)]; NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init]; [doubleValueWithMaxTwoDecimalPlaces

Re-Apply currency formatting to a UITextField on a change event

可紊 提交于 2019-12-17 06:13:19
问题 I'm working with a UITextField that holds a localized currency value. I've seen lots of posts on how to work with this, but my question is: how do I re-apply currency formatting to the UITextField after every key press? I know that I can set up and use a currency formatter with: NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; ... [currencyFormatter stringFromNumber:...]; but I don't know how to hook it

How to input currency format on a text field (from right to left) using Swift?

北城以北 提交于 2019-12-16 19:44:38
问题 I have a number let’s say 0.00 . When the user taps 1. We should have 0.01 When the user taps 2. We should display 0.12 When the user taps 3. We should display 1.23 When the user taps 4. We should display 12.34 How can I do that with Swift? 回答1: For Swift 3 . Input currency format on a text field (from right to left) override func viewDidLoad() { super.viewDidLoad() textField.addTarget(self, action: #selector(myTextFieldDidChange), for: .editingChanged) } func myTextFieldDidChange(_ textField

NSNumberFormatter thousand separator and trailing zeros

人盡茶涼 提交于 2019-12-13 10:43:26
问题 I have an object in my account array with a value of 8500.00. I'd like to display that as 8,500.00 but when I try to do so and print to the console it returns null. Any ideas? App Delegate NSArray *balance = [[NSArray alloc] initWithObjects:@"120.50", @"8500.00", nil]; // View controller // Setup number formatter NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *balance = [[appDelegate.accounts objectForKey:@