Objective c 给出错误值的数字

Objective c 给出错误值的数字,objective-c,nsnumber,Objective C,Nsnumber,我有这些价值观 积分=7,回答=40,跳过=0,总记录=2 NSNumber *percentage =[NSNumber numberWithFloat:(score / (answered + skipped)) / totalRecords]; 它返回0.000而不是0.0875。 问题在哪里您使用的是整数,因此四舍五入为0 您需要将它们创建为浮动: float score = 7.0 或将值更改为浮动: scoreFloat = (float)score; 您使用的是整数,

我有这些价值观 积分=7,回答=40,跳过=0,总记录=2

    NSNumber *percentage =[NSNumber numberWithFloat:(score / (answered + skipped)) / totalRecords];
它返回0.000而不是0.0875。
问题在哪里

您使用的是整数,因此四舍五入为0

您需要将它们创建为浮动:

float score = 7.0
或将值更改为浮动:

scoreFloat = (float)score;

您使用的是整数,因此四舍五入为0

您需要将它们创建为浮动:

float score = 7.0
或将值更改为浮动:

scoreFloat = (float)score;

您使用的是整数,因此四舍五入为0

您需要将它们创建为浮动:

float score = 7.0
或将值更改为浮动:

scoreFloat = (float)score;

您使用的是整数,因此四舍五入为0

您需要将它们创建为浮动:

float score = 7.0
或将值更改为浮动:

scoreFloat = (float)score;

将初始值从int更改为float:

float score = 7.0, answered = 40.0, skipped = 0.0, totalRecords = 2.0;

将初始值从int更改为float:

float score = 7.0, answered = 40.0, skipped = 0.0, totalRecords = 2.0;

将初始值从int更改为float:

float score = 7.0, answered = 40.0, skipped = 0.0, totalRecords = 2.0;

将初始值从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];

不使用真正应该使用整数的浮点变量,只需转换分母即可

NSNumber *percentage =[NSNumber numberWithFloat:
            (score / (float)(answered + skipped)) / (float)totalRecords];

不使用真正应该使用整数的浮点变量,只需转换分母即可

NSNumber *percentage =[NSNumber numberWithFloat:
            (score / (float)(answered + skipped)) / (float)totalRecords];

不使用真正应该使用整数的浮点变量,只需转换分母即可

NSNumber *percentage =[NSNumber numberWithFloat:
            (score / (float)(answered + skipped)) / (float)totalRecords];

你不能把整数除以,然后得到浮点。你需要投到浮球上。i、 e.1/2=1,但1.0/2.0=0.5。使用浮点值代替int!试试这个NSNumber*百分比=[NSNumber numberWithFloat:(7.0/(40.0+0))/2.0];NSLog(@“%@”,百分比);你不能把整数除以,然后得到浮点。你需要投到浮球上。i、 e.1/2=1,但1.0/2.0=0.5。使用浮点值代替int!试试这个NSNumber*百分比=[NSNumber numberWithFloat:(7.0/(40.0+0))/2.0];NSLog(@“%@”,百分比);你不能把整数除以,然后得到浮点。你需要投到浮球上。i、 e.1/2=1,但1.0/2.0=0.5。使用浮点值代替int!试试这个NSNumber*百分比=[NSNumber numberWithFloat:(7.0/(40.0+0))/2.0];NSLog(@“%@”,百分比);你不能把整数除以,然后得到浮点。你需要投到浮球上。i、 e.1/2=1,但1.0/2.0=0.5。使用浮点值代替int!试试这个NSNumber*百分比=[NSNumber numberWithFloat:(7.0/(40.0+0))/2.0];NSLog(@“%@”,百分比);