JavaScript 在添加值时,它会像字符串一样计算
javascript while adding values it calculates like string
将小计+增值税+CST+舍入总计
function update_totals()
{
var grand_total = 0;
var grand_subtotal = 0;
var vat=0;
var cst=0;
var rounds = parseInt($('.round').val());
var vat_percent = parseInt($('.vat-percent').val());
if ( (vat_percent < 0) || (vat_percent > 90) ) { vat_percent=10; $('.vat-percent').find('input').val(10); }
var cst_percent = parseInt($('.cst-percent').val());
if ( (cst_percent < 0) || (cst_percent > 90) ) { cst_percent=10; $('.cst-percent').find('input').val(10); }
$('#invoice-items .cell').children('.subtotal-cell').each(function(){
grand_subtotal += parseFloat( $(this).find('input').val() );
});
vat=(grand_subtotal * (vat_percent/100)).toFixed(2);
cst=(grand_subtotal * (cst_percent/100)).toFixed(2);
grand_total = (grand_subtotal + vat + cst rounds);
grand_subtotal = grand_subtotal.toFixed(2);
grand_total =(grand_subtotal) + (vat);
//update dom
$('.sub-total').val(grand_subtotal);
$('.vat').val(vat);
$('.cst').val(cst);
$('.grand-total').val(grand_total);
}
在图片中查找错误 两个值并并排添加添加到grand_total =grand_subtotal + 增值税 + CST + 圆形它显示 25.001.25 在此 25.00 是小计 1.25 是增值税
谢谢
Number.toFixed()
返回一个字符串。
作为旁注:鉴于 JavaScript 中的0.1 * 0.2
出现0.020000000000000004
你真的想使用浮点算法进行财务计算吗?
如何处理 JavaScript 中的浮点数精度?
这里不确定,但您是否缺少+
.
grand_total = (grand_subtotal + vat + cst rounds);
我的意思是它应该是
grand_total = (grand_subtotal + vat + cst + rounds);
只是一个很小的输入。
相关文章:
- 计算输入中有多少逗号分隔的字符串
- 如何在JavaScript/Node中计算SHA256哈希和Base64字符串编码
- JavaScript:计算字符串中特定整数的实例
- 计算空字符串的值
- 检查字符串是否包含计算
- 以点表示法计算字符串,以从视图中的对象获取相应的值
- 计算字符串中的特定字母
- 计算字符串javascript中的某些单词
- Javascript-修改函数或从字符串中计算函数
- 在不渲染的情况下计算字符串大小
- 计算 jQuery 中字符串中的特殊字符
- regex从字符串中精确计算值
- 如何计算字符串集合(0.5000个元素)的哈希代码
- Javascript将从Flask发送的字符串计算为整数
- 将变量与 HTML 字符串一起使用,并计算
的数量 - 在 JavaScript 中给定时区字符串的情况下计算 UTC 偏移量
- jQuery/Javascript拆分字符串计算
- 字符串计算的小数
- 从JSON日期字符串计算日期差异
- Javascript字符串计算从PHP变量包含数学表达式(但实际上是一个日期)