Skip to content

Commit fefafac

Browse files
author
linzhijun
committed
使用 BigDecimal 代替 double
1 parent ea3b1f0 commit fefafac

File tree

16 files changed

+403
-420
lines changed

16 files changed

+403
-420
lines changed

java/toolgood.algorithm/README-EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ToolGood.Algorithm supports `Four arithmetic`, `Excel formula`, and supports `Cu
2020
<dependency>
2121
<groupId>io.github.toolgood</groupId>
2222
<artifactId>toolgood-algorithm</artifactId>
23-
<version>3.1.0.0</version>
23+
<version>3.0.3.0</version>
2424
</dependency>
2525
```
2626

java/toolgood.algorithm/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ToolGood.Algorithm支持`四则运算`、`Excel公式`,并支持`自定义参数
2020
<dependency>
2121
<groupId>io.github.toolgood</groupId>
2222
<artifactId>toolgood-algorithm</artifactId>
23-
<version>3.1.0.0</version>
23+
<version>3.0.3.0</version>
2424
</dependency>
2525
```
2626

java/toolgood.algorithm/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.toolgood</groupId>
88
<artifactId>toolgood-algorithm</artifactId>
9-
<version>3.1.0.0</version>
9+
<version>3.3.0.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>toolgood.algorithm</name>

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngine.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,13 @@ public double TryEvaluate(final String exp, final double defvalue) {
260260
LastError = obj.ErrorMsg();
261261
return defvalue;
262262
}
263-
return obj.NumberValue();
263+
return obj.NumberValue().doubleValue();
264264
}
265265
} catch (final Exception ex) {
266266
LastError = ex.getMessage();
267267
}
268268
return defvalue;
269269
}
270-
271270
public long TryEvaluate(final String exp, final long defvalue) {
272271
try {
273272
if (Parse(exp)) {

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngineEx.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public double TryEvaluateCategory(final String categoryName, final double defval
348348
LastError = obj.ErrorMsg();
349349
return defvalue;
350350
}
351-
return obj.NumberValue();
351+
return obj.NumberValue().doubleValue();
352352
} catch (final Exception ex) {
353353
LastError = ex.getMessage();
354354
}
@@ -362,7 +362,7 @@ public long TryEvaluateCategory(final String categoryName, final long defvalue)
362362
LastError = obj.ErrorMsg();
363363
return defvalue;
364364
}
365-
return obj.LongValue();
365+
return obj.NumberValue().longValue();
366366
} catch (final Exception ex) {
367367
LastError = ex.getMessage();
368368
}
@@ -455,7 +455,7 @@ public double TryEvaluate(final String exp, final double defvalue) {
455455
LastError = obj.ErrorMsg();
456456
return defvalue;
457457
}
458-
return obj.NumberValue();
458+
return obj.NumberValue().doubleValue();
459459
} catch (final Exception ex) {
460460
LastError = ex.getMessage();
461461
}
@@ -469,7 +469,7 @@ public long TryEvaluate(final String exp, final long defvalue) {
469469
LastError = obj.ErrorMsg();
470470
return defvalue;
471471
}
472-
return obj.LongValue();
472+
return obj.NumberValue().longValue();
473473
} catch (final Exception ex) {
474474
LastError = ex.getMessage();
475475
}

java/toolgood.algorithm/src/main/java/toolgood/algorithm/MyDate.java

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package toolgood.algorithm;
22

3+
import java.math.BigDecimal;
34
import java.time.LocalDate;
45
import java.time.temporal.ChronoUnit;
56
import java.util.Date;
@@ -10,6 +11,20 @@
1011
import org.joda.time.DateTimeZone;
1112

