我有一个像“1.23,45”这样的字符串,(德国/法国风格或其他风格)。如何将格式删除为普通字符串或 nsnumber?我只知道从数字转换为字符串。:(谢谢。
问问题
1648 次
4 回答
2
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencySymbol:@""];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale:locale];
NSString *formattedOutput = [formatter stringFromNumber:[NSNumber numberWithFloat:[aPrice floatValue]]];
[locale release];
[formatter release];
NSLog(@"%@", formattedOutput];
进一步将 formattedString 转换为数字
NSNumber myNumber = [formattedOutput intValue]; //replace intValue by floatValue to get decimal output
于 2012-09-26T10:40:53.353 回答
1
您可以使用 a将 a 转换NSString
为a ,但您必须告诉它您为字符串使用的语言环境(在本例中为德语):NSNumber
NSNumberFormatter
// Create a number formatter that can convert from/to German currency:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
[formatter setLenient:YES];
// Use the number formatter to create a NSNumber:
NSNumber *number = [formatter numberFromString:@"1.23,45"];
// Return the number formatter to the current user's locale:
formatter.locale = [NSLocale currentLocale];
// Finally, create a string from the NSNumber formatted in the user's locale:
NSString *formattedString = [formatter stringFromNumber:number];
于 2012-10-30T16:22:37.703 回答
0
It sounds like you need to do two things:
1) break the string into an arrays of substrings based upon comma separation
NSString *startString = @"1.23,45";
NSArray *subStringArray= [colorStr componentsSeparatedByString:@","];
2) convert each substring to a number
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *subString = [subStringArray objectAtIndex:0];
NSNumber *thisNumber = [formatter numberFromString:subString];
NSLog (@"thisNumber: %@", thisNumber);
于 2012-09-26T10:52:26.980 回答
0
NSNumber * myNumber = [NSNumber numberWithInt:[@"1.23,45" intValue]];
或者
NSNumber * myNumber = [NSNumber numberWithFloat:[@"1.23,45" floatValue]];
注意:最好从字符串中修剪逗号(,)符号
您可以使用以下方法
+ (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
于 2012-09-26T10:32:21.167 回答