我有这些值 int score = 7,answered = 40,skipped = 0,totalRecords = 2;
NSNumber *percentage =[NSNumber numberWithFloat:(score / (answered + skipped)) / totalRecords];
它返回 0.000 而不是 0.0875。哪里有问题
我有这些值 int score = 7,answered = 40,skipped = 0,totalRecords = 2;
NSNumber *percentage =[NSNumber numberWithFloat:(score / (answered + skipped)) / totalRecords];
它返回 0.000 而不是 0.0875。哪里有问题
您使用的是整数,因此四舍五入为 0。
您需要将它们创建为浮点数:
float score = 7.0
或将您的值更改为浮点数:
scoreFloat = (float)score;
将初始值从 int 更改为 float :
float score = 7.0, answered = 40.0, skipped = 0.0, totalRecords = 2.0;
而不是在你真正应该使用整数的地方使用浮点变量,你可以只转换分母
NSNumber *percentage =[NSNumber numberWithFloat:
(score / (float)(answered + skipped)) / (float)totalRecords];