简体   繁体   English

Javascript - 从价格含增值税中保存价格不含增值税时出现的圆形问题

[英]Javascript - round issue when saving price excl vat from price incl vat

In the backend (Node.js) I save my prices in cents (*100) and always excluding VAT.在后端 (Node.js) 中,我以美分 (*100) 为单位保存价格,并且始终不包括增值税。
However, on the frontend we would like the user to be able to define their prices including VAT.但是,在前端,我们希望用户能够定义他们的价格,包括增值税。

So what we do is as follows:所以我们的做法如下:
Price incl VAT: 10,00价格含增值税:10,00
Formula to incl VAT: (10*100)/1.21 which gives 826.4462809917355 Math.round it to: 826 The 826 is the price excl VAT which we save in the database.含增值税的公式: (10*100)/1.21得出826.4462809917355为: 826 826是我们保存在数据库中的不含增值税的价格。
But when we add the VAT (21%) to it, then it becomes this:但是当我们加上增值税 (21%) 时,就会变成这样:

(826/100)*1.21

Which will become 999.4599999999999 .这将成为999.4599999999999
Math.round makes 999 of it. Math.round 做了 999 个。
So when our users filling in 10,00 as price incl VAT.因此,当我们的用户填写 10,00 作为价格(含增值税)时。 It will end up being 9,99它最终将是 9,99

The solution I went with is to increase the multiplier (100) to 100000.我采用的解决方案是将乘数 (100) 增加到 100000。
Haven't had any issues with it since.从那以后就没有任何问题了。 Probably not a 100% solution, as I suppose this only reduces the change of issues.可能不是 100% 的解决方案,因为我认为这只会减少问题的变化。

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

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