From 2e91f8abdcb87feda9e237895d4460a19fd7aed8 Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期五, 10 一月 2025 15:41:23 +0800 Subject: [PATCH] 2025年01月10日15:40:19 --- app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java | 143 +++++++++++++++++++++++++++++------------------ 1 files changed, 88 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java index 4124f7a..5a4fd71 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java @@ -74,71 +74,46 @@ return sHdlCommonLogic; } - public static String getConvertDoubleUnit(String value) { - if (TextUtils.isEmpty(value)) { - return UnitType.noValue; - } - BigDecimal formattedValue = getBigDecimal(value); - return formattedValue.toString(); - - } - - public static String getConvertDoubleUnit(int value) { - if (value == 0) { - return UnitType.noValue; - } - BigDecimal formattedValue = getBigDecimal(value + ""); - return formattedValue.toString(); - } - /** * @param value 鍊� - * @param unit 渚嬪:UnitType.kWh - * @return 甯﹀崟浣嶅�艰繑鍥� + * @return 瀛楃涓插�艰繑鍥� */ - public static String getConvertDoubleUnit(String value, String unit) { + public static String convertDoubleValue(String value) { if (TextUtils.isEmpty(value)) { - return UnitType.noValue + unit; + return UnitType.noValue; } - if (unit.equals(UnitType.kW)) { - return divideByOneThousandAndFormat(value).toString() + unit; + return divideByOneThousandAndFormat(value).toString(); + } + /** + * @param value 鍊� + * @param diploidValue 鍊嶆暟 + * @return 瀛楃涓� + */ + public static String convertDoubleValue(String value, int diploidValue) { + try { + double doubleValue = Double.parseDouble(value); + BigDecimal bigDecimal = new BigDecimal(doubleValue); + return bigDecimal.divide(new BigDecimal(diploidValue), 2, RoundingMode.HALF_EVEN).toString(); + } catch (Exception e) { + return "0.00"; } - return getBigDecimal(value).toString() + unit; - } /** - * @param value 鍊� - * @param unitValue 渚嬪:UnitType.kWh - * @param isUnit true琛ㄧず鏈夊崟浣嶈繑鍥� - * @return 甯﹀崟浣嶅�艰繑鍥� + * @param value 鍊� + * @param diploidValue 鍊嶆暟 + * @return 瀛楃涓� */ - public static String getConvertDoubleUnit(String value, String unitValue, boolean isUnit) { - if (TextUtils.isEmpty(value)) { - return UnitType.noValue + (isUnit ? unitValue : ""); + public static BigDecimal convertBigDecimal(String value, int diploidValue) { + try { + double doubleValue = Double.parseDouble(value); + BigDecimal bigDecimal = new BigDecimal(doubleValue); + return bigDecimal.divide(new BigDecimal(diploidValue), 2, RoundingMode.HALF_EVEN); + } catch (Exception e) { + return new BigDecimal(0); } - if (unitValue.equals(UnitType.kW)) { - return divideByOneThousandAndFormat(value).toString() + (isUnit ? unitValue : ""); - } - return getBigDecimal(value).toString() + (isUnit ? unitValue : ""); - } - - - /** - * @param value 鍊� - * @param unit 渚嬪:UnitType.kWh - * @return 甯﹀崟浣嶅�艰繑鍥� - */ - public static String getConvertDoubleUnit(int value, String unit) { - if (value == 0) { - return UnitType.noValue + unit; - } - BigDecimal formattedValue = getBigDecimal(value + ""); - return formattedValue.toString() + unit; - } - public static String convertString(Object value) { return String.valueOf(value); @@ -153,6 +128,8 @@ return BigDecimal.valueOf(doubleValue).setScale(2, RoundingMode.HALF_UP); } + + /** * 闄や互涓�鍗冨拰鏍煎紡 * @@ -160,11 +137,67 @@ * @return BigDecimal */ public static BigDecimal divideByOneThousandAndFormat(String value) { - double doubleValue = Double.parseDouble(value); - BigDecimal bigDecimal = new BigDecimal(doubleValue); - return bigDecimal.divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN); + try { + double doubleValue = Double.parseDouble(value); + int mDoubleValue = doubleValue(value); + if (mDoubleValue == 0) { + return getBigDecimal(value); + } else { + BigDecimal bigDecimal = new BigDecimal(doubleValue); + return bigDecimal.divide(new BigDecimal(mDoubleValue), 2, RoundingMode.HALF_EVEN); + } + } catch (Exception e) { + BigDecimal bigDecimal = new BigDecimal(0); + return bigDecimal.divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN); + } } + /** + * 鑾峰彇鍊嶆暟 + * + * @param value 鍊� + */ + public static int doubleValue(String value) { + try { + double doubleValue = Double.parseDouble(value); + if (doubleValue > 1000 && doubleValue < 1000 * 1000) { + return 1000; + } else if (doubleValue > 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) { + return 1000 * 1000; + } else if (doubleValue > 1000 * 1000 * 1000) { + return 1000 * 1000 * 1000; + } else { + return 0; + } + } catch (Exception e) { + return 0; + } + + } + + /** + * 杞崲鍗曚綅 + * + * @param value 鍊� + * @param unitType 琛ㄧず鏄崟浣嶏紙鍔熺巼锛岀數閲忥級 + */ + public static String convertUnit(String value, String unitType) { + try { + double doubleValue = Double.parseDouble(value); + if (doubleValue > 1000 && doubleValue < 1000 * 1000) { + return unitType.equals(UnitType.kW) ? UnitType.mW : UnitType.mWh; + } else if (doubleValue > 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) { + return unitType.equals(UnitType.kW) ? UnitType.gW : UnitType.gWh; + } else if (doubleValue > 1000 * 1000 * 1000) { + return unitType.equals(UnitType.kW) ? UnitType.tW : UnitType.tWh; + } else { + return unitType.equals(UnitType.kW) ? UnitType.kW : UnitType.kWh; + } + } catch (Exception e) { + return unitType.equals(UnitType.kW) ? UnitType.kW : UnitType.kWh; + } + + } /** * 鐢熸垚浜岀淮鐮� -- Gitblit v1.8.0