Java程序将Double转换为数字原始数据类型
假设我们在这里有Double对象。
Double obj = new Double("39.45");现在,如果要将此Double转换为短基本数据类型。为此,请使用内置shortValue()方法。
// converting to short primitive types short shortObj = obj.shortValue(); System.out.println(shortObj);
以相同的方式将Double转换为另一个数字原始数据类型int。为此,请使用内置intValue()方法。
// converting to int primitive types int intObj = obj.intValue(); System.out.println(intObj);
以下是将Double转换为数字基本类型short,int,float等的示例。
示例
public class Demo {
public static void main(String args[]) {
Double obj = new Double("39.45");
int intVal = obj.intValue();
System.out.println(intVal);
byte byteVal = obj.byteValue();
System.out.println(byteVal);
short shortVal = obj.shortValue();
System.out.println(shortVal);
float floatVal = obj.floatValue();
System.out.println(floatVal);
double doubleVal = obj.doubleValue();
System.out.println(doubleVal);
}
}输出结果
39 39 39 39.45 39.45
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短