// UTILITY CLASS // Frans Coenen // University of Liverpool // 19 April 2013 import java.text.DecimalFormat; class Utility { /** Converts the given double to a two decimal place double. @param doubleNum the given double. @return the given double converted to a two decimal place double. */ public static double twoDecPlaces(double doubleNum) { int integerNum = (int) ((doubleNum+0.005)*100.0); double newDoubleNum = (double) integerNum/100.0; return(newDoubleNum); } /** Alternative 1: Uses DecimalFormat class to converts the given double to a two decimal place double but represented as a string. @param doubleNum the given double. @return the given double converted to a two decimal place string. */ public static String twoDecPlacesAlt1(double doubleNum) { DecimalFormat df = new DecimalFormat("#.##"); return(df.format(doubleNum)); } /** Alternative 2: Uses format method in String class to converts the given double to a two decimal place double but represented as a string. @param doubleNum the given double. @return the given double converted to a two decimal place string. */ public static String twoDecPlacesAlt2(double doubleNum) { return(String.format("%.3g%n",doubleNum)); } }