Skip to content

Commit e3728c1

Browse files
committed
BigDecimal小数点截取Filter
1 parent b953006 commit e3728c1

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.http.converter.filter;
19+
20+
import com.alibaba.fastjson.serializer.ValueFilter;
21+
import org.minbox.framework.api.boot.plugin.http.converter.filter.annotation.ApiBootDecimalAccuracy;
22+
import org.springframework.util.ReflectionUtils;
23+
24+
import java.lang.reflect.Field;
25+
import java.math.BigDecimal;
26+
27+
/**
28+
* ApiBoot Decimal Accuracy Value Filter
29+
*
30+
* @author:恒宇少年 - 于起宇
31+
* <p>
32+
* DateTime:2019-04-16 13:17
33+
* Blog:http://blog.yuqiyu.com
34+
* WebSite:http://www.jianshu.com/u/092df3f77bca
35+
* Gitee:https://gitee.com/hengboy
36+
* GitHub:https://github.com/hengboy
37+
*/
38+
public class DecimalAccuracyFilter implements ValueFilter {
39+
40+
@Override
41+
public Object process(Object object, String name, Object value) {
42+
try {
43+
// find field
44+
Field field = ReflectionUtils.findField(object.getClass(), name);
45+
// Have ApiBootDecimalAccuracy Annotation
46+
// Value is BigDecimal Instance
47+
if (field.isAnnotationPresent(ApiBootDecimalAccuracy.class) && value instanceof BigDecimal) {
48+
ApiBootDecimalAccuracy decimalAccuracy = field.getDeclaredAnnotation(ApiBootDecimalAccuracy.class);
49+
BigDecimal decimalValue = (BigDecimal) value;
50+
return decimalValue.setScale(decimalAccuracy.scale(), decimalAccuracy.roundingMode());
51+
}
52+
} catch (Exception e) {
53+
//ignore
54+
return value;
55+
}
56+
return value;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.http.converter.filter.annotation;
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
import java.math.BigDecimal;
25+
26+
/**
27+
* ApiBoot Decimal Accuracy
28+
*
29+
* @author:恒宇少年 - 于起宇
30+
* <p>
31+
* DateTime:2019-04-16 13:18
32+
* Blog:http://blog.yuqiyu.com
33+
* WebSite:http://www.jianshu.com/u/092df3f77bca
34+
* Gitee:https://gitee.com/hengboy
35+
* GitHub:https://github.com/hengboy
36+
*/
37+
@Target({ElementType.FIELD})
38+
@Retention(RetentionPolicy.RUNTIME)
39+
public @interface ApiBootDecimalAccuracy {
40+
/**
41+
* Keep two decimal points
42+
*
43+
* @return number
44+
*/
45+
int scale() default 2;
46+
47+
/**
48+
* BigDecimal round mode
49+
*
50+
* @return mode
51+
*/
52+
int roundingMode() default BigDecimal.ROUND_DOWN;
53+
}

0 commit comments

Comments
 (0)