Share on Google+Share on Google+

Incrementing a Double by the Smallest Possible Amount

Advertisement
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

Advertisements

Advertisement

Posted on: February 16, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.