在javascript中显示两个db记录的总和

时间:2017-06-18 10:53:34

标签: javascript codeigniter

我现在陷入困境,我需要用paper_score + final_score(即paper_score和final_score的总和)替换grand_score。我尝试了不同的改动,但仍然没有取得丰硕的成果。请帮帮我。

'<td>' + result['records'][i]['paper_score'] + '</td>' + //continuous assessment
                        '<td>' + result['records'][i]['final_score'] + '</td>' + //examination
                        '<td>' + result['records'][i]['grand_score'] + '</td>' + //ca + exam

1 个答案:

答案 0 :(得分:0)

您只需使用+运算符将它们添加到一起:

'<td>' + result['records'][i]['paper_score'] + '</td>' + //continuous assessment
'<td>' + result['records'][i]['final_score'] + '</td>' + //examination
'<td>' + (result['records'][i]['paper_score'] + result['records'][i]['final_score']) + '</td>' + //ca + exam

如果paper_scorefinal_score是字符串而不是数字,则需要先将它们强制转换(假设这里有浮点数,您也可以使用parseInt()表示整数):

'<td>' + (parseFloat(result['records'][i]['paper_score']) + parseFloat(result['records'][i]['final_score'])) + '</td>' + //ca + exam