javaint接收float_Java Float floatToIntBits()用法及代码示例

Float类中的floatToIntBits()方法是Java中的内置函数,它根据IEEE 754浮点“single format”位布局返回指定浮点值的表示形式。

用法:

public static int floatToIntBits(float val)

参数:该方法仅接受一个参数val,该参数指定要转换为整数位的浮点数。

返回值:该函数返回表示浮点数的整数位。以下是特殊情况:

如果参数为正无穷大,则结果为0x7f800000。

如果参数为负无穷大,则结果为0xff800000。

如果参数为NaN,则结果为0x7fc00000。

以下示例程序旨在说明Float.floatToIntBits()方法的使用:

示例1:

// Java program to demonstrate

// Float.floatToIntBits() method

import java.lang.*;

class Gfg1 {

public static void main(String args[])

{

float val = 1.5f;

// function call

int answer = Float.floatToIntBits(val);

// print

System.out.println(val + " in int bits: "

+ answer);

}

}

输出:

1.5 in int bits: 1069547520

示例2:

// Java program to demonstrate

// Float.floatToIntBits() method

import java.lang.*;

class Gfg1 {

public static void main(String args[])

{

float val = Float.POSITIVE_INFINITY;

float val1 = Float.NEGATIVE_INFINITY;

float val2 = Float.NaN;

// function call

int answer = Float.floatToIntBits(val);

// print

System.out.println(val + " in int bits: "

+ answer);

// function call

answer = Float.floatToIntBits(val1);

// print

System.out.println(val1 + " in int bits: "

+ answer);

// function call

answer = Float.floatToIntBits(val);

// print

System.out.println(val2 + " in int bits: "

+ answer);

}

}

输出:

Infinity in int bits: 2139095040

-Infinity in int bits: -8388608

NaN in int bits: 2139095040

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值