Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. 54.2 / 2 = 27.1 (exact split) returns true
  2. 61.05 / 2 = 30.525 (not exact split) returns false
  3.  
  4. (54.20 * 100) % 2 = 0
  5. (61.05 * 100) % 2 = 1
  6.  
  7. if (((number*100) % 1) == 0) {
  8. // exact
  9. } else {
  10. // not exact
  11. }
  12.  
  13. String decimals = Float.toString(f);
  14. String[] tokens = decimals.split("[.]");
  15. return tokens[1].length() == 2;
  16.  
  17. public static boolean isExactSplit (BigDecimal number, int nbParts) {
  18. try {
  19. number.divide(new BigDecimal(nbParts), 2, RoundingMode.UNNECESSARY);
  20. return true;
  21. } catch (ArithmeticException roundingError) {
  22. return false;
  23. }
  24. }
  25.  
  26. System.out.println(isExactSplit(new BigDecimal("54.2"), 2)); // prints true
  27. System.out.println(isExactSplit(new BigDecimal("61.05"), 2)); // prints false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement