java.lang.Number.floatValue() 方法返回指定數為float的值。這可能會涉及到舍入或截斷。
以下是java.lang.Number.floatValue()方法的宣告
public abstract float floatValue()
NA
此方法返回轉換後該物件表示為float型別的數值。
NA
下面的例子顯示lang.Number.floatValue()方法的使用。
package com.yiibai; public class NumberDemo { public static void main(String[] args) { // get a number as integer Integer x = new Integer(123456); // get a number as double Double y = new Double(9876); // print their value as float System.out.println("x as integer:" + x + ", x as float:" + x.floatValue()); System.out.println("y as double:" + y + ", y as float:" + y.floatValue()); } }
讓我們來編譯和執行上面的程式,這將產生以下結果:
x as integer:123456, x as float:123456.0 y as double:9876.0, y as float:9876.0