简体   繁体   English

如何在Java中识别Double中的BigDecimal

[英]How to identify BigDecimal from Double in Java

I'm writing a financial application where i'm getting a value as string and need to check if the value is can be fit into double or BigDecimal without data loss. 我正在编写一个财务应用程序,我得到一个值作为字符串,需要检查值是否适合double或BigDecimal而不会丢失数据。

What will be the best way to identify if the string value should be converted to a double or a BigDecimal? 确定字符串值是否应转换为double或BigDecimal的最佳方法是什么?

If it's a financial value, you should pretty much always use BigDecimal - or use an integer with an implicit scaling value (eg store cents instead of dollars). 如果它是一个财务价值,你几乎应该总是使用BigDecimal - 或者使用一个隐含缩放值的整数(例如存储美分而不是美元)。

Floating binary point ( float , double ) is almost never appropriate for financial applications, as the values usually have precise decimal values which aren't exactly representable in floating binary point. 浮点二进制点( floatdouble )几乎不适用于财务应用程序,因为值通常具有精确的十进制值,这些值在浮点二进制点中无法准确表示。

That doesn't mean double is a useless data type - it's fine for natural, continuous values such as distances. 这并不意味着double是一种无用的数据类型 - 它对于诸如距离之类的自然连续值很好。 It's just not good for "artificial" discrete values which are typically measured specified in decimal. 这对于通常以十进制指定的“人工”离散值来说是不合适的。

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

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