有没有把Double转换成BigInteger? [英] is there anyway to convert from Double to BigInteger?

查看:2071
本文介绍了有没有把Double转换成BigInteger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有从 double 值转换为 BigInteger

double doubleValue = 64654679846513164.2;
BigInteger bigInteger = (BigInteger) doubleValue;

我尝试投递,但没有起作用。

I try to cast it but it didn't work.

推荐答案

BigInteger用于保存任意精度整数,而不是小数位。您可以使用 BigDecimal 类来保存双倍。

BigInteger is made for holding arbitrary precision integer numbers, not decimals. You can use the BigDecimal class to hold a double.

BigDecimal k = BigDecimal.valueOf(doublevalue);

一般来说,您不能将Java基元转换为另一个类。我知道的例外是扩展 Number 的类,例如 Long Integer 包装器类,允许您将int值转换为整数,依此类推。

In general, you cannot type cast a Java primitive into another class. The exceptions that I know about are the classes extending Number, such as the Long and Integer wrapper classes, which allow you to cast an int value into an Integer, and so on.

这篇关于有没有把Double转换成BigInteger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