FileDocCategorySizeDatePackage
Round.javaAPI DocExample925Sat Mar 06 16:17:32 GMT 2004None

Round

public class Round extends Object
Demonstrate our own version round().
author
Ian F. Darwin, http://www.darwinsys.com/
version
$Id: Round.java,v 1.4 2004/03/06 22:17:32 ian Exp $

Fields Summary
public static final double
THRESHOLD
We round a number up if its fraction exceeds this threshold.
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)

		for (double d = 0.1; d<=1.0; d+=0.01) {
			System.out.println("My way:  " + d + "-> " + round(d));
			System.out.println("Math way:" + d + "-> " + Math.round(d));
		}
	
static intround(double d)

	/* 
	 * Round floating values to integers.
	 * @Return the closest int to the argument.
	 * @param d A non-negative values to be rounded.
	 */
	    
		if (d < 0) {
			throw new IllegalArgumentException("Value must be non-negative");
		}
		int di = (int)Math.floor(d);	// integral value below (or ==) d
		if ((d - di) > THRESHOLD) {
			return di + 1;
		} else {
			return di;
		}