nsnumberformatter

NSNumberFormatter maximumFractionDigits and maximumSignificantDigits bug

﹥>﹥吖頭↗ 提交于 2019-11-30 05:13:00
问题 Is this a bug when using maximumFractionDigits and maximumSignificantDigits together on NSNumberForamtter on iOS 8? NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.maximumFractionDigits = 2; formatter.maximumSignificantDigits = 3; NSLog(@"%@", [formatter stringFromNumber:@(0.3333)]); // output 0.333 expected 0.33 It works fine if I only use maximumFractionDigits NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.maximumFractionDigits = 2;

NSNumberFormatter numberFromString returns null

若如初见. 提交于 2019-11-29 22:56:40
问题 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

how to move cursor in UITextField after setting its value

与世无争的帅哥 提交于 2019-11-29 21:27:00
can anybody help me with this: i need to implement UITextField for input number. This number should always be in decimal format with 4 places e.g. 12.3456 or 12.3400. So I created NSNumberFormatter that helps me with decimal places. I am setting the UITextField value in -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method. I proccess input, use formatter and finally call [textField setText: myFormattedValue]; This works fine but this call also moves the cursor to the end of my field. That is unwanted. E.g. I have 12

iPhone NSNumberFormatter setFormat:

天涯浪子 提交于 2019-11-29 17:52:05
问题 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

NSNumberFormatter from 13000 to 13K

醉酒当歌 提交于 2019-11-29 10:21:15
I would like to convert numbers to the shorter form using NSNumberFormatter . For example: From 23000 to get 23K. How I can do it using NSNumberFormatter ? I know it is possible to do calculation on my own, but in my case it is not right way to go. Lolloz89 The 'K' suffix is not recognized as standard, so you can't do this using NSNumberFormatter. anyway this is a simple way to do that. -(NSString *)kFormatForNumber:(int)number{ int result; if(number >= 1000){ result = number/1000; NSString *kValue = [NSString stringWithFormat:@"%dk",result]; return kValue; } return [NSString stringWithFormat:

NSNumberformatter add extra zero

*爱你&永不变心* 提交于 2019-11-28 23:43:11
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? 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 with 0's. Here's an example: NSString *example = [NSString stringWithFormat:@"%010d", number]; If the

how to move cursor in UITextField after setting its value

萝らか妹 提交于 2019-11-28 17:32:30
问题 can anybody help me with this: i need to implement UITextField for input number. This number should always be in decimal format with 4 places e.g. 12.3456 or 12.3400. So I created NSNumberFormatter that helps me with decimal places. I am setting the UITextField value in -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method. I proccess input, use formatter and finally call [textField setText: myFormattedValue]; This

How to get NSNumberFormatter currency style from ISOCurrencyCodes?

我的未来我决定 提交于 2019-11-28 12:25:16
If I were to have EUR, or USD (both ISO currency codes), how could I make my NSNumberFormatter to properly format me a string like this? For EUR code: 10.000,00 € For USD code: $10,000.00 I could do this by setting localeIdentifier. But I really need to get it from the ISO currency code. Is this possible? [numberFormatter stringFromNumber: [_property price]]; Thank you! Use the setCurrencyCode: method of NSNumberFormatter . [numberFormatter setCurrencyCode:@"EUR"]; or [numberFormatter setCurrencyCode:@"USD"]; Keep in mind that even though the desired currency symbol will be used, the resulting

NSNumberFormatter from 13000 to 13K

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 03:41:02
问题 I would like to convert numbers to the shorter form using NSNumberFormatter . For example: From 23000 to get 23K. How I can do it using NSNumberFormatter ? I know it is possible to do calculation on my own, but in my case it is not right way to go. 回答1: The 'K' suffix is not recognized as standard, so you can't do this using NSNumberFormatter. anyway this is a simple way to do that. -(NSString *)kFormatForNumber:(int)number{ int result; if(number >= 1000){ result = number/1000; NSString

Limit a double to two decimal places without trailing zeros

泪湿孤枕 提交于 2019-11-27 21:01:41
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 setNumberStyle:NSNumberFormatterDecimalStyle]; [doubleValueWithMaxTwoDecimalPlaces setPaddingPosition