📜  Java中的浮点 shortValue() 方法及示例

📅  最后修改于: 2022-05-13 01:55:45.740000             🧑  作者: Mango

Java中的浮点 shortValue() 方法及示例

Float 类中的shortValue()方法是Java中的内置方法,它在类型转换后返回通过调用对象指定的值作为 short。
语法

public short shortValue()

返回值:它以short形式返回 FloatObject 的值。
下面的程序说明了Java中的shortValue()方法:
方案一:

Java
// Java code to demonstrate
// Float shortValue() method
 
class GFG {
    public static void main(String[] args)
    {
 
        // Float value
        Float a = 17.47f;
 
        // wrapping the Float value
        // in the wrapper class Float
        Float b = new Float(a);
 
        // shortValue of the Float Object
        short output = b.shortValue();
 
        // print shorting the output
        System.out.println("Short value of "
                           + b + " is : " + output);
    }
}


Java
// Java code to demonstrate
// Float shortValue() method
 
class GFG {
    public static void main(String[] args)
    {
 
        String value = "54.1f";
 
        // wrapping the Float value
        // in the wrapper class Float
        Float b = new Float(value);
 
        // shortValue of the Float Object
        short output = b.shortValue();
 
        // print shorting the output
        System.out.println("Short value of "
                           + b + " is : " + output);
    }
}


输出:
Short value of 17.47 is : 17

方案二:

Java

// Java code to demonstrate
// Float shortValue() method
 
class GFG {
    public static void main(String[] args)
    {
 
        String value = "54.1f";
 
        // wrapping the Float value
        // in the wrapper class Float
        Float b = new Float(value);
 
        // shortValue of the Float Object
        short output = b.shortValue();
 
        // print shorting the output
        System.out.println("Short value of "
                           + b + " is : " + output);
    }
}
输出:
Short value of 54.1 is : 54

参考: https: Java/lang/Float.html#shortValue()