三方活动:计算中采用5位小数(四舍五入),结果保留4位小数(截断)
This commit is contained in:
parent
b02f0bfdf3
commit
95d1293abb
@ -20,20 +20,26 @@ public class Calc {
|
|||||||
public static void main(String[] argv) {
|
public static void main(String[] argv) {
|
||||||
|
|
||||||
Map<String, Double> map = new HashMap<>();
|
Map<String, Double> map = new HashMap<>();
|
||||||
map.put("T", Double.valueOf(0.7));
|
map.put("T", Double.valueOf(0.1234));
|
||||||
String ruleExpression = "T + 10%";
|
String ruleExpression = "T * 30%";
|
||||||
double rs = eval(ruleExpression, map);
|
double rs = eval(ruleExpression, map);
|
||||||
System.out.println(rs);
|
System.out.println(rs);
|
||||||
System.out.println(0.7 + 0.1);
|
System.out.println(0.7 + 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//计算中采用5位小数(四舍五入),结果保留4位小数(截断)
|
||||||
public static double eval(String ruleExpression, Map<String, Double> params) {
|
public static double eval(String ruleExpression, Map<String, Double> params) {
|
||||||
|
|
||||||
|
return eval(ruleExpression, params, RoundingMode.FLOOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double eval(String ruleExpression, Map<String, Double> params, RoundingMode rm) {
|
||||||
|
|
||||||
String[] split = ruleExpression.split("[()*/\\-+?]");
|
String[] split = ruleExpression.split("[()*/\\-+?]");
|
||||||
for (String splitValue : split) {
|
for (String splitValue : split) {
|
||||||
if (splitValue.endsWith("%")) {
|
if (splitValue.endsWith("%")) {
|
||||||
BigDecimal rate = new BigDecimal(splitValue.trim().replace("%", ""));
|
BigDecimal rate = new BigDecimal(splitValue.trim().replace("%", ""));
|
||||||
ruleExpression = ruleExpression.replace(splitValue, rate.divide(BigDecimal.valueOf(100)).setScale(2,
|
ruleExpression = ruleExpression.replace(splitValue, rate.divide(BigDecimal.valueOf(100)).setScale(5,
|
||||||
RoundingMode.HALF_UP).toString());
|
RoundingMode.HALF_UP).toString());
|
||||||
} else {
|
} else {
|
||||||
if (pattern.matcher(splitValue.trim()).matches()) {
|
if (pattern.matcher(splitValue.trim()).matches()) {
|
||||||
@ -42,8 +48,9 @@ public class Calc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expression expression = new Expression(ruleExpression);
|
Expression expression = new Expression(ruleExpression);
|
||||||
expression.setPrecision(3);
|
expression.setPrecision(5);
|
||||||
return expression.eval().doubleValue();
|
expression.setRoundingMode(RoundingMode.HALF_UP);
|
||||||
|
return expression.eval().setScale(4, rm).doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void test(String s, double x) {
|
private static void test(String s, double x) {
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
|
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
|
||||||
@ -34,8 +35,17 @@ public class OrderDataLogic implements ServiceLogic {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PileOrderService pileOrderService;
|
private PileOrderService pileOrderService;
|
||||||
@Autowired
|
private static Map<String, String> SM = new HashMap<>();
|
||||||
private ChargingController chargingController;
|
|
||||||
|
static {
|
||||||
|
SM.put("40", "APP 远程停止");
|
||||||
|
SM.put("41", "SOC 达到 100%");
|
||||||
|
SM.put("42", "充电电量满足设定条件");
|
||||||
|
SM.put("43", "充电金额满足设定条件");
|
||||||
|
SM.put("44", "充电时间满足设定条件");
|
||||||
|
SM.put("45", "手动停止充电");
|
||||||
|
SM.put("72", "急停按钮停止充电(0X72)");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServiceResult service(ServiceParameter sp) throws Exception {
|
public ServiceResult service(ServiceParameter sp) throws Exception {
|
||||||
@ -54,6 +64,7 @@ public class OrderDataLogic implements ServiceLogic {
|
|||||||
cacheOrderData.setCreateTime(DateUtil.now());
|
cacheOrderData.setCreateTime(DateUtil.now());
|
||||||
cacheOrder.put("orderData", cacheOrderData);
|
cacheOrder.put("orderData", cacheOrderData);
|
||||||
cacheOrder.put("status", "已结束");
|
cacheOrder.put("status", "已结束");
|
||||||
|
cacheOrder.put("stopReason", translate(cacheOrderData.getStopReason()));
|
||||||
REDIS.setCacheMap(orderkey, cacheOrder);
|
REDIS.setCacheMap(orderkey, cacheOrder);
|
||||||
String gunkey = "gun:".concat(orderData.getPileNo()).concat(orderData.getGunId());
|
String gunkey = "gun:".concat(orderData.getPileNo()).concat(orderData.getGunId());
|
||||||
Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
|
Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
|
||||||
@ -89,7 +100,29 @@ public class OrderDataLogic implements ServiceLogic {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private CacheOrderData translate(OrderData orderData) throws InvocationTargetException, IllegalAccessException, InstantiationException {
|
private String translate(String stopReason) {
|
||||||
|
|
||||||
|
String sr = SM.get(stopReason);
|
||||||
|
if (sr == null) {
|
||||||
|
final Integer stopReasonInt = Integer.valueOf(stopReason, 16);
|
||||||
|
if (stopReasonInt <= 0x49) {
|
||||||
|
sr = "充电完成(0X".concat(stopReason).concat(")");
|
||||||
|
} else if (stopReasonInt <= 0x69) {
|
||||||
|
sr = "充电启动失败(0X".concat(stopReason).concat(")");
|
||||||
|
} else if (stopReasonInt < 0x90) {
|
||||||
|
sr = "充电异常中止(0X".concat(stopReason).concat(")");
|
||||||
|
} else if (stopReasonInt == 0x90) {
|
||||||
|
sr = "未知原因中止(0X".concat(stopReason).concat(")");
|
||||||
|
} else {
|
||||||
|
sr = "未知原因中止(UC_0X".concat(stopReason).concat(")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sr;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
private CacheOrderData translate(OrderData orderData) throws InvocationTargetException, IllegalAccessException,
|
||||||
|
InstantiationException {
|
||||||
|
|
||||||
Class<CacheOrderData> codclz = CacheOrderData.class;
|
Class<CacheOrderData> codclz = CacheOrderData.class;
|
||||||
Class<OrderData> odclz = OrderData.class;
|
Class<OrderData> odclz = OrderData.class;
|
||||||
|
|||||||
@ -23,7 +23,7 @@ public class CacheDataUtils {
|
|||||||
Object tarobj = null;
|
Object tarobj = null;
|
||||||
for (Constructor c : constructors) {
|
for (Constructor c : constructors) {
|
||||||
if (c.getParameters().length == 0) {
|
if (c.getParameters().length == 0) {
|
||||||
tarobj = constructors[0].newInstance();
|
tarobj = c.newInstance();
|
||||||
for (Field tarfield : targetfields) {
|
for (Field tarfield : targetfields) {
|
||||||
String tarFieldName = tarfield.getName();
|
String tarFieldName = tarfield.getName();
|
||||||
String srcval = null;
|
String srcval = null;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user