我有一个动态表,其中的数据可以采用不同的格式。例如:500 500,57 1500,00 1.500,00。目前,我正在执行str_replace以删除千分之一的第一个点,然后用点替换逗号。
echo str_replace(array(".", ",",), array("", "."), $row['rad_iznos']);
效果很好,十进制数字上只有千分之一的点分隔符。但是当我使用总和上的toFixed(2)选项保留小数点后的值,它应根据以下条件工作:https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tofixed。如您所见,仅将小数点后两位数字四舍五入。
var totals=[0,0,0];
$(document).ready(function(){
var $dataRows=$("#sum_table tr:not('.totalColumn, .titlerow')");
$dataRows.each(function() {
$(this).find('.rowDataSd').each(function(i){
totals[i]+=parseInt( $(this).html());
});
});
$("#sum_table td.totalCol").each(function(i){
$(this).html('<span style="font-weight: bold;">'+totals[i].toFixed(2)+' kn</span>');
});
});
问题不是.toFixed()
,而是:
totals[i] += parseInt($(this).html());
将每一行的值添加为整数-因此会丢失小数点,因此当到达.toFixed()
时,它已经丢失了小数点。
解决方案是改用parseFloat()
,以保留小数位:
totals[i] += parseFloat($(this).html());