简体   繁体   English

如何在Java中找到BigDecimal的二进制等效项?

[英]How Does One Find the Binary Equivalent of a BigDecimal in Java?

There aren't any any existing answers for my question on Stock Exchange or anywhere else for that matter, at least not any that I could find yet. 对于我在联交所或其他任何地方的问题,目前没有任何答案,至少没有我能找到的答案。 If this IS a duplicate, please include a link to the original answer. 如果重复,请提供原始答案的链接。

Anyways, I'm programming an application in Java that converts a BigDecimal value into its binary value. 无论如何,我正在用Java编写一个将BigDecimal值转换为其二进制值的应用程序。 I've successfully done this using a BigInteger applying the toString(int radix) method, and setting the radix to two. 我已经成功使用BigInteger应用toString(int radix)方法,并将radix设置为2,成功完成了此操作。 However, the toString() method for a BigDecimal does not accept a radix parameter. 但是, BigDecimaltoString()方法不接受radix参数。

I have considered parsing the BigDecimal into a double and using one of the Stock Exchange answers on getting a double 's binary form. 我已经考虑过将BigDecimal解析为double并在获取double的二进制形式时使用联交所的答案之一。

Please help :) 请帮忙 :)

Part of the issue is that this question is sort of nonsensical: many BigDecimal s have no representation as binary fractions; 问题的部分原因在于,这个问题有点荒谬:许多BigDecimal都不以二进制分数表示; eg 0.1 is well known to have no exact binary representation. 例如,众所周知0.1没有精确的二进制表示形式。

If you just want to turn it into bits somehow , and you don't care the actual details of the representation, then call BigDecimal.unscaledValue().toByteArray() and combine that with BigDecimal.scale() . 如果您只想以某种方式将其转换为位,并且不关心表示的实际细节,则调用BigDecimal.unscaledValue().toByteArray()并将其与BigDecimal.scale()

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

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