nsnumberformatter

Converting currency string into NSDecimalNumber

。_饼干妹妹 提交于 2020-01-05 04:37:05
问题 I'm trying to convert some currency string (e.g., $25,000) into NSDecimalNumber using the following code: NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; [currencyFormatter setGeneratesDecimalNumbers:TRUE]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; // textField.text is some currency string (e.g., $25,000) NSDecimalNumber *decimalNumber = [currencyFormatter numberFromString

How to avoid rounding off in NSNumberFormatter

扶醉桌前 提交于 2020-01-01 08:37:22
问题 I am trying to have a number string with maximum 2 decimals precision, while rest decimals just trimmed off instead of rounding them up. For example: I have: 123456.9964 I want: 123456.99 -> Just want to trim rest of the decimal places What I have tried is: NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; [numberFormatter setMaximumFractionDigits:2]; NSString *numberAsString = [numberFormatter

NSNumberFormatter with comma decimal separator

一曲冷凌霜 提交于 2019-12-28 04:12:08
问题 I tried to convert an NSString like "12000.54" into "12.000,54". I wrote an NSNumberFormatter instance. NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; [formatter setGroupingSeparator:@"."]; [formatter setDecimalSeparator:@","]; But when I NSLog this : NSLog(@"%@",[formatter stringFromNumber:[formatter numberFromString:value]]); It prints

NumberFormatter grouping not working as expected

£可爱£侵袭症+ 提交于 2019-12-24 21:11:42
问题 Working on currency formatting, I've found an issue when trying to format Chilean pesos. Following this code: let priceFormatter = NumberFormatter() priceFormatter.locale = Locale(identifier: "es_CL") priceFormatter.numberStyle = .currency priceFormatter.currencyCode = "CLP" priceFormatter.string(from: 9990) // A priceFormatter.string(from: 99900) // B Executing this I get $9990 for A and $99.990 for B . What I want to achieve is $9.990 for A Looks like the formatter is not adding the

Make NSNumberformatter().numberFromString return an optional

家住魔仙堡 提交于 2019-12-24 14:53:18
问题 I'm pretty new to swift. Just wanted to know how to convert the code below to be an optional. var displayValue: Double { get { return NSNumberFormatter().numberFromString(display.text! as NSString)!.doubleValue } set { display.text = "\(newValue)" userIsInTheMiddleOfTypeingANumber = false } } this is from the stanford calculator lecture series. The lecturer didn't show how to make it optional. My understanding is that when there is a string in displayValue that can't be converted to a Double

How can i get the locale from currency names list

限于喜欢 提交于 2019-12-24 04:31:41
问题 i am making a currency selecting screen.for that i am displaying the list of currencies using the following code to create an array of currencies from which a tableview is populated NSArray *countryArray = [NSLocale ISOCurrencyCodes]; for(NSString *country in countryArray) { //NSLog(@"%@",[[NSLocale currentLocale] displayNameForKey:NSLocaleCurrencyCode value:country]); if([[NSLocale currentLocale] displayNameForKey:NSLocaleCurrencyCode value:country]) { [countriesArray addObject:[[NSLocale

How do I force a sign-character on the output of an NSNumberFormatter

会有一股神秘感。 提交于 2019-12-23 07:03:35
问题 I want to use a number formatter to generate my output, so the number is automatically formatted for the user's locale, but I want it to work like "%+.1f" does in printf() , that is always have a sign specified. NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; nf.numberStyle = NSNumberFormatterDecimalStyle; nf.maximumFractionDigits = 1; double val = 3.1234; label.text = [NSString stringWithFormat: @"XXX %@ XXX", [nf stringFromNumber: [NSNumber numberWithDouble: val]]]; I want the

How to put comma and decimals in my UITextField dynamically?

柔情痞子 提交于 2019-12-23 02:48:08
问题 I want to add a ',' in '11000' like this '11,000' and decimal ('.') in 465 like 4.65. I wrote this for Comma : - (BOOL) textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string { NSString *unformattedValue; NSNumberFormatter *formatter; NSNumber *amount; switch ([textField tag]) { case 102: unformattedValue = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""]; unformattedValue = [unformattedValue

Format long numbers for axis labels core plot

感情迁移 提交于 2019-12-22 12:44:12
问题 I am trying to format long numbers in form of "k" for my axis labels. For example; if I have 10000 , I would like it to be displayed as 10k . I have already tried to follow this answer. But I could not manage to get formatted numbers. What I need to do additionally? Please guide me. Thanks. EDIT: HumanReadableFormatter.h @interface HumanReadableFormatter : NSNumberFormatter @end HumanReadableFormatter.m I have modified code from the link above to meet my requirements. #import

Format long numbers for axis labels core plot

吃可爱长大的小学妹 提交于 2019-12-22 12:43:17
问题 I am trying to format long numbers in form of "k" for my axis labels. For example; if I have 10000 , I would like it to be displayed as 10k . I have already tried to follow this answer. But I could not manage to get formatted numbers. What I need to do additionally? Please guide me. Thanks. EDIT: HumanReadableFormatter.h @interface HumanReadableFormatter : NSNumberFormatter @end HumanReadableFormatter.m I have modified code from the link above to meet my requirements. #import