diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index 9582e2ea9..b4c0f8342 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -26,6 +26,8 @@ object UnitConversions { /** Formats temperature as a string with the unit suffix. */ fun Float.toTempString(isFahrenheit: Boolean): String { + if (this.isNaN()) return "--" + val temp = if (isFahrenheit) celsiusToFahrenheit(this) else this val unit = if (isFahrenheit) "F" else "C" diff --git a/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt b/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt index 16424f7fb..07832a903 100644 --- a/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt +++ b/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt @@ -77,6 +77,12 @@ class UnitConversionsTest { assertEquals("-40°F", (-40.0f).toTempString(true)) } + @Test + fun `toTempString handles NaN`() { + assertEquals("--", Float.NaN.toTempString(false)) + assertEquals("--", Float.NaN.toTempString(true)) + } + @Test fun `celsiusToFahrenheit converts correctly`() { mapOf( diff --git a/feature/node/src/main/kotlin/org/meshtastic/feature/node/component/EnvironmentMetrics.kt b/feature/node/src/main/kotlin/org/meshtastic/feature/node/component/EnvironmentMetrics.kt index 5a9c01966..2143f1916 100644 --- a/feature/node/src/main/kotlin/org/meshtastic/feature/node/component/EnvironmentMetrics.kt +++ b/feature/node/src/main/kotlin/org/meshtastic/feature/node/component/EnvironmentMetrics.kt @@ -145,15 +145,17 @@ internal fun EnvironmentMetrics( } if (hasTemperature() && hasRelativeHumidity()) { val dewPoint = UnitConversions.calculateDewPoint(temperature, relativeHumidity) - add( - DrawableMetricInfo( - Res.string.dew_point, - dewPoint.toTempString(isFahrenheit), - org.meshtastic.feature.node.R.drawable.ic_outlined_dew_point_24, - ), - ) + if (!dewPoint.isNaN()) { + add( + DrawableMetricInfo( + Res.string.dew_point, + dewPoint.toTempString(isFahrenheit), + org.meshtastic.feature.node.R.drawable.ic_outlined_dew_point_24, + ), + ) + } } - if (hasSoilTemperature()) { + if (hasSoilTemperature() && !soilTemperature.isNaN()) { add( DrawableMetricInfo( Res.string.soil_temperature,