简体   繁体   English

从 JavaScript 中的数字中减去增值税

[英]Subtracting VAT from a number in JavaScript

I am beginner in JS and this code我是 JS 和这段代码的初学者

$('.brutto_amount').each(function (index, value) {
    let amount = $(this).text().replace(' brutto / rok', '').replace('(', ''); console.log(amount);
    if (discountType == 0) {
        let newAmount = (amount - discountValue).toFixed(2);
        if(newAmount < 0) newAmount = 1;
        $(this).html(`${newAmount} brutto / rok `);
    } else if (discountType == 1) {
        let newAmount = (amount - ((parseInt(amount) * parseInt(discountValue)) / 100)).toFixed(2);
        if(newAmount < 0) newAmount = 1;
        $(this).html(`${newAmount} brutto / rok `);
    }
});

works fine so far.到目前为止工作正常。

How can I subtract 23% VAT from the variable newAmount and round it to 2 decimal places?如何从变量newAmount中减去 23% 的增值税并将其四舍五入到小数点后 2 位?

I solved the problem similar to what Lapskaus mentions in the comments :我解决了类似于 Lapskaus 在评论中提到的问题:

The first is a simple math problem, if your newAmount value is a brutto value, calculating the netto, with a vat of 23%, is as simple as newAmount / 123 * 100 .第一个是一个简单的数学问题,如果你的newAmount值是一个 brutto 值,计算净值,23% 的增值税,就像newAmount / 123 * 100一样简单。 Use calculatedNetto.toFixed(2) to round that to 2 numbers.使用calculatedNetto.toFixed(2)将其四舍五入为 2 个数字。 Be aware though, that calculations in JS will have rounding errors due to floating point precision.但请注意,由于浮点精度,JS 中的计算会有舍入误差。 For further information about the issue and how to circumvent that, read this .有关该问题以及如何规避该问题的更多信息,请阅读 newAmount will be a brutto value which will be 123% from which you want to calculate the 100% value. newAmount 将是一个 123% 的 brutto 值,您要从中计算 100% 的值。 You substract 23% off of 123 which is too much你从 123 中减去 23%,这太多了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM