4

我需要用NSNumberFormatter. 如果数字很小(例如0.000001)将以十进制格式显示0.000001。如果数字很大,许多小数或整数(例如12113454560.000000011111553242)将以科学计数法显示,1.211 E9.

我有这段代码(非常糟糕),但对我来说还不够:(。我认为解决方案是-setPositiveFormat格式模式#,##0.00000000.###E+0,有什么想法吗?

NSNumberFormatter *formato = [[NSNumberFormatter alloc] init];
    [formato setNumberStyle:NSNumberFormatterDecimalStyle];
    [formato setPositiveFormat:@"#,##0.0000000"];   
    [formato setNegativeFormat:@"-#,##0.0000000"];
    [formato setMaximumFractionDigits:7];
    [formato setMinimumFractionDigits:0];
    [formato setRoundingMode: NSNumberFormatterRoundHalfDown];

    NSString *numero = [formato stringFromNumber:[NSNumber numberWithDouble:valor]];

    //si vale después de formatear y el valor inicial es distinto de 0 se va de rango,
    //por lo tanto usamos notación científica
    if([numero isEqualToString:@"0"] && valor != 0){
        //inicializamos el objeto de nuevo para que no de problemas con setPositiveFormat
        [formato release];
        formato = [[NSNumberFormatter alloc] init];
        [formato setNumberStyle:NSNumberFormatterScientificStyle];
        [formato setMaximumFractionDigits:4];
        [formato setRoundingMode: NSNumberFormatterRoundHalfDown];
        [formato setExponentSymbol:@"e"];
        numero = [formato stringFromNumber:[NSNumber numberWithDouble:valor]] ;
    }

    [formato release];
4

2 回答 2

3

您可以在发布之前测试数字并根据其大小更改数字:

if (fabs(number)>10e7 || fabs(number)<10e-7) {
    [self.formatter setNumberStyle:NSNumberFormatterScientificStyle]; 
}
else
    [self.formatter setNumberStyle:NSNumberFormatterDecimalStyle];
于 2012-03-23T20:49:25.810 回答
0

下面的代码为我工作..

- (NSString *)checkAndConvertNumberFormat:(NSString *)numString {
    NSRange range = [numString rangeOfString:@" "];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [formatter setPositiveFormat:@"#,##0.0000000"];
    [formatter setNegativeFormat:@"-#,##0.0000000"];
    [formatter setMaximumFractionDigits:7];
    [formatter setMinimumFractionDigits:0];
    [formatter setRoundingMode: NSNumberFormatterRoundHalfDown];


    NSNumber *numberFromString;

    if (range.location == NSNotFound) {

        range = [numString rangeOfString:@","];
        if (([numString length]-range.location) == DECIMAL_POSITION) {

            NSLog(@"Comma notation");

            [formatter setGroupingSeparator:@"."];
            [formatter setDecimalSeparator:@","];
        }

        NSLog(@"%d", [numString length]-range.location);

        range = [numString rangeOfString:@"."];
        if (([numString length]-range.location) == DECIMAL_POSITION) {

            NSLog(@"Dot notation");

            [formatter setGroupingSeparator:@","];
            [formatter setDecimalSeparator:@"."];

            numberFromString = [formatter numberFromString:numString];
        }

        NSLog(@"%d", [numString length]-range.location);


    } else {

        NSLog(@"Space Notation");

        [formatter setGroupingSeparator:@" "];
        [formatter setDecimalSeparator:@","];
        numberFromString = [formatter numberFromString:numString];
    }

    numberFromString = [formatter numberFromString:numString];

    [formatter setGroupingSeparator:@""]; // Whatever you want here
    [formatter setDecimalSeparator:@"."]; // Whatever you want here

    return [formatter stringFromNumber:numberFromString];
}
于 2013-12-11T13:53:25.387 回答