diff --git a/devices/devices.go b/devices/devices.go index 7a61e36..e27a953 100644 --- a/devices/devices.go +++ b/devices/devices.go @@ -48,7 +48,11 @@ func (m Milli) Float64() float64 { // String returns the value formatted as a string. func (m Milli) String() string { - return fmt.Sprintf("%d.%03d", m/1000, m%1000) + d := m % 1000 + if d < 0 { + d = -d + } + return fmt.Sprintf("%d.%03d", m/1000, d) } // Celsius is a temperature at a precision of 0.001°C. diff --git a/devices/devices_test.go b/devices/devices_test.go index b60cbf5..6b095e1 100644 --- a/devices/devices_test.go +++ b/devices/devices_test.go @@ -16,6 +16,16 @@ func TestMilli(t *testing.T) { } } +func TestMilli_neg(t *testing.T) { + o := Milli(-10010) + if s := o.String(); s != "-10.010" { + t.Fatalf("%#v", s) + } + if f := o.Float64(); f > -10.009 || f < -10.011 { + t.Fatalf("%f", f) + } +} + func TestCelsius(t *testing.T) { o := Celsius(10010) if s := o.String(); s != "10.010°C" {