本文主要是介绍ios 科学计数法,保留n个有效值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*科学计数法,保留n个有效值*/-(NSString *) toExponent:(double)d rms:(unsigned)n
{if(n==0){return nil;}CFLocaleRef currentLocale = CFLocaleCopyCurrent();CFNumberFormatterRef customCurrencyFormatter = CFNumberFormatterCreate(NULL, currentLocale, kCFNumberFormatterCurrencyStyle);NSString *s_n = @"#";if(n > 1){for(int j = 0; j < n; j++){NSString *temp = s_n;if(j == 0){s_n = [temp stringByAppendingString:@"."];}else{s_n = [temp stringByAppendingString:@"0"];}}}CFNumberFormatterSetFormat(customCurrencyFormatter, (CFStringRef)s_n);double i=1;int exponent = 0;while (1) {i = i*10;exponent++;if(d < i){break;}}double n1 = d * 10 / i;CFNumberRef number1 = CFNumberCreate(NULL, kCFNumberDoubleType, &n1);CFStringRef string1 = CFNumberFormatterCreateStringWithNumber(NULL, customCurrencyFormatter, number1);NSString * result = [NSString stringWithFormat:@"%s E%d",CFStringGetCStringPtr(string1, CFStringGetSystemEncoding()),exponent];CFRelease(currentLocale);CFRelease(customCurrencyFormatter);CFRelease(number1);CFRelease(string1);return result;}
这篇关于ios 科学计数法,保留n个有效值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!