From 846877e598c5c5b5a63daaff4257ddb638ae272a Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 14 Apr 2024 16:13:14 +0200 Subject: [PATCH] Do not create new `BigDecimals` if a given object is already `BigDecimal` (#4177) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin GrzeĊ›lowski --- .../java/org/openhab/core/library/types/DecimalType.java | 2 ++ .../java/org/openhab/core/library/types/QuantityType.java | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DecimalType.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DecimalType.java index fd453ec7e..87e26524a 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DecimalType.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DecimalType.java @@ -55,6 +55,8 @@ public class DecimalType extends Number implements PrimitiveType, State, Command this.value = type.toBigDecimal(); } else if (value instanceof HSBType type) { this.value = type.toBigDecimal(); + } else if (value instanceof BigDecimal decimal) { + this.value = decimal; } else { this.value = new BigDecimal(value.toString()); } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java index 8fb81a2c7..27a20a30b 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java @@ -169,7 +169,12 @@ public class QuantityType> extends Number */ public QuantityType(Number value, Unit unit) { // Avoid scientific notation for double - BigDecimal bd = new BigDecimal(value.toString()); + BigDecimal bd; + if (value instanceof BigDecimal decimal) { + bd = decimal; + } else { + bd = new BigDecimal(value.toString()); + } quantity = (Quantity) Quantities.getQuantity(bd, unit, Scale.RELATIVE); }