任何方式将java.lang.Double
投射到java.lang.Integer
?
抛出异常
“java.lang.ClassCastException:java.lang.Double与java.lang.Integer不兼容”
答案 0 :(得分:529)
您需要使用方法intValue()显式获取int值,如下所示:
Double d = 5.25;
Integer i = d.intValue(); // i becomes 5
或
double d = 5.25;
int i = (int) d;
答案 1 :(得分:429)
Double
不是Integer
,因此演员无效。请注意Double
类与double
原语之间的差异。另请注意,Double
是Number
,so it has the method intValue
,您可以将其用作基元int
的值。
答案 2 :(得分:42)
我认为,如果不解释背后的陷阱和推理,就无法理解其他答案。
您无法直接将Integer
转换为Double
个对象。此外,Double
和Integer
是不可变对象,因此您无法以任何方式修改它们。
每个数字类都有原始替代(Double
vs double
,Integer
vs int
,. ..)。请注意,这些基元以小写字符开头(例如int
)。这告诉我们它们不是类/对象。这也意味着他们没有方法。相比之下,类(例如Integer
)就像这些基元周围的框/包装一样,这使得它们可以像对象一样使用它们。
要将Double
转换为Integer
,您需要遵循以下策略:
Double
对象转换为基元double
。 (=“拆箱”)double
转换为原始int
。 (=“cast”)int
转换回Integer
对象。 (=“拳击”)// starting point
Double myDouble = Double.valueOf(10.0);
// step 1: unboxing
double dbl = myDouble.doubleValue();
// step 2: casting
int intgr = (int) dbl;
// step 3: boxing
Integer val = Integer.valueOf(intgr);
实际上有一条捷径。您可以立即从Double
直接拆箱到原始int
。这样,您可以完全跳过第2步。
Double myDouble = Double.valueOf(10.0);
Integer val = Integer.valueOf(myDouble.intValue()); // the simple way
但是,上面的代码中没有涉及很多内容。 上面的代码不是空的。
Double myDouble = null;
Integer val = Integer.valueOf(myDouble.intValue()); // will throw a NullPointerException
// a null-safe solution:
Integer val = (myDouble == null)? null : Integer.valueOf(myDouble.intValue());
现在它适用于大多数值。但是,与Double
相比,整数的范围(最小/最大值)非常小。最重要的是,双打也可以保持“特殊值”,整数不能:
因此,根据应用程序的不同,您可能需要添加一些过滤以避免令人讨厌的异常。
然后,下一个缺点是舍入策略。默认情况下,Java将始终向下舍入。四舍五入在所有编程语言中都非常有意义。基本上Java只是丢掉了一些字节。在财务应用程序中,您肯定希望使用半向上舍入(例如:round(0.5) = 1
和round(0.4) = 0
)。
// null-safe and with better rounding
long rounded = (myDouble == null)? 0L: Math.round(myDouble.doubleValue());
Integer val = Integer.valueOf(rounded);
你可能很想在这里使用auto-(un)boxing,但我不愿意。如果你现在已经陷入困境,那么下一个例子也不会那么明显。如果您不了解自动(非)拳击的内部工作原因,请不要使用它。
Integer val1 = 10; // works
Integer val2 = 10.0; // doesn't work
Double val3 = 10; // doesn't work
Double val4 = 10.0; // works
Double val5 = null;
double val6 = val5; // doesn't work (throws a NullPointerException)
我想以下不应该是一个惊喜。但如果是,那么您可能想阅读一些关于使用Java进行转换的文章。
double val7 = (double) 10; // works
Double val8 = (Double) Integer.valueOf(10); // doesn't work
Integer val9 = (Integer) 9; // pure nonsense
另外,不要试图使用new Integer()
构造函数(正如其他一些答案所提出的那样)。 valueOf()
方法更好,因为它们使用缓存。使用这些方法是一个好习惯,因为它们会不时地为你节省一些记忆。
long rounded = (myDouble == null)? 0L: Math.round(myDouble.doubleValue());
Integer val = new Integer(rounded); // waste of memory
答案 3 :(得分:37)
我看到三种可能性。前两个截断数字,最后一个数字舍入到最接近的整数。
double d = 9.5;
int i = (int)d;
//i = 9
Double D = 9.5;
int i = Integer.valueOf(D.intValue());
//i = 9
double d = 9.5;
Long L = Math.round(d);
int i = Integer.valueOf(L.intValue());
//i = 10
答案 4 :(得分:21)
像这样:
Double foo = 123.456;
Integer bar = foo.intValue();
答案 5 :(得分:21)
实际上,最简单的方法是使用intValue()
。但是,这只返回整数部分;它没有做任何舍入。如果您希望Integer 为Double值,则需要执行以下操作:
Integer integer = Integer.valueOf((int) Math.round(myDouble)));
不要忘记空案例:
Integer integer = myDouble == null ? null : Integer.valueOf((int) Math.round(myDouble)));
Math.round()
处理奇怪的鸭子情况,如无穷大和NaN,相对优雅。
答案 6 :(得分:13)
double a = 13.34;
int b = (int) a;
System.out.println(b); //prints 13
答案 7 :(得分:7)
只需这样做......
Double d = 13.5578;
int i = d.intValue();
System.out.println(i);
答案 8 :(得分:6)
Double d = 100.00;
Integer i = d.intValue();
还应该补充一点,它适用于自动装箱。
否则,你得到一个int(原始),然后可以从那里得到一个Integer:
Integer i = new Integer(d.intValue());
答案 9 :(得分:5)
在intValue()
对象上拨打Double
。
答案 10 :(得分:3)
试试这个
double doubleValue = 6.5;Double doubleObj = new Double(doubleValue);int intResult = doubleObj.intValue();
答案 11 :(得分:2)
Double和Integer分别是double和int的Java原语的包装类。你可以在这些之间施放,但你将失去浮点。也就是说,转换为int的5.4将是5.如果你将其转换回来,它将是5.0。
答案 12 :(得分:1)
或者,可以先转换为原始 let a = 5, b = 15; //initial values
//in the second line of your code is...
1. (++b) is here `pre-increment` add 16 here and b value is 16 now.
2. As usual add 7
3. (b++) is here `post-increment` add 16 here and b value is 17 now.
4. (b--) is here `post-decrement` add 17 here and b value is 16 now.
5. += is sum and assignment operator.
now the result will be a = 5 + 16 + 7 + 16 + 17 (61)
result:
last value of a is : 61
&
last value of b is: 16
,然后转换为 double
,让它自动装箱为 int
。
Integer
答案 13 :(得分:0)
使用doubleNumber.intValue();
方法。
答案 14 :(得分:0)
只需使用Double的intValue方法
wpcf7_enqueue_scripts
答案 15 :(得分:0)
高效的内存,因为它将共享已经创建的Double实例。
import tkinter as tk
def justify_text(option):
"""Change text justify setting."""
text.tag_configure('justify', justify=option)
# make sure all text has the tag
text.tag_add('justify', '1.0', 'end')
root = tk.Tk()
options = ["right", "center", "left"]
var = tk.StringVar(root, options[0])
menu = tk.OptionMenu(root, var, *options, command=justify_text)
menu.pack()
text = tk.Text(root)
text.insert('1.0', "Please enter your notes here", 'justify')
text.tag_configure('justify', justify=options[0])
text.pack()
root.mainloop()
答案 16 :(得分:-2)
它为我工作。试试这个:
double od = Double.parseDouble("1.15");
int oi = (int) od;