科学计数法表示数字,并保留三位有效数字,记录一下
示例如下:
- (NSString *)getScientificStyleWithString:(NSString *)str{
double num = [[NSString stringWithFormat:@"%@",str] floatValue];
NSNumberFormatter * formatter = [[NSNumberFormatter alloc]init];
formatter.numberStyle = NSNumberFormatterScientificStyle;
NSString *resultStr;
NSString * ttlString = [formatter stringFromNumber:[NSNumber numberWithDouble:num]];
NSRange eRange = [ttlString rangeOfString:@"E"];
if (eRange.location != NSNotFound) {
NSString *dataStr = [ttlString substringToIndex:eRange.location];
NSString *eStr = [ttlString substringFromIndex:eRange.location];
resultStr = [NSString stringWithFormat:@"%.2f%@",dataStr.floatValue,eStr];
}else{
resultStr = ttlString;
}
return resultStr;
}
网友评论