2010-10-26 17 views
35

Ho letto this e that. Voglio che questo esattamente:Limita un doppio a due decimali senza zero finale

1,4324 => "1.43"
9,4000 => "9.4"
43.000 => "43"

9.4 => "9.40" (sbagliato)
43.000 = > "43.00" (errato)

In entrambe le domande le risposte indicano NSNumberFormatter. Quindi dovrebbe essere facile da raggiungere, ma non per me.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)]; 

    NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init]; 
    [doubleValueWithMaxTwoDecimalPlaces setNumberStyle:NSNumberFormatterDecimalStyle]; 
    [doubleValueWithMaxTwoDecimalPlaces setPaddingPosition:NSNumberFormatterPadAfterSuffix]; 
    [doubleValueWithMaxTwoDecimalPlaces setFormatWidth:2]; 

    NSNumber *myValue = [NSNumber numberWithDouble:0.]; 
    //NSNumber *myValue = [NSNumber numberWithDouble:0.1]; 

    myLabel.text = [doubleValueWithMaxTwoDecimalPlaces stringFromNumber:myValue]; 

    [self.view addSubview:myLabel]; 
    [myLabel release]; 
    myLabel = nil; 
    [doubleValueWithMaxTwoDecimalPlaces release]; 
    doubleValueWithMaxTwoDecimalPlaces = nil; 
} 

Ho anche provato con

NSString *resultString = [NSString stringWithFormat: @"%.2lf", [myValue doubleValue]]; 
NSLog(@"%@", resultString); 

Così come posso formattare valori doppi con un massimo di due cifre decimali? Se l'ultima posizione contiene uno zero, lo zero deve essere omesso.

Soluzione:

NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init]; 
[doubleValueWithMaxTwoDecimalPlaces setNumberStyle:NSNumberFormatterDecimalStyle]; 
[doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2]; 
NSNumber *myValue = [NSNumber numberWithDouble:0.]; 
NSLog(@"%@",[doubleValueWithMaxTwoDecimalPlaces stringFromNumber:myValue]]; 
[doubleValueWithMaxTwoDecimalPlaces release]; 
doubleValueWithMaxTwoDecimalPlaces = nil; 
+0

Vuoi arrotondare? Vale a dire 1.4363 => "1.43" o "1.44"? –

+0

Penso che avrebbe senso. – testing

+0

Non dimenticare di rilasciare doubleValueWithMaxTwoDecimalPlaces dopo tutto ... – Lukasz

risposta

41

Provate ad aggiungere la seguente riga, durante la configurazione del formattatore:

[doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2]; 
0

Come circa il taglio dei caratteri indesiderati dalla fine della stringa ?:

NSString* CWDoubleToStringWithMax2Decimals(double d) { 
    NSString* s = [NSString stringWithFormat:@"%.2f", d]; 
    NSCharacterSet* cs = [NSCharacterSet characterSetWithCharacterInString:@"0."]; 
    NSRange r = [s rangeOfCharacterInSet:cs 
           options:NSBackwardsSearch | NSAnchoredSearch]; 
    if (r.location != NSNotFound) { 
     s = [s substringToIndex:r.location]; 
    } 
    return s; 
} 
+1

Anche se questa soluzione funziona, c'è una soluzione molto migliore (vedi risposta accettata) – Muxa

0
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
[numberFormatter setRoundingMode:NSNumberFormatterRoundDown]; 
[numberFormatter setMinimumFractionDigits:2]; 
numberFormatter.positiveFormat = @"0.##"; 
NSNumber *num = @(total_Value); 
+0

pl spiega la tua risposta –

+0

@SahilMittal Sopra il codice NSNumberFormatterRoundDown restituisce il valore senza round, quindi positiveFormat fornisce solo due valori di frazione. –

Problemi correlati