Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 54.2 / 2 = 27.1 (exact split) returns true
- 61.05 / 2 = 30.525 (not exact split) returns false
- (54.20 * 100) % 2 = 0
- (61.05 * 100) % 2 = 1
- if (((number*100) % 1) == 0) {
- // exact
- } else {
- // not exact
- }
- String decimals = Float.toString(f);
- String[] tokens = decimals.split("[.]");
- return tokens[1].length() == 2;
- public static boolean isExactSplit (BigDecimal number, int nbParts) {
- try {
- number.divide(new BigDecimal(nbParts), 2, RoundingMode.UNNECESSARY);
- return true;
- } catch (ArithmeticException roundingError) {
- return false;
- }
- }
- System.out.println(isExactSplit(new BigDecimal("54.2"), 2)); // prints true
- System.out.println(isExactSplit(new BigDecimal("61.05"), 2)); // prints false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement