📌  相关文章
📜  Java中的 DoubleAccumulator floatValue() 方法及示例

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

Java中的 DoubleAccumulator floatValue() 方法及示例

Java.DoubleAccumulator.floatValue()是Java中的一个内置方法,它在缩小原始转换后将当前值作为浮点数返回。这意味着初始数据类型是 double ,它被显式转换为 float 类型。

句法:

public float floatValue()

参数:该函数不接受任何参数。

返回值:该方法返回此对象表示的数值转换为float类型后的数值。

下面的程序说明了上述方法:

方案一:

// Java program to demonstrate the
// floatValue() method
  
import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
  
public class GFG {
    public static void main(String args[])
    {
  
        DoubleAccumulator num
            = new DoubleAccumulator(
                Double::sum, 0L);
  
        // accumulate operation on num
        num.accumulate(42);
        num.accumulate(10);
  
        // Print before floatValue operation
        System.out.println("Old value is: "
                           + num);
  
        // Print after floatValue operation
        System.out.println("Current float value is: "
                           + num.floatValue());
    }
}
输出:
Old value is: 52.0
Current float value is: 52.0

方案二:

// Java program to demonstrate the
// floatValue() method
  
import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
  
public class GFG {
    public static void main(String args[])
    {
  
        DoubleAccumulator num
            = new DoubleAccumulator(
                Double::sum, 0L);
  
        // accumulate operation on num
        num.accumulate(7);
        num.accumulate(85);
  
        // Print before floatValue operation
        System.out.println("Old value is: "
                           + num);
  
        // Print after floatValue operation
        System.out.println("Current float value is: "
                           + num.floatValue());
    }
}
输出:
Old value is: 92.0
Current float value is: 92.0