@Test public void getAccurateDivide() throws Exception { int a = 4, b = -7, n = 1; logger.info("getAccurateDivide {}/{}, n is: {} result is: {}", a, b, n, DataUtil.getAccurateDivide(a, b, n)); a = 3; b = 7; n = 30; logger.info("getAccurateDivide {}/{}, n is: {} result is: {}", a, b, n, DataUtil.getAccurateDivide(a, b, n)); }
public static String getAccurateDivide(int a, int b, int n) { if (b == 0 || n < 0) { throw new IllegalArgumentException(); } String symbol = ""; if (a > 0 && b < 0 || a < 0 && b > 0) { symbol = "-"; } StringBuilder sBuilder = new StringBuilder(symbol); int intPrefix = a / b; a = Math.abs(a); b = Math.abs(b); if (n == 0) { if (a % b * 10 / b >= 5) { sBuilder.append(intPrefix + 1); } else { sBuilder.append(intPrefix); } } else { sBuilder.append(intPrefix).append("."); if (n == 1) { a = a % b * 10; } else { for (int i = 0; i < n - 1; i++) { a = a % b * 10; sBuilder.append(a / b); } } int c = a / b; a = a % b * 10; if (a / b >= 5) { sBuilder.append(c + 1); } else { sBuilder.append(c); } } return sBuilder.toString(); }
DataUtil { public static String getAccurateDivide(int a, int b, int n) { if (b == 0 || n < 0) { throw new IllegalArgumentException(); } String symbol = ""; if (a > 0 && b < 0 || a < 0 && b > 0) { symbol = "-"; } StringBuilder sBuilder = new StringBuilder(symbol); int intPrefix = a / b; a = Math.abs(a); b = Math.abs(b); if (n == 0) { if (a % b * 10 / b >= 5) { sBuilder.append(intPrefix + 1); } else { sBuilder.append(intPrefix); } } else { sBuilder.append(intPrefix).append("."); if (n == 1) { a = a % b * 10; } else { for (int i = 0; i < n - 1; i++) { a = a % b * 10; sBuilder.append(a / b); } } int c = a / b; a = a % b * 10; if (a / b >= 5) { sBuilder.append(c + 1); } else { sBuilder.append(c); } } return sBuilder.toString(); } }
DataUtil { public static String getAccurateDivide(int a, int b, int n) { if (b == 0 || n < 0) { throw new IllegalArgumentException(); } String symbol = ""; if (a > 0 && b < 0 || a < 0 && b > 0) { symbol = "-"; } StringBuilder sBuilder = new StringBuilder(symbol); int intPrefix = a / b; a = Math.abs(a); b = Math.abs(b); if (n == 0) { if (a % b * 10 / b >= 5) { sBuilder.append(intPrefix + 1); } else { sBuilder.append(intPrefix); } } else { sBuilder.append(intPrefix).append("."); if (n == 1) { a = a % b * 10; } else { for (int i = 0; i < n - 1; i++) { a = a % b * 10; sBuilder.append(a / b); } } int c = a / b; a = a % b * 10; if (a / b >= 5) { sBuilder.append(c + 1); } else { sBuilder.append(c); } } return sBuilder.toString(); }  }
DataUtil { public static String getAccurateDivide(int a, int b, int n) { if (b == 0 || n < 0) { throw new IllegalArgumentException(); } String symbol = ""; if (a > 0 && b < 0 || a < 0 && b > 0) { symbol = "-"; } StringBuilder sBuilder = new StringBuilder(symbol); int intPrefix = a / b; a = Math.abs(a); b = Math.abs(b); if (n == 0) { if (a % b * 10 / b >= 5) { sBuilder.append(intPrefix + 1); } else { sBuilder.append(intPrefix); } } else { sBuilder.append(intPrefix).append("."); if (n == 1) { a = a % b * 10; } else { for (int i = 0; i < n - 1; i++) { a = a % b * 10; sBuilder.append(a / b); } } int c = a / b; a = a % b * 10; if (a / b >= 5) { sBuilder.append(c + 1); } else { sBuilder.append(c); } } return sBuilder.toString(); }  static int getMatrixHeavy(int[][] matrix, int m, int n); static int maxSequenceSum(int[] array); static int fibonacciRecursive(int n); static int fibonacci(int n); static int findMaxUnContinuousSum(int[] array); static void findContinuousSequenceMethod1(int n); static void findContinuousSequenceMethod2(int n); static void findContinuousSequenceMethod3(int n); static void findContinuousSequenceMethod4(int n); static void pickNum(int[] originArray, int n, int m, int[] selectArray, int startIndex, boolean inorder); static int binarySearch(int[] array, int key); static int findFirstEqual(int[] array, int key); static int findFirstEqualLarger(int[] array, int key); static int findFirstLarger(int[] array, int key); static int findLastEqual(int[] array, int key); static int findLastEqualSmaller(int[] array, int key); static List<Integer> shufflePump(List<Integer> origin); static List<Integer> shuffleSwap(List<Integer> origin); static int[] shuffleSwap(int[] origin); static String getAccurateDivide(int a, int b, int n); static Set<Integer> getPrimeNumInefficient(int range); static Set<Integer> getPrimeNumEfficient(int range); static int getIntervalDays(int year, int month, int day); static int pageHelper(int n, int m); static boolean isOdd(int num); }
DataUtil { public static String getAccurateDivide(int a, int b, int n) { if (b == 0 || n < 0) { throw new IllegalArgumentException(); } String symbol = ""; if (a > 0 && b < 0 || a < 0 && b > 0) { symbol = "-"; } StringBuilder sBuilder = new StringBuilder(symbol); int intPrefix = a / b; a = Math.abs(a); b = Math.abs(b); if (n == 0) { if (a % b * 10 / b >= 5) { sBuilder.append(intPrefix + 1); } else { sBuilder.append(intPrefix); } } else { sBuilder.append(intPrefix).append("."); if (n == 1) { a = a % b * 10; } else { for (int i = 0; i < n - 1; i++) { a = a % b * 10; sBuilder.append(a / b); } } int c = a / b; a = a % b * 10; if (a / b >= 5) { sBuilder.append(c + 1); } else { sBuilder.append(c); } } return sBuilder.toString(); }  static int getMatrixHeavy(int[][] matrix, int m, int n); static int maxSequenceSum(int[] array); static int fibonacciRecursive(int n); static int fibonacci(int n); static int findMaxUnContinuousSum(int[] array); static void findContinuousSequenceMethod1(int n); static void findContinuousSequenceMethod2(int n); static void findContinuousSequenceMethod3(int n); static void findContinuousSequenceMethod4(int n); static void pickNum(int[] originArray, int n, int m, int[] selectArray, int startIndex, boolean inorder); static int binarySearch(int[] array, int key); static int findFirstEqual(int[] array, int key); static int findFirstEqualLarger(int[] array, int key); static int findFirstLarger(int[] array, int key); static int findLastEqual(int[] array, int key); static int findLastEqualSmaller(int[] array, int key); static List<Integer> shufflePump(List<Integer> origin); static List<Integer> shuffleSwap(List<Integer> origin); static int[] shuffleSwap(int[] origin); static String getAccurateDivide(int a, int b, int n); static Set<Integer> getPrimeNumInefficient(int range); static Set<Integer> getPrimeNumEfficient(int range); static int getIntervalDays(int year, int month, int day); static int pageHelper(int n, int m); static boolean isOdd(int num);  }