diff --git a/README.md b/README.md
index e9f046f..97bf325 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,98 @@
How It Work
------------------------
+
+## Background
+
+
+### Difficult to Comprehension
+```txt
+
+BigDecimal big10 = new BigDecimal(10);
+BigDecimal big20 = new BigDecimal(20);
+
+System.out.println(big10.compareTo(big20) < -1); // false
+System.out.println(big10.compareTo(big20) <= -1); // true
+System.out.println(big10.compareTo(big20) == -1); // true
+System.out.println(big10.compareTo(big20) >= -1); // true
+System.out.println(big10.compareTo(big20) > -1); // false
+System.out.println(big10.compareTo(big20) != -1); // false
+
+System.out.println(big10.compareTo(big20) < 0); // true
+System.out.println(big10.compareTo(big20) <= 0); // true
+System.out.println(big10.compareTo(big20) == 0); // false
+System.out.println(big10.compareTo(big20) >= 0); // false
+System.out.println(big10.compareTo(big20) > 0); // false
+System.out.println(big10.compareTo(big20) != 0); // true
+
+System.out.println(big10.compareTo(big20) < 1); // true
+System.out.println(big10.compareTo(big20) <= 1); // true
+System.out.println(big10.compareTo(big20) == 1); // false
+System.out.println(big10.compareTo(big20) >= 1); // false
+System.out.println(big10.compareTo(big20) > 1); // false
+System.out.println(big10.compareTo(big20) != 1); // true
+
+```
+
+### Error-prone
+
+
+![傻傻分不清楚](./src/test/java/com/sunchp/utils/math/Test.java)
+
+```
+ double d = 0.1D;
+ float f = 0.1F;
+
+ Double aDouble = d;
+ Float aFloat = f;
+
+ BigDecimal correctedDoubleBigDecimal = new BigDecimal(aDouble.toString());
+ BigDecimal dbigDecimal1 = new BigDecimal(d);
+ BigDecimal dbigDecimal2 = new BigDecimal(aDouble);
+ BigDecimal dbigDecimal3 = BigDecimal.valueOf(d);
+ BigDecimal dbigDecimal4 = BigDecimal.valueOf(aDouble);
+ BigDecimal correctedFloatBigDecimal = new BigDecimal(aFloat.toString());
+ BigDecimal fbigDecimal1 = new BigDecimal(f);
+ BigDecimal fbigDecimal2 = new BigDecimal(aFloat);
+ BigDecimal fbigDecimal3 = BigDecimal.valueOf(f);
+ BigDecimal fbigDecimal4 = BigDecimal.valueOf(aFloat);
+
+ Double d01 = (double) f;
+
+ //0.10000000149011612
+ System.out.println(d01);
+
+ //0.1
+ System.out.println(correctedDoubleBigDecimal);
+ //0.1000000000000000055511151231257827021181583404541015625
+ System.out.println(dbigDecimal1);
+ //0.1000000000000000055511151231257827021181583404541015625
+ System.out.println(dbigDecimal2);
+ //0.1
+ System.out.println(dbigDecimal3);
+ //0.1
+ System.out.println(dbigDecimal4);
+ //0.1
+ System.out.println(correctedFloatBigDecimal);
+ //0.100000001490116119384765625
+ System.out.println(fbigDecimal1);
+ //0.100000001490116119384765625
+ System.out.println(fbigDecimal2);
+ //0.10000000149011612
+ System.out.println(fbigDecimal3);
+ //0.10000000149011612
+ System.out.println(fbigDecimal4);
+
+
+```
+
+
- Import library to your code:
-```java
+```
import static com.sunchp.utils.math.BigDecimalUtils.*;
```
- Doing comparison:
-```java
+```
if(is(income).lt(amount)) {
// ....
}else {
@@ -15,13 +101,13 @@ How It Work
```
- Do calculation:
-```java
+```
remain = cal(income).minus(amount).result();
```
Other methods currently in this library
------------------------
-```java
+```
is(income).eq(amount); // Equal
is(income).gt(amount); // Greater than
is(income).gteq(amount); // Greater than equal
@@ -38,7 +124,7 @@ Other methods currently in this library
Currently comparison support only String and BigDecimal:
-```java
+```
is(bigdecimal).eq(bigdecimal); // BigDecimal and BigDecimal
is(bigdecimal).eq("1000"); // BigDecimal and String
is("1000").lt(bigdecimal); // String and BigDecimal
@@ -46,9 +132,23 @@ Currently comparison support only String and BigDecimal:
```
Calculation support only String and BigDecimal:
-```java
+```
cal(bigdecimal).minus(bigdecimal) // BigDecimal and BigDecimal
cal(bigdecimal).minus("500") // BigDecimal and String
cal("500").minus(bigdecimal) // String and BigDecimal
cal("1000").minus("500") // String and String
```
+
+## 增加功能
+
+* 支持 所有算数类型的对比
+* 如果传入String不是一个能转换为数字的 字符串 得到的结果将是0
+* 支援带分隔符的数字转换 例如 +1,000,000 -1,000,000
+* 去掉了Ascii码中所有符号 除了- 和 . 符号
+
+## add feature
+
+* Support for comparison of all arithmetic types
+* If the passed String is not a string that can be converted to a number, the result will be 0
+* Support for delimited numeric conversions such as +1,000,000 -1,000,000
+* Removed all symbols from the Ascii code except the - and . symbol `\u002d\u002e`
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 86dfd42..a86a5a8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,6 +14,14 @@
junit
4.12
+
+
+
+ org.apache.commons
+ commons-lang3
+ 3.8.1
+
+
\ No newline at end of file
diff --git a/src/main/java/com/sunchp/utils/math/AsciiSymbolContants.java b/src/main/java/com/sunchp/utils/math/AsciiSymbolContants.java
new file mode 100644
index 0000000..1bdb3c3
--- /dev/null
+++ b/src/main/java/com/sunchp/utils/math/AsciiSymbolContants.java
@@ -0,0 +1,15 @@
+package com.sunchp.utils.math;
+
+public class AsciiSymbolContants {
+ public static final String DISPLAY_SYMBOL = "\u0020\u0021"
+ + "\""
+ + "\u0023\u0024\u0025\u0026\u0027\u0028\u0029\u002a\u002b\u002c\u002d\u002e\u002f"
+ + "\u003a\u003b\u003c\u003d\u003e\u003f\u0040\u005b"
+ + "\\"
+ + "\u005d\u005e\u005f\u0060\u007b\u007c\u007d\u007e";
+
+ public static final String NOT_DISPLAY_SYMBOL =
+ "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009"
+ + "\n" + "\u000b\u000c"
+ + "\r" + "\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f";
+}
diff --git a/src/main/java/com/sunchp/utils/math/BigDecimalCalculation.java b/src/main/java/com/sunchp/utils/math/BigDecimalCalculation.java
index f71ddcc..2e40eb0 100644
--- a/src/main/java/com/sunchp/utils/math/BigDecimalCalculation.java
+++ b/src/main/java/com/sunchp/utils/math/BigDecimalCalculation.java
@@ -1,8 +1,13 @@
package com.sunchp.utils.math;
+import static com.sunchp.utils.math.BigDecimalUtils.stringSafeGetBigDecimal;
+
import java.math.BigDecimal;
import java.math.RoundingMode;
+/**
+ * @author albert
+ */
public class BigDecimalCalculation {
private final BigDecimal amount;
@@ -11,7 +16,11 @@ public BigDecimalCalculation(BigDecimal amount) {
}
public BigDecimalCalculation(String amount) {
- this.amount = new BigDecimal(amount);
+ this.amount = stringSafeGetBigDecimal(amount);
+ }
+
+ public BigDecimalCalculation(Number amount) {
+ this.amount = stringSafeGetBigDecimal(amount.toString());
}
public BigDecimalCalculation plus(BigDecimal value) {
@@ -19,7 +28,11 @@ public BigDecimalCalculation plus(BigDecimal value) {
}
public BigDecimalCalculation plus(String value) {
- return plus(new BigDecimal(value));
+ return plus(stringSafeGetBigDecimal(value));
+ }
+
+ public BigDecimalCalculation plus(Number value) {
+ return plus(stringSafeGetBigDecimal(value.toString()));
}
public BigDecimalCalculation minus(BigDecimal value) {
@@ -27,7 +40,11 @@ public BigDecimalCalculation minus(BigDecimal value) {
}
public BigDecimalCalculation minus(String value) {
- return minus(new BigDecimal(value));
+ return minus(stringSafeGetBigDecimal(value));
+ }
+
+ public BigDecimalCalculation minus(Number value) {
+ return minus(stringSafeGetBigDecimal(value.toString()));
}
public BigDecimalCalculation mul(BigDecimal value) {
@@ -35,7 +52,11 @@ public BigDecimalCalculation mul(BigDecimal value) {
}
public BigDecimalCalculation mul(String value) {
- return mul(new BigDecimal(value));
+ return mul(stringSafeGetBigDecimal(value));
+ }
+
+ public BigDecimalCalculation mul(Number value) {
+ return mul(stringSafeGetBigDecimal(value.toString()));
}
public BigDecimalCalculation div(BigDecimal value) {
@@ -43,7 +64,11 @@ public BigDecimalCalculation div(BigDecimal value) {
}
public BigDecimalCalculation div(String value) {
- return div(new BigDecimal(value));
+ return div(stringSafeGetBigDecimal(value));
+ }
+
+ public BigDecimalCalculation div(Number value) {
+ return div(stringSafeGetBigDecimal(value.toString()));
}
public BigDecimalCalculation div(BigDecimal value, int scale) {
@@ -51,7 +76,11 @@ public BigDecimalCalculation div(BigDecimal value, int scale) {
}
public BigDecimalCalculation div(String value, int scale) {
- return div(new BigDecimal(value), scale);
+ return div(stringSafeGetBigDecimal(value), scale);
+ }
+
+ public BigDecimalCalculation div(Number value, int scale) {
+ return div(stringSafeGetBigDecimal(value.toString()), scale);
}
public BigDecimal result() {
diff --git a/src/main/java/com/sunchp/utils/math/BigDecimalLogic.java b/src/main/java/com/sunchp/utils/math/BigDecimalLogic.java
index 01f6bbe..e4f3a43 100644
--- a/src/main/java/com/sunchp/utils/math/BigDecimalLogic.java
+++ b/src/main/java/com/sunchp/utils/math/BigDecimalLogic.java
@@ -1,12 +1,17 @@
package com.sunchp.utils.math;
+import static com.sunchp.utils.math.BigDecimalUtils.stringSafeGetBigDecimal;
+
import java.math.BigDecimal;
+/**
+ * @author albert
+ */
public class BigDecimalLogic {
- private static final int ZERO = 0;
-
private final BigDecimal amount;
+ private static final int ZERO = 0;
+
public BigDecimalLogic(BigDecimal amount) {
this.amount = amount;
}
@@ -15,7 +20,7 @@ public BigDecimalLogic(String amount) {
if (amount == null) {
this.amount = null;
} else {
- this.amount = new BigDecimal(amount);
+ this.amount = stringSafeGetBigDecimal(amount);
}
}
@@ -24,7 +29,12 @@ public boolean eq(BigDecimal bigDecimal) {
}
public boolean eq(String value) {
- return eq(new BigDecimal(value));
+ return eq(stringSafeGetBigDecimal(value));
+ }
+
+
+ public boolean eq(Number value) {
+ return eq(stringSafeGetBigDecimal(value.toString()));
}
public boolean gt(BigDecimal bigDecimal) {
@@ -32,7 +42,11 @@ public boolean gt(BigDecimal bigDecimal) {
}
public boolean gt(String value) {
- return gt(new BigDecimal(value));
+ return gt(stringSafeGetBigDecimal(value));
+ }
+
+ public boolean gt(Number value) {
+ return gt(stringSafeGetBigDecimal(value.toString()));
}
public boolean gteq(BigDecimal bigDecimal) {
@@ -40,7 +54,11 @@ public boolean gteq(BigDecimal bigDecimal) {
}
public boolean gteq(String value) {
- return gteq(new BigDecimal(value));
+ return gteq(stringSafeGetBigDecimal(value));
+ }
+
+ public boolean gteq(Number value) {
+ return gteq(stringSafeGetBigDecimal(value.toString()));
}
public boolean lt(BigDecimal bigDecimal) {
@@ -48,7 +66,11 @@ public boolean lt(BigDecimal bigDecimal) {
}
public boolean lt(String value) {
- return lt(new BigDecimal(value));
+ return lt(stringSafeGetBigDecimal(value));
+ }
+
+ public boolean lt(Number value) {
+ return lt(stringSafeGetBigDecimal(value.toString()));
}
public boolean lteq(BigDecimal bigDecimal) {
@@ -56,7 +78,11 @@ public boolean lteq(BigDecimal bigDecimal) {
}
public boolean lteq(String value) {
- return lteq(new BigDecimal(value));
+ return lteq(stringSafeGetBigDecimal(value));
+ }
+
+ public boolean lteq(Number value) {
+ return lteq(stringSafeGetBigDecimal(value.toString()));
}
public boolean isZero() {
diff --git a/src/main/java/com/sunchp/utils/math/BigDecimalUtils.java b/src/main/java/com/sunchp/utils/math/BigDecimalUtils.java
index 56a2d07..fab5010 100644
--- a/src/main/java/com/sunchp/utils/math/BigDecimalUtils.java
+++ b/src/main/java/com/sunchp/utils/math/BigDecimalUtils.java
@@ -1,25 +1,72 @@
package com.sunchp.utils.math;
+import org.apache.commons.lang3.RegExUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+
import java.math.BigDecimal;
+/**
+ * @author albert
+ */
public class BigDecimalUtils {
+
private BigDecimalUtils() {
}
- public static BigDecimalLogic is(BigDecimal value) {
+ public static BigDecimalLogic is(String value) {
return new BigDecimalLogic(value);
}
- public static BigDecimalLogic is(String value) {
- return new BigDecimalLogic(value);
+ public static BigDecimalLogic is(Number value) {
+ return new BigDecimalLogic(value.toString());
}
- public static BigDecimalCalculation cal(BigDecimal value) {
+ public static BigDecimalCalculation cal(String value) {
return new BigDecimalCalculation(value);
}
- public static BigDecimalCalculation cal(String value) {
- return new BigDecimalCalculation(new BigDecimal(value));
+ public static BigDecimalCalculation cal(Number value) {
+ return new BigDecimalCalculation(value.toString());
+ }
+
+
+ public static BigDecimal getMinimum(BigDecimal v1, BigDecimal v2) {
+ if (v1 == null) {
+ return v2;
+ }
+ if (v2 == null) {
+ return v1;
+ }
+ return v1.compareTo(v2) < 0 ? v1 : v2;
+ }
+
+ public static BigDecimal getMaximum(BigDecimal v1, BigDecimal v2) {
+ if (v1 == null) {
+ return v2;
+ }
+ if (v2 == null) {
+ return v1;
+ }
+ return v1.compareTo(v2) > 0 ? v1 : v2;
+ }
+
+ public static BigDecimal stringSafeGetBigDecimal(String stringNumber) {
+ stringNumber = replaceAllNotDigitalCharsExceptDotAndMinusSign(stringNumber);
+ if (NumberUtils.isCreatable(stringNumber)) {
+ return new BigDecimal(stringNumber);
+ }
+ throw new NumberFormatException("Argument "+stringNumber + " is not a digital");
}
+
+ private static String replaceAllNotDigitalCharsExceptDotAndMinusSign(String stringNumber) {
+ String symbol = AsciiSymbolContants.DISPLAY_SYMBOL + AsciiSymbolContants.NOT_DISPLAY_SYMBOL;
+ symbol = RegExUtils.replaceAll(symbol, "\\-", "");
+ symbol = RegExUtils.replaceAll(symbol, "\\.", "");
+ stringNumber = StringUtils.replaceChars(stringNumber, symbol, "");
+ return stringNumber;
+ }
+
+
}
diff --git a/src/test/java/com/sunchp/utils/math/BigDecimalLogicTest.java b/src/test/java/com/sunchp/utils/math/BigDecimalLogicTest.java
index f5b6510..f0edbf0 100644
--- a/src/test/java/com/sunchp/utils/math/BigDecimalLogicTest.java
+++ b/src/test/java/com/sunchp/utils/math/BigDecimalLogicTest.java
@@ -9,19 +9,30 @@
import static org.junit.Assert.assertTrue;
public class BigDecimalLogicTest {
- BigDecimal one = new BigDecimal("1.4201");
+ BigDecimal one = new BigDecimal("0.1");
BigDecimal two = new BigDecimal("3.521");
BigDecimal three = new BigDecimal("6.521");
BigDecimal four = new BigDecimal("6.521");
BigDecimal five = null;
+ double aDouble = 6.521;
+ float aFloat = 6.521F;
+
+
@Test
public void testTwoBigdecimalsEquality() {
+
+
assertTrue(is(three).eq(four));
assertTrue(is(one).eq(one));
assertTrue(!is(one).eq(two));
assertTrue(is(three).eq("6.521"));
+ assertTrue(is(three).eq(aDouble));
+ assertTrue(is(three).eq(aFloat));
assertTrue(is("1").eq("1"));
+ assertTrue(is("0.1").eq("0.1"));
+ assertTrue(is(0.1D).eq(0.1F));
+ assertTrue(is((float)0.1D).eq(0.1F));
}
@Test
diff --git a/src/test/java/com/sunchp/utils/math/CauseTest.java b/src/test/java/com/sunchp/utils/math/CauseTest.java
new file mode 100644
index 0000000..3765573
--- /dev/null
+++ b/src/test/java/com/sunchp/utils/math/CauseTest.java
@@ -0,0 +1,67 @@
+package com.sunchp.utils.math;
+
+import java.math.BigDecimal;
+
+public class CauseTest {
+
+ public static void main(String[] args) {
+
+ System.out.println(AsciiSymbolContants.DISPLAY_SYMBOL);
+ System.out.println(AsciiSymbolContants.NOT_DISPLAY_SYMBOL);
+
+ double d = 0.1D;
+ float f = 0.1F;
+
+ Double aDouble = d;
+ Float aFloat = f;
+
+ BigDecimal correctedDoubleBigDecimal = new BigDecimal(aDouble.toString());
+ BigDecimal dBigDecimal1 = new BigDecimal(d);
+ BigDecimal dBigDecimal2 = new BigDecimal(aDouble);
+ BigDecimal dBigDecimal3 = BigDecimal.valueOf(d);
+ BigDecimal dBigDecimal4 = BigDecimal.valueOf(aDouble);
+ BigDecimal correctedFloatBigDecimal = new BigDecimal(aFloat.toString());
+ BigDecimal fBigDecimal1 = new BigDecimal(f);
+ BigDecimal fBigDecimal2 = new BigDecimal(aFloat);
+ BigDecimal fBigDecimal3 = BigDecimal.valueOf(f);
+ BigDecimal fBigDecimal4 = BigDecimal.valueOf(aFloat);
+
+ Double d01 = (double) f;
+
+ System.out.println(d01);
+
+
+ System.out.println("----");
+ System.out.println("new BigDecimal(aDouble.toString()) :\n");
+ System.out.println(correctedDoubleBigDecimal);
+ System.out.println("----");
+ System.out.println("new BigDecimal(d) : \n");
+ System.out.println(dBigDecimal1);
+ System.out.println("----");
+ System.out.println("new BigDecimal(aDouble) :");
+ System.out.println(dBigDecimal2);
+ System.out.println("----");
+ System.out.println("BigDecimal.valueOf(d) : ");
+ System.out.println(dBigDecimal3);
+ System.out.println("----");
+ System.out.println("BigDecimal.valueOf(aDouble) :");
+ System.out.println(dBigDecimal4);
+ System.out.println("----");
+ System.out.println("new BigDecimal(aFloat.toString()) : " + (fBigDecimal2.compareTo(correctedDoubleBigDecimal) != 0));
+ System.out.println(correctedFloatBigDecimal);
+ System.out.println("----");
+ System.out.println("new BigDecimal(f) : " + (fBigDecimal2.compareTo(new BigDecimal("0.1")) < 1));
+ System.out.println(fBigDecimal1);
+ System.out.println("----");
+ System.out.println("new BigDecimal(aFloat) : " + ((fBigDecimal2.compareTo(new BigDecimal("0.1")) > -1) && (fBigDecimal2.compareTo(new BigDecimal("0.1")) < 1)));
+ System.out.println(fBigDecimal2);
+ System.out.println("----");
+ System.out.println("BigDecimal.valueOf(f) : " + (fBigDecimal3.compareTo(new BigDecimal("0.1")) == 0));
+ System.out.println(fBigDecimal3);
+ System.out.println("----");
+ System.out.println("BigDecimal.valueOf(aFloat) : " + (fBigDecimal4.compareTo(BigDecimal.valueOf(0.1D)) == 0));
+ System.out.println(fBigDecimal4);
+
+
+ }
+}