Java.math.BigDecimal.floatValue()方法



描述

Java.math.BigDecimal.floatValue()將此BigDecimal轉換為float。如果此BigDecimal具有太大作為float表示的幅度,則將其轉換為Float.NEGATIVE_INFINITY或Float.POSITIVE_INFINITY(視具體情況而定)。當返回值有限時,轉換也會丟失有關BigDecimal值精度的資訊。

宣告

以下是Java.math.BigDecimal.floatValue()方法的宣告。

public Float floatValue()

由以下指定的

Number類中的floatValue。

引數

返回值

此方法返回BigDecimal物件的float值。

異常

示例

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

package com.tutorialspoint;

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
java_math_bigdecimal.htm
廣告