Java Number.longValue()方法

longValue()  以 long 形式返回指定的数值

1 语法

public abstract int longValue()  

2 参数

3 返回值

 以 long 形式返回指定的数值

4 示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.longValue()的例子
 */
public class Demo {
    
    public static void main(String[] args) {
        Number x = 666655599;
        long y = 2550;
        Long result = x.longValue()/y;
        System.out.println("Value is = "+result);
    }
}

输出结果为:

Value is = 261433

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.longValue()的例子
 */
public class Demo {

    public static void main(String[] args) {
        Byte B = 85;
        long l = B.longValue();
        System.out.println(l);
        Short S = 4101;
        l = S.longValue();
        System.out.println(l);
        Integer I = 59;
        l = I.longValue();
        System.out.println(l);
        Double D = 52134.232676;
        l = D.longValue();
        System.out.println(l);   //输出 : 52134  (值被截取)
    }
}

输出结果为:

85
4101
59
52134

6 示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.longValue()的例子
 */
public class Demo {

    public static void main(String[] args) {
        Number num1 = 298689.9549;
        //将Number的值返回为long
        System.out.println("Output Number as long: " + num1.longValue());
        Float num2 = -98723886f;
        //将此float的值返回为long
        System.out.println("Output Float as long: " + num2.longValue());
    }
}

输出结果为:

Output Number as long: 298689
Output Float as long: -98723888

 

热门文章

优秀文章