简体   繁体   English

如何在 Java 中将双精度乘以 BigInteger

[英]How to multiply a double with a BigInteger in Java

I want to multiply for example;例如,我想乘法; 0.1 * 10^30 0.1 * 10^30

How do I achieve this?我如何实现这一目标?

0.1 is a double 10^30 is a BigInteger 0.1 是一个 double 10^30 是一个 BigInteger

Human input will be converted into a double value but the value needs to be multiplied by 10^30 for computation.人工输入将被转换为双精度值,但该值需要乘以 10^30 进行计算。

Human input can be 0.09172349 for example.例如,人工输入可以是 0.09172349。

Your best bet is to convert both to java.math.BigDecimal .最好的办法是将两者都转换为java.math.BigDecimal

Please see the caveats and advice at the Javadoc for BigDecimal(double) .请参阅Javadoc 中 BigDecimal(double)的警告和建议。 You mention "human input";你提到“人工输入”; if you can take that human input directly as-is, and avoid bringing double into the picture at all, then you should.如果您可以直接按原样接受人工输入,并且完全避免在图片中添加double ,那么您应该这样做。

[…] the value needs to be multiplied by 10^30 for computation. [...] 该值需要乘以 10^30 进行计算。

If you are always multiplying by 10 30 — that's a fixed constant — then you should use BigDecimal.movePointRight(int) .如果您总是乘以 10 30 — 这是一个固定常数 — 那么您应该使用BigDecimal.movePointRight(int)

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

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