问题描述
我正在使用Tomcat/JSTL/MySQL/etc建立一个新的电子商务平台.
I am building a new ecommerce platform using Tomcat/JSTL/MySQL/etc.
我知道与使用浮点计算有关的各种问题-例如12.3456000000000789 ...我知道舍入还是截断也是一个问题.
I am aware of the various issues related to using floating point calculations - e.g. 12.3456000000000789... I know rounding vs truncation can also be an issue.
如果不将所有业务逻辑都移到Java类中,那么在保持体系结构简单的同时对货币进行可靠计算的最佳方法是什么?
Without moving all of the business logic into Java classes, what is the best way to make reliable calculations on currency while still keeping the architecture simple?
我对可能性的想法包括:
My thoughts on possibilities include:
- 隔离问题计算并单独处理
- 将数字表示为longs,并使用美分进行计算
- 使用BigDecimal并使用Java进行所有计算
- 尽可能将数字存储在字符串中
- 使用< fmt:formatNumber var ="...而不是< c:set var =" ...
我可能不是第一个这样做的人.
I might not be the first to doing this.
最直接的方法是什么?
推荐答案
不是答案.无论您使用哪种技术,都可以从列表中的其他地方获取.
Not an answer. Whatever technique you use will come from elsewhere in your list.
可行但笨拙,在金融中有些情况下,百分之一的分数是有效的:例如外汇.
Feasible but clumsy, and there are cases in finance where fractions of a cent are valid: for example, foreign exchange.
这就是答案.更好的是,在数据库中进行计算.
This is the answer. Better still, do the calculations in the database.
不是答案.您仍然需要计算,并且当您这样做时,该技术将来自列表中的其他地方.
Not an answer. You still have to calculate, and when you do the technique will come from elsewhere in your list.
不是答案.当然,您将必须使用formatNumber,但是它并没有解决您将如何计算的问题.我看不出c:set与它有什么关系,它做了另外一件事.它不能替代numberFormat.
Not an answer. You will certainly have to use formatNumber, but it doesn't address how you are going to calculate. I don't see what c:set has to do with it, it does a different thing. It's not an alternative to numberFormat.
这篇关于使用JSTL/Tomcat进行财务计算的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!