@Test public void testRoundToDecimals() { double a = 3; assertTrue(Utils.isClose(a, Utils.roundToDecimals(a, 0))); assertTrue(Utils.isClose(a, Utils.roundToDecimals(a, 1))); a = 3.00001; assertTrue(Utils.isClose(3, Utils.roundToDecimals(a, 4))); assertFalse(Utils.isClose(3, Utils.roundToDecimals(a, 5))); a = 3.14723; assertTrue(Utils.isClose(3.14, Utils.roundToDecimals(a, 2))); assertFalse(Utils.isClose(3.14, Utils.roundToDecimals(a, 3))); assertTrue(Utils.isClose(a, Utils.roundToDecimals(a, 8))); thrown.expect(IllegalArgumentException.class); Utils.roundToDecimals(a, -1); Utils.roundToDecimals(a, 9); }
public static double roundToDecimals(double num, int precision) { if (precision < 0 || precision > 8) { throw new IllegalArgumentException("Precision must be an int from 0 to 8."); } long multiplier = (long)Math.pow(10, (double)precision); return ((int)(num*multiplier))/(double)multiplier; }
Utils { public static double roundToDecimals(double num, int precision) { if (precision < 0 || precision > 8) { throw new IllegalArgumentException("Precision must be an int from 0 to 8."); } long multiplier = (long)Math.pow(10, (double)precision); return ((int)(num*multiplier))/(double)multiplier; } }
Utils { public static double roundToDecimals(double num, int precision) { if (precision < 0 || precision > 8) { throw new IllegalArgumentException("Precision must be an int from 0 to 8."); } long multiplier = (long)Math.pow(10, (double)precision); return ((int)(num*multiplier))/(double)multiplier; }  }
Utils { public static double roundToDecimals(double num, int precision) { if (precision < 0 || precision > 8) { throw new IllegalArgumentException("Precision must be an int from 0 to 8."); } long multiplier = (long)Math.pow(10, (double)precision); return ((int)(num*multiplier))/(double)multiplier; }  static String tokenToAuthString(String token); static void showToast(Context context, String errorMessage); static SharedPreferences.Editor getSharedPrefEditor(Context context); static SharedPreferences getSharedPref(Context context); static void logout(Context context); static Uri getNewImageUri(Context context); static boolean isLocalUrl(String url); static boolean isClose(double a, double b); static boolean objectEquals(Object a, Object b); static double roundToDecimals(double num, int precision); static boolean isValidYear(String s); static String yearString(int year); }
Utils { public static double roundToDecimals(double num, int precision) { if (precision < 0 || precision > 8) { throw new IllegalArgumentException("Precision must be an int from 0 to 8."); } long multiplier = (long)Math.pow(10, (double)precision); return ((int)(num*multiplier))/(double)multiplier; }  static String tokenToAuthString(String token); static void showToast(Context context, String errorMessage); static SharedPreferences.Editor getSharedPrefEditor(Context context); static SharedPreferences getSharedPref(Context context); static void logout(Context context); static Uri getNewImageUri(Context context); static boolean isLocalUrl(String url); static boolean isClose(double a, double b); static boolean objectEquals(Object a, Object b); static double roundToDecimals(double num, int precision); static boolean isValidYear(String s); static String yearString(int year);  }