diff --git a/devices/devices.go b/devices/devices.go index f7c6f84..63d2378 100644 --- a/devices/devices.go +++ b/devices/devices.go @@ -129,7 +129,11 @@ func (r RelativeHumidity) Float64() float64 { // String returns the humidity formatted as a string. func (r RelativeHumidity) String() string { - return fmt.Sprintf("%d.%02d%%rH", r/100, r%100) + m := r % 100 + if m < 0 { + m = -m + } + return fmt.Sprintf("%d.%02d%%rH", r/100, m) } // Environment represents measurements from an environmental sensor. diff --git a/devices/devices_test.go b/devices/devices_test.go index 6b095e1..180d990 100644 --- a/devices/devices_test.go +++ b/devices/devices_test.go @@ -68,3 +68,13 @@ func TestRelativeHumidity(t *testing.T) { t.Fatalf("%f", f) } } + +func TestRelativeHumidity_neg(t *testing.T) { + o := RelativeHumidity(-5010) + if s := o.String(); s != "-50.10%rH" { + t.Fatalf("%#v", s) + } + if f := o.Float64(); f < -50.11 || f > -50.09 { + t.Fatalf("%f", f) + } +}