devices: stop using fmt for units

Use strconv directly instead.
pull/1/head
Marc-Antoine Ruel 8 years ago
parent 5b5083e8fb
commit cf6b4f08f0

@ -5,7 +5,7 @@
package devices
import (
"fmt"
"strconv"
)
// Milli is a fixed point value with 0.001 precision.
@ -22,7 +22,7 @@ func (m Milli) String() string {
if d < 0 {
d = -d
}
return fmt.Sprintf("%d.%03d", m/1000, d)
return strconv.Itoa(int(m)/1000) + "." + prefixZeros(3, int(d))
}
// Celsius is a temperature at a precision of 0.001°C.
@ -87,5 +87,17 @@ func (r RelativeHumidity) String() string {
if m < 0 {
m = -m
}
return fmt.Sprintf("%d.%02d%%rH", r/100, m)
return strconv.Itoa(int(r)/100) + "." + prefixZeros(2, int(m)) + "%rH"
}
//
func prefixZeros(digits, v int) string {
// digits is expected to be around 2~3.
s := strconv.Itoa(v)
for len(s) < digits {
// O(n²) but since digits is expected to run 2~3 times at most, it doesn't matter.
s = "0" + s
}
return s
}

@ -60,11 +60,11 @@ func TestKPascal(t *testing.T) {
}
func TestRelativeHumidity(t *testing.T) {
o := RelativeHumidity(5010)
if s := o.String(); s != "50.10%rH" {
o := RelativeHumidity(5006)
if s := o.String(); s != "50.06%rH" {
t.Fatalf("%#v", s)
}
if f := o.Float64(); f > 50.11 || f < 50.09 {
if f := o.Float64(); f >= 50.07 || f <= 50.05 {
t.Fatalf("%f", f)
}
}
@ -74,7 +74,7 @@ func TestRelativeHumidity_neg(t *testing.T) {
if s := o.String(); s != "-50.10%rH" {
t.Fatalf("%#v", s)
}
if f := o.Float64(); f < -50.11 || f > -50.09 {
if f := o.Float64(); f <= -50.11 || f >= -50.09 {
t.Fatalf("%f", f)
}
}

Loading…
Cancel
Save