Skip to content

Commit 5889328

Browse files
author
Jay Janssen
committed
fix issue #17
1 parent 0121f03 commit 5889328

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

myqlib/numbers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ func collapse_number(value float64, width int64, precision int64, units UnitsDef
7979
// If we have space for some extra precision, use it
8080
return fmt.Sprintf(fmt.Sprint(`%.`, left-1, `f%s`), raw, unit)
8181
} else {
82-
if factor != 1 && str == fmt.Sprintf("0%s", unit) {
82+
if factor != 1 && raw < 1 && left > 0 && fmt.Sprintf(`%.1f`, raw ) != `1.0` {
83+
// Raw is < 1, therefore str is rounded up. Let's print a decimal instead
84+
return fmt.Sprintf(fmt.Sprint(`%0.`, precision+left, `f%s`), raw, unit)[1:]
85+
} else if factor != 1 && str == fmt.Sprintf("0%s", unit) {
8386
if left > 0 {
8487
// There's still some space left to print something intelligent
8588
return fmt.Sprintf(fmt.Sprint(`%.`, precision+1, `f%s`), raw, unit)[1:]

myqlib/numbers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestOne(t *testing.T) {
2424
assert(`one twenty three k`, `123k`, NumberUnits, 123000, 0, 4)
2525

2626
assert(`point one em`, `.1m`, NumberUnits, 123000, 0, 3)
27+
assert(`point six em`, `.6m`, NumberUnits, 550000, 0, 3)
2728

2829
assert(`twelve m`, `12m`, NumberUnits, 12300000, 0, 4)
2930
assert(`twelve point three m`, `12.3m`, NumberUnits, 12300000, 0, 5)
@@ -35,7 +36,7 @@ func TestOne(t *testing.T) {
3536
assert(`one point oh`, `1.0b`, MemoryUnits, 1, 1, 4)
3637

3738
assert(`five oh oh rounded down`, `.5K`, MemoryUnits, 500, 0, 3)
38-
assert(`five fifty rounded up`, `1K`, MemoryUnits, 550, 0, 3)
39+
assert(`five fifty fit`, `.5K`, MemoryUnits, 550, 0, 3)
3940
assert(`five fifty bee`, `550b`, MemoryUnits, 550, 0, 4)
4041

4142
assert(`one kay`, `1K`, MemoryUnits, 1000, 0, 3)

0 commit comments

Comments
 (0)