nsnumberformatter

NSNumberFormatter numberFromString is adding a small fraction when converting certain numbers

心不动则不痛 提交于 2019-12-01 21:10:53
I'm seeing some strange bugs in my iPhone app that I have narrowed down to my use of NSNumberFormatter. A stripped down example... In Xcode playground I have: let numberFormatter = NSNumberFormatter() //numberFormatter.numberStyle = .DecimalStyle - does not change behavior let numberString = "546000.06" let number: NSNumber = numberFormatter.numberFromString(numberString)! print("number: \(number)") let number1: NSNumber = NSDecimalNumber(string: numberString) print("number1: \(number1)") This is the output: number: 546000.0600000001 number1: 546000.06 Note that setting the numberStyle to

Limiting both the fractional and total number of digits when formatting a float for display

时间秒杀一切 提交于 2019-12-01 17:01:31
I need to print a float value in area of limited width most efficiently. I'm using an NSNumberFormatter , and I set two numbers after the decimal point as the default, so that when I have a number like 234.25 it is printed as is: 234.25 . But when I have 1234.25 I want it to be printed as: 1234.3 , and 11234.25 should be printed 11234 . I need a maximum of two digits after the point, and a maximum of five digits overall if I have digits after the point, but it also should print more than five digits if the integer part has more. I don't see ability to limit the total number of digits in

Does NSNumberFormatter.stringFromNumber ever return nil?

送分小仙女□ 提交于 2019-12-01 16:25:10
It seems to me that any valid number can also be expressed as a String , so I don't know why this function returns a String? instead of a String . My best guess would be because of the legacy support. This is from the official documentation: The behavior of an NSNumberFormatter object can conform either to the range of behaviors existing prior to OS X v10.4 or to the range of behavior since that release. NSNumberFormatter Class Reference 来源: https://stackoverflow.com/questions/28103113/does-nsnumberformatter-stringfromnumber-ever-return-nil

NSNumberFormatter returns nil on device, not on simulator

一曲冷凌霜 提交于 2019-12-01 06:53:29
I use a NSNumberFormatter to convert a user input to a NSNumber (a decimal number), I use ARC. NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; [f setMaximumFractionDigits:1]; _myNumber = [f numberFromString:myTextField.text]; Sometimes this results in _myNumber to be nil . Even when I am absolutely sure the users input is a correct decimal number (I checked during debugging). For completeness: _myNumber is a synthesized property of the ViewController. This only happens when running the app on a device, not when I run it in the

NSNumberFormatter returns nil on device, not on simulator

孤街醉人 提交于 2019-12-01 04:49:58
问题 I use a NSNumberFormatter to convert a user input to a NSNumber (a decimal number), I use ARC. NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; [f setMaximumFractionDigits:1]; _myNumber = [f numberFromString:myTextField.text]; Sometimes this results in _myNumber to be nil . Even when I am absolutely sure the users input is a correct decimal number (I checked during debugging). For completeness: _myNumber is a synthesized property of

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

こ雲淡風輕ζ 提交于 2019-11-30 19:58:30
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.leftAxis.valueFormatter?.locale = NSLocale.currentLocale() self.lineChartView.leftAxis.valueFormatter?

NSNumberFormatter numberFromString returns null

若如初见. 提交于 2019-11-30 15:45:26
Here's my code NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; NSNumber *amount = [[NSNumber alloc] init]; NSLog(@"the price string is %@", price); amount = [currencyStyle numberFromString:price]; NSLog(@"The converted number is %@",[currencyStyle numberFromString:price]); NSLog(@"The NSNumber is %@", amount); NSLog(@"The formatted version is %@", [currencyStyle stringFromNumber:amount]); NSLog(@"--------------------"); self.priceLabel.text =

Converting NSString to Currency - The Complete Story

删除回忆录丶 提交于 2019-11-30 14:36:38
问题 After over a day of poking around with this problem I will see if I can get some help. This question has been more or less asked before, but it seems no one is giving a full answer so hopefully we can get it now. Using a UILabel and a UITextView (w/ number keyboard) I want to achieve an ATM like behavior of letting the users just type the numbers and it is formatted as currency in the label. The idea is basically outlined here: What is the best way to enter numeric values with decimal points?

iPhone NSNumberFormatter setFormat:

北城余情 提交于 2019-11-30 12:10:19
It appears as though NSNumberFormatter's method for setting custom formatters . setFormat: isn't available on the iPhone... SO this... crashes my app: NSNumberFormatter *lengthFormatter = [[NSNumberFormatter alloc] init]; [lengthFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; [lengthFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; [lengthFormatter setFormat:@"#,##0.0M"]; Has this occurred to anyone else? Thanks, Dan **ANSWERED BELOW. NSNumberFormatter also has methods called setPositiveFormat: setNegativeFormat: which replace the depreciated setFormat: ** Yes, it's

Converting NSString to Currency - The Complete Story

匆匆过客 提交于 2019-11-30 10:39:52
After over a day of poking around with this problem I will see if I can get some help. This question has been more or less asked before, but it seems no one is giving a full answer so hopefully we can get it now. Using a UILabel and a UITextView (w/ number keyboard) I want to achieve an ATM like behavior of letting the users just type the numbers and it is formatted as currency in the label. The idea is basically outlined here: What is the best way to enter numeric values with decimal points? The only issue is that it never explicitly says how we can go from having an integer like 123 in the