将指数编号转换为nsnumber

时间:2013-10-22 13:41:01

标签: ios objective-c

我无法将指数号码(如9.471347923322231e + 18)转换为nsnumber。有没有人知道如何做到反过来?感谢。

1 个答案:

答案 0 :(得分:1)

对于指数,你应该使用:

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:12345
                                       exponent:-3
                                       isNegative:YES];

或者如果你想从NSString中获取它:

+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString locale:(NSDictionary *)locale;

例如:

NSString *stringNumber = @"9.471347923322231e+18";
NSDictionary    *locale = [NSDictionary dictionaryWithObject:@"." forKey:NSLocaleDecimalSeparator];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:stringNumber locale:locale];