Java中的 ChoiceFormat nextDouble(double, boolean) 方法及示例
Java.text.ChoiceFormat类的nextDouble(double, boolean)方法用于在传递的布尔值为 true 时获取刚好大于传递的双精度值的双精度值,如果传递的布尔值为真,则返回比传递的双精度值稍小的双精度值传递的布尔值是假的。
句法:
public static double nextDouble(double d, boolean positive)
参数:此方法接受以下参数,如下所示
- d:-它取 double 值,要找到更大或更小的值。
- 正数:-它采用布尔值来检查返回的双精度值是否更大或更小。
返回值:如果传递的布尔值为真,则此方法返回比传递的双精度值稍大的双精度值,如果传递的布尔值为假,它将返回比传递的双精度值稍小的双精度值。
下面是说明nextDouble()方法的示例:
示例 1:
Java
// Java program to demonstrate
// nextDouble() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// getting double value just
// greater than the passed value
// using nextDouble() method
double value
= ChoiceFormat.nextDouble(22, false);
// display the result
System.out.print("Next Double value : "
+ value);
}
}
Java
// Java program to demonstrate
// nextDouble() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// calling getValue() method
getValue(1.23d, true);
getValue(10d, false);
getValue(-12d, true);
getValue(1.2f, false);
getValue(50, true);
}
// defining getValue() method
public static void getValue(double doub,
boolean bool)
{
// getting double value just
// greater than the passed value
// using nextDouble() method
double value
= ChoiceFormat.nextDouble(doub, bool);
// display the result
if (bool)
System.out.println("Just greater than "
+ doub + ": "
+ value);
else
System.out.println("Just lesser than "
+ doub + ": "
+ value);
}
}
输出:
Next Double value : 21.999999999999996
示例 2:
Java
// Java program to demonstrate
// nextDouble() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// calling getValue() method
getValue(1.23d, true);
getValue(10d, false);
getValue(-12d, true);
getValue(1.2f, false);
getValue(50, true);
}
// defining getValue() method
public static void getValue(double doub,
boolean bool)
{
// getting double value just
// greater than the passed value
// using nextDouble() method
double value
= ChoiceFormat.nextDouble(doub, bool);
// display the result
if (bool)
System.out.println("Just greater than "
+ doub + ": "
+ value);
else
System.out.println("Just lesser than "
+ doub + ": "
+ value);
}
}
输出:
Just greater than 1.23: 1.2300000000000002
Just lesser than 10.0: 9.999999999999998
Just greater than -12.0: -11.999999999999998
Just lesser than 1.2000000476837158: 1.2000000476837156
Just greater than 50.0: 50.00000000000001
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/ChoiceFormat.html#nextDouble-double-boolean-