Which data type to use for manipulating currency

早过忘川 提交于 2019-12-23 09:32:37

问题


I am trying to decide which data type shall i use for a financial application.

I have read that Double or BigDecimal should be used. And i am confused between them. Any help in this regard will be highly appreciated


回答1:


You almost certainly don't want to use floating-point types (double, float, Double, Float) to handle monetary amounts, especially if you will be performing computations on them. The main reason for this is that there are many simple-looking numbers that cannot be represented exactly as a double et al. One such number is 0.1.

BigDecimal is therefore a much better choice for this use case.




回答2:


Use BigDecimal, it's a lot better than Double for financial stuff. See here the accepted answer for a similar question: Double vs. BigDecimal?




回答3:


For most applications, a simple long will do: when representing cents, this will handle amounts up to 10^17 {Dollars, Euros, whatever}.




回答4:


BigDecimal isn't only best choice, BigDecimal is designed for such case.



来源:https://stackoverflow.com/questions/15865012/which-data-type-to-use-for-manipulating-currency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!