如何在 java 中打印没有科学记数法或零的“double”类型值?
How to print a “double” type value without the scientific notation or zeros in java?
我构建了一个使用双精度类型的程序,它运行良好,但我想要的是打印最终值及其所有数字。为了解释我在寻找什么,我写了这个例子:
/* sample number my program might generate */
Double Big = new Double ("2937093129380193810983901274934098138098390183");
/* Double.toString(double) method */
String reallyBigString = Double.toString(Big);
System.out.println(“Double.toString = " + reallyBigString);
/* Big Integer class */
BigInteger reallyBigInteger2 = BigDecimal.valueOf(Big).toBigInteger();
System.out.println("BigInteger = " + reallyBigInteger2);
/* BigDecimal class */
BigDecimal reallyBigInteger1 = new BigDecimal(Big);
System.out.println("BigDecimal = " + reallyBigInteger1);
我想得到什么:
the double real value = 2937093129380193810983901274934098138098390183
我得到的:
Double.toString = 2.937093129380194E45
BigInteger = 2937093129380194000000000000000000000000000000
BigDecimal = 2937093129380193767890297090901187942913409024
BigInteger
结果使用了一种四舍五入的值,我不需要这个。
我怎样才能获得正确的价值?
您没有使用正确的方法将 BigDecimal 转换为 BigInteger
BigInteger reallyBigInteger2 = new BigDecimal(Big).toBigInteger();
通过传递双精度值创建new BigDecimal() 对象,然后转换为 BigInteger 将给出正确的结果。
对于 BigDecimal:这有点棘手,因为它们不做同样的事情。 BigDecimal.valueOf(double) will use the canonical String representation 传入的 double 值实例化 BigDecimal 对象。换句话说:BigDecimal 对象的值将是您在 System.out.println(d).
时看到的值
但是,如果您使用 new BigDecimal(d),则 BigDecimal 将尝试尽可能准确地表示双精度值。这通常会导致存储比您想要的多得多的数字。严格来说,它比 valueOf() 更正确,但直观性差了很多。
正如@RealSkeptic 和@Ken Slade 所说,我需要从 String
而不是 Double
构建 BigInteger
,仅此而已:
/* the sample number converted to a String*/
String Big = new String ("2937093129380193810983901274934098138098390183");
/* BigInteger */
BigInteger reallyBigInteger = new BigInteger(Big);
System.out.println("BigInteger = " + reallyBigInteger);
结果是正确的:
BigInteger = 2937093129380193810983901274934098138098390183
然后我使用 BigInteger
和 String
类型从零开始重建程序。
感谢大家。
我构建了一个使用双精度类型的程序,它运行良好,但我想要的是打印最终值及其所有数字。为了解释我在寻找什么,我写了这个例子:
/* sample number my program might generate */
Double Big = new Double ("2937093129380193810983901274934098138098390183");
/* Double.toString(double) method */
String reallyBigString = Double.toString(Big);
System.out.println(“Double.toString = " + reallyBigString);
/* Big Integer class */
BigInteger reallyBigInteger2 = BigDecimal.valueOf(Big).toBigInteger();
System.out.println("BigInteger = " + reallyBigInteger2);
/* BigDecimal class */
BigDecimal reallyBigInteger1 = new BigDecimal(Big);
System.out.println("BigDecimal = " + reallyBigInteger1);
我想得到什么:
the double real value = 2937093129380193810983901274934098138098390183
我得到的:
Double.toString = 2.937093129380194E45
BigInteger = 2937093129380194000000000000000000000000000000
BigDecimal = 2937093129380193767890297090901187942913409024
BigInteger
结果使用了一种四舍五入的值,我不需要这个。
我怎样才能获得正确的价值?
您没有使用正确的方法将 BigDecimal 转换为 BigInteger
BigInteger reallyBigInteger2 = new BigDecimal(Big).toBigInteger();
通过传递双精度值创建new BigDecimal() 对象,然后转换为 BigInteger 将给出正确的结果。
对于 BigDecimal:这有点棘手,因为它们不做同样的事情。 BigDecimal.valueOf(double) will use the canonical String representation 传入的 double 值实例化 BigDecimal 对象。换句话说:BigDecimal 对象的值将是您在 System.out.println(d).
时看到的值但是,如果您使用 new BigDecimal(d),则 BigDecimal 将尝试尽可能准确地表示双精度值。这通常会导致存储比您想要的多得多的数字。严格来说,它比 valueOf() 更正确,但直观性差了很多。
正如@RealSkeptic 和@Ken Slade 所说,我需要从 String
而不是 Double
构建 BigInteger
,仅此而已:
/* the sample number converted to a String*/
String Big = new String ("2937093129380193810983901274934098138098390183");
/* BigInteger */
BigInteger reallyBigInteger = new BigInteger(Big);
System.out.println("BigInteger = " + reallyBigInteger);
结果是正确的:
BigInteger = 2937093129380193810983901274934098138098390183
然后我使用 BigInteger
和 String
类型从零开始重建程序。
感谢大家。