How to identify BigDecimal in double in Java

I'm writing a financial application. I get a value as a string. I need to check whether the value is suitable for double or BigDecimal without losing data

What is the best way to determine whether a string value should be converted to double or BigDecimal?

Solution

If it's a financial value, you should almost always use BigDecimal - or an integer with an implied scaling value (for example, store cents instead of dollars)

Floating point binary points (float, double) are almost never suitable for financial applications because values usually have exact decimal values that cannot be accurately represented in floating point binary points

This does not mean that double is a useless data type - it works well for natural continuous values such as distance This is not appropriate for "manual" discrete values usually specified in decimal

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>