Ruby Divide Always Returning Zero in Rails Axlsx Export -
i cannot life of me figure out why dividing isn't working in aslsx export. can add, subtract, multiple , modulus division fine, when try answer_number / question_number
coming out 0. ideas i'm doing wrong?
works spitting out answer_number
times question_number
:
@survey.questions.each |question| question_number = choice.where(question_id: question.id).length question.answers.each |answer| answer_number = choice.where(answer_id: answer.id).length sheet.add_row [answer_number, question_number, answer_number * question_number] end end
doesn't work - spits out 0:
@survey.questions.each |question| question_number = choice.where(question_id: question.id).length question.answers.each |answer| answer_number = choice.where(answer_id: answer.id).length sheet.add_row [answer_number, question_number, answer_number / question_number] end end
i thought maybe doing sort of rounding down when number less 0, doesn't seem work either (the end goal percent of each answer chosen):
@survey.questions.each |question| question_number = choice.where(question_id: question.id).length question.answers.each |answer| answer_number = choice.where(answer_id: answer.id).length sheet.add_row [answer_number, question_number, answer_number / question_number * 100] end end
since both numbers integers, ruby doing integer division, ignores remaining fractions, , guess divisions returning numbers between 0 , 1, answer 0, avoid 1 of both numbers should float @mtm suggested, add to_f
function either of numbers
Comments
Post a Comment