From fd7cbfd4bfea4cd6d3d78b0719e06656972c71cf Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Sat, 1 Apr 2017 21:56:18 -0400 Subject: [PATCH] devices: Fix negative Milli --- devices/devices.go | 6 +++++- devices/devices_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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" {