Java Double类相关方法调用示例

x33g5p2x  于2021-08-19 转载在 Java  
字(3.0k)|赞(0)|评价(0)|浏览(164)

Double类在一个对象中封装了一个原始类型为double的值。一个Double类型的对象包含一个类型为double的单一字段。

此外,这个类还提供了一些方法,用于将双数转换为String,将String转换为双数,以及其他在处理双数时有用的常数和方法。

Double类构造函数

  • Double(double value) - 构建一个新分配的Double对象,代表原始的双倍数参数。
  • Double(String s) - 构建一个新分配的Double对象,表示由字符串表示的双倍类型的浮点值。
    例子:
double b = 55.05;
String str = "45";

// Construct two Double objects
Double x = new Double(b);
Double y = new Double(str);

Double类方法

类图显示了Integer类所提供的API/方法的列表。

在这篇文章中,我们将学习Integer封装类的几个重要方法。

**例子:**这个程序演示了Doublewrapper类方法的使用。

package com.javaguides.corejava.wrapperclasses;

//*/*
/* This class to demonstrate Double wrapper class methods
/* @author javaguides.net
/*
/*/
public class DoubleClassExample {
 public static void main(String[] args) {
  double b = 55.05;
  String str = "45";

  // Construct two Double objects
  Double x = new Double(b);
  Double y = new Double(str);

  // toString()
  System.out.println("toString(b) = " + Double.toString(b));

  // valueOf()
  // return Double object
  Double z = Double.valueOf(b);
  System.out.println("valueOf(b) = " + z);
  z = Double.valueOf(str);
  System.out.println("ValueOf(bb) = " + z);

  // parseDouble()
  // return primitive double value
  double zz = Double.parseDouble(str);
  System.out.println("parseDouble(bb) = " + zz);

  System.out.println("bytevalue(x) = " + x.byteValue());
  System.out.println("shortvalue(x) = " + x.shortValue());
  System.out.println("intvalue(x) = " + x.intValue());
  System.out.println("longvalue(x) = " + x.longValue());
  System.out.println("doublevalue(x) = " + x.doubleValue());
  System.out.println("floatvalue(x) = " + x.floatValue());

  int hash = x.hashCode();
  System.out.println("hashcode(x) = " + hash);

  boolean eq = x.equals(y);
  System.out.println("x.equals(y) = " + eq);

  int e = Double.compare(x, y);
  System.out.println("compare(x,y) = " + e);

  int f = x.compareTo(y);
  System.out.println("x.compareTo(y) = " + f);

  Double d = Double.valueOf("1010.54789654123654");
  System.out.println("isNaN(d) = " + d.isNaN());

  System.out.println("Double.isNaN(45.12452) = " + Double.isNaN(45.12452));

  // Double.POSITIVE_INFINITY stores
  // the positive infinite value
  d = Double.valueOf(Double.POSITIVE_INFINITY + 1);
  System.out.println("Double.isInfinite(d) = " + Double.isInfinite(d.doubleValue()));

  double dd = 10245.21452;
  System.out.println("Double.toString(dd) = " + Double.toHexString(dd));

  long double_to_long = Double.doubleToLongBits(dd);
  System.out.println("Double.doubleToLongBits(dd) = " + double_to_long);

  double long_to_double = Double.longBitsToDouble(double_to_long);
  System.out.println("Double.LongBitsToDouble(double_to_long) = " + long_to_double);
 }

}

输出。

toString(b) = 55.05
valueOf(b) = 55.05
ValueOf(bb) = 45.0
parseDouble(bb) = 45.0
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.05
floatvalue(x) = 55.05
hashcode(x) = 640540672
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1
isNaN(d) = false
Double.isNaN(45.12452) = false
Double.isInfinite(d) = true
Double.toString(dd) = 0x1.4029b7564302bp13
Double.doubleToLongBits(dd) = 4666857980575363115
Double.LongBitsToDouble(double_to_long) = 10245.21452

参考资料

https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html

相关文章

热门文章

更多