devices: Fix negative Milli

pull/1/head
Marc-Antoine Ruel 9 years ago
parent edc050f0a7
commit fd7cbfd4bf

@ -48,7 +48,11 @@ func (m Milli) Float64() float64 {
// String returns the value formatted as a string. // String returns the value formatted as a string.
func (m Milli) String() 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. // Celsius is a temperature at a precision of 0.001°C.

@ -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) { func TestCelsius(t *testing.T) {
o := Celsius(10010) o := Celsius(10010)
if s := o.String(); s != "10.010°C" { if s := o.String(); s != "10.010°C" {

Loading…
Cancel
Save