ChoiceFormat nextDouble(double, boolean) method in Java with Examples

The nextDouble(double, boolean) method of java.text.ChoiceFormat class is used to get the double value just greater than the passed double value if passed Boolean value is true or it will return the double value just lesser than the passed double value if passed Boolean value is false.

Syntax:

public static double nextDouble(double d, boolean positive)

Parameter: This method accepts following parameter as follow

  • d:- it takes double value whose just greater or lesser value is to be found.
  • positive:- it takes Boolean value which check that the returned double value will be grater or smaller.

Return Value: This method returns the double value just greater than the passed double value if passed Boolean value is true or it will return the double value just lesser than the passed double value if passed Boolean value is false.

Below are the examples to illustrate the nextDouble() method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Next Double value : 21.999999999999996

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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() mehtod
        getValue(1.23d, true);
        getValue(10d, false);
        getValue(-12d, true);
        getValue(1.2f, false);
        getValue(50, true);
    }
  
    // defining getValue() mehtod
    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);
    }
}

chevron_right


Output:

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

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#nextDouble-double-boolean-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.