Incrementing a Double by the Smallest Possible Amount


 

Incrementing a Double by the Smallest Possible Amount

In this section, you will earn how to increment the double value by the smallest possible amount.

In this section, you will earn how to increment the double value by the smallest possible amount.

Incrementing a Double by the Smallest Possible Amount

In this section, you will earn how to increment the double value by the smallest possible amount.

To format a range of numbers, java.text.* package introduces the class ChoiceFormat. This class is a concrete subclass of NumberFormat that maps numerical ranges to strings, or formats. Here we have to find the  largest double value less than the specified double value.

In the given example, first of all, we have specified a value of double type. Now to determine a double value by the smallest possible amount, we have invoked the previousDouble() method of ChoiceFormat class. This method specify the limits used by a ChoiceFormat and generate the greatest double value less than the specified double value.

Here is the code:

import java.text.*;

public class IncrementDouble {
	public static void main(String[] args) {
		double d = 1.5;
		double incd = ChoiceFormat.previousDouble(d);
		System.out.println(incd);
	}
}

Output:

1.4999999999999998

Ads