2011-01-11 21 views
0

Ich berechne ein Prozent abgeschlossen mit Variablen der gemischten Datentypen.Math mit gemischten variablen Datentypen

int incompleteCritical = 12; 
int total = 24; 
float progress = 0; 

NSLog(@"Incomplete Total: %d", incompleteCritical); 
NSLog(@"Total Total: %d", total); 

if (total > 0) { 
    progress = ((float)incompleteCritical/(float)total)*100; 
    NSLog(@"Progress: %d", progress); 
} 

Die Konsolenausgabe ist wie folgt:

2011-01-11 10:02:59.993 [18570:207] Incomplete Total: 12 
2011-01-11 10:02:59.993 [18570:207] Total Total: 24 
2011-01-11 10:02:59.994 [18570:207] Progress: 0 

Warum ist Fortschritt nicht "50" Rückkehr?

Antwort

2

Sie verwenden die falsche Formatzeichenfolge in Ihrer NSLog-Anweisung. %d wird für Ganzzahlen verwendet. Sie müssen %f verwenden, wenn Sie Gleitkommazahlen protokollieren. (T here are extra parameters zur Begrenzung der Anzahl der Dezimalstellen usw.)