float floatValue()

优质
小牛编辑
127浏览
2023-12-01

描述 (Description)

java.math.BigDecimal.floatValue()将此BigDecimal转换为float。 如果此BigDecimal的大小表示为浮点数,则它将根据需要转换为Float.NEGATIVE_INFINITY或Float.POSITIVE_INFINITY。 当返回值有限时,转换也可能丢失有关BigDecimal值精度的信息。

声明 (Declaration)

以下是java.math.BigDecimal.floatValue()方法的声明。

public Float floatValue()

指定 (Specified by)

Number floatValue。

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回BigDecimal对象的float值。

异常 (Exception)

NA

例子 (Example)

以下示例显示了math.BigDecimal.floatValue()方法的用法。

package cn.xnip;
import java.math.*;
public class BigDecimalDemo {
   public static void main(String[] args) {
      // create a BigDecimal object
      BigDecimal bg;
      // create a Float object
      Float f;
      bg = new BigDecimal("1234.123486");
      // assign the converted value of bg to f
      f = bg.floatValue();
      String str = "Float value of " + bg + " is " + f;
      // print f value
      System.out.println( str );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Float value of 1234.123486 is 1234.1235