2011-01-11 33 views
0

我正在计算一个完整的混合数据类型变量的百分比。混合变量数据类型的数学

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); 
} 

控制台输出如下:

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 

为什么进步没有返回 “50”?

回答

2

您在NSLog语句中使用了错误的格式字符串。 %d用于整数。记录浮点数时需要使用%f。 (T here are extra parameters用于限制小数位数等)