[Smhi] Ensure trailing zeroes are removed before updating channels (#11353)

* Ensure unnecessary trailing zeroes are removed, to avoid issues with transformations.

Signed-off-by: Anders Alfredsson <andersb86@gmail.com>

* Change to use setScale()

Signed-off-by: Anders Alfredsson <andersb86@gmail.com>
This commit is contained in:
Anders Alfredsson 2021-10-16 13:38:51 +02:00 committed by GitHub
parent 19cd505b0c
commit 1e57450e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 3 deletions

View File

@ -15,6 +15,7 @@ package org.openhab.binding.smhi.internal;
import static org.openhab.binding.smhi.internal.SmhiBindingConstants.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
@ -218,7 +219,7 @@ public class SmhiHandler extends BaseThingHandler {
newState = new QuantityType<>(value.get(), MetricPrefix.MILLI(SIUnits.METRE));
break;
default:
newState = new DecimalType(value.get());
newState = new DecimalType(value.get().setScale(0, RoundingMode.DOWN));
}
}

View File

@ -18,12 +18,14 @@ import static org.openhab.binding.smhi.internal.SmhiBindingConstants.*;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.ZonedDateTime;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.library.types.DecimalType;
/**
* @author Anders Alfredsson - Initial contribution
@ -101,7 +103,8 @@ public class SmhiTest {
assertEquals(0, pcat0.compareTo(BigDecimal.valueOf(0)));
assertEquals(0, pmean0.compareTo(BigDecimal.valueOf(0)));
assertEquals(0, pmedian0.compareTo(BigDecimal.valueOf(0)));
assertEquals(0, wsymb0.compareTo(BigDecimal.valueOf(2)));
assertEquals(0, wsymb0.compareTo(BigDecimal.valueOf(20)));
assertEquals("20", new DecimalType(wsymb0.setScale(0, RoundingMode.DOWN)).toString());
Forecast forecast1 = timeSeries1.getForecast(TIME.plusHours(1), 0).orElseThrow(AssertionError::new);