0

現在、このコードを使用して、テーブルの列から合計を計算しています:

function calculateTotals() {
//these will hold the totals
var hours = 0;

//reference the rows you want to add
//this will not include the header row
var rows = $("#deliverables tr:not(:first-child, :last-child)");
rows.children("td:nth-child(2)").each(function() {
    hours += parseInt($(this).html());
});

midHours = (hours).toFixed(2);

$(".total-hours").html(midHours);

};

ただし、出力midHoursには小数は表示されません。たとえば、値が 12.5 のテーブル セルの場合、セルでは 12 として出力されます.total-hours

4

3 に答える 3

2

parseInt() を使用しているため、代わりに parseFloat() を使用してください

var rows = $("#deliverables tr:not(:first-child, :last-child)");
rows.children("td:nth-child(2)").each(function() {
    hours += parseFloat($(this).html());
});
于 2013-08-21T17:00:40.137 に答える
0

Arun が言ったことですが、説明を提供します。明らかに変数についてあまり知らないので、整数は 15 や 256 や 108,197 のような小数点以下の桁数のない数値です。parseInt(n) の int は整数を表し、parseFloat(n) の float は浮動小数点数を表します

float を int に変換すると、丸めずに小数点以下のすべてを単純に削除するため、72.9999999999 は 72 になります。簡単な修正は、.499999999 を追加してから整数に変換することですが、実際に変換したい場合のみです。丸めを使用して整数を計算しますが、そうではないように聞こえます。

小数点以下の桁数が必要な場合は parseFloat を使用します

于 2013-08-21T17:15:14.013 に答える