Do not create new BigDecimals if a given object is already BigDecimal (#4177)

Signed-off-by: Martin Grześlowski <martin.grzeslowski@gmail.com>
This commit is contained in:
Martin 2024-04-14 16:13:14 +02:00 committed by GitHub
parent 3a435ec1e7
commit 846877e598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View File

@ -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());
}

View File

@ -169,7 +169,12 @@ public class QuantityType<T extends Quantity<T>> extends Number
*/
public QuantityType(Number value, Unit<T> 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<T>) Quantities.getQuantity(bd, unit, Scale.RELATIVE);
}