1213
public class MyDate {
14+
public Integer Year;
15+
public Integer Month;
16+
public Integer Day;
17+
public int Hour;
18+
public int Minute;
19+
public int Second;
20+
21+
// public MyDate(TimeSpan dt) {
22+
// Day = dt.Days;
23+
// Hour = dt.Hours;
24+
// Minute = dt.Minutes;
25+
// Second = dt.Seconds;
26+
// }
27+
1328
private MyDate() {
1429
}
1530

@@ -41,6 +56,40 @@ public MyDate(DateTime dTime) {
4156
Second = dTime.getSecondOfMinute();
4257
}
4358

59+
public MyDate(BigDecimal num) {
60+
int days = num.intValue();
61+
if (days > 365) {
62+
LocalDate start = LocalDate.of(1900, 1, 1);
63+
start = start.plusDays(days - 2);
64+
Year = start.getYear();
65+
Month = start.getMonthValue();
66+
Day = start.getDayOfMonth();
67+
} else {
68+
Day = days;
69+
}
70+
BigDecimal d = num.subtract(new BigDecimal(days));
71+
Hour = d.multiply(new BigDecimal(24)).intValue();
72+
Minute = d.multiply(new BigDecimal(24)).subtract(new BigDecimal(Hour)).multiply(new BigDecimal(60)).intValue();
73+
Second= d.multiply(new BigDecimal(24)).subtract(new BigDecimal(Hour)).multiply(new BigDecimal(60)).subtract(new BigDecimal(Minute)).multiply(new BigDecimal(60)).intValue();
74+
}
75+
76+
public MyDate(double num) {
77+
int days = (int) num;
78+
if (days > 365) {
79+
LocalDate start = LocalDate.of(1900, 1, 1);
80+
start = start.plusDays(days - 2);
81+
Year = start.getYear();
82+
Month = start.getMonthValue();
83+
Day = start.getDayOfMonth();
84+
} else {
85+
Day = days;
86+
}
87+
double d = num - days;
88+
Hour = (int) (d * 24);
89+
Minute = (int) ((d * 24 - Hour) * 60.0);
90+
Second = (int) (((d * 24 - Hour) * 60.0 - Minute) * 60.0);
91+
}
92+
4493
public static MyDate parse(String txt) {
4594
String t = txt.trim();
4695
Matcher m = Pattern
@@ -105,37 +154,6 @@ public static MyDate now() {
105154
return new MyDate(DateTime.now());
106155
}
107156

108-
// public MyDate(TimeSpan dt) {
109-
// Day = dt.Days;
110-
// Hour = dt.Hours;
111-
// Minute = dt.Minutes;
112-
// Second = dt.Seconds;
113-
// }
114-
115-
public MyDate(double num) {
116-
int days = (int) num;
117-
if (days > 365) {
118-
LocalDate start = LocalDate.of(1900, 1, 1);
119-
start = start.plusDays(days - 2);
120-
Year = start.getYear();
121-
Month = start.getMonthValue();
122-
Day = start.getDayOfMonth();
123-
} else {
124-
Day = days;
125-
}
126-
double d = num - days;
127-
Hour = (int) (d * 24);
128-
Minute = (int) ((d * 24 - Hour) * 60.0);
129-
Second = (int) (((d * 24 - Hour) * 60.0 - Minute) * 60.0);
130-
}
131-
132-
public Integer Year;
133-
public Integer Month;
134-
public Integer Day;
135-
public int Hour;
136-
public int Minute;
137-
public int Second;
138-
139157
@Override
140158
public String toString() {
141159
StringBuffer stringBuffer = new StringBuffer();
@@ -329,4 +347,23 @@ public MyDate DIV(Double num) {
329347
return new MyDate(this.ToNumber() / num);
330348
}
331349

350+
351+
public MyDate ADD(BigDecimal num) {
352+
return new MyDate(this.ToNumber() + num.doubleValue());
353+
}
354+
355+
356+
public MyDate SUB(BigDecimal num) {
357+
return new MyDate(this.ToNumber() - num.doubleValue());
358+
}
359+
360+
public MyDate MUL(BigDecimal num) {
361+
return new MyDate(this.ToNumber() * num.doubleValue());
362+
}
363+
364+
public MyDate DIV(BigDecimal num) {
365+
return new MyDate(this.ToNumber() / num.doubleValue());
366+
}
367+
368+
332369
}

0 commit comments

Comments
 (0)