Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trailling #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions number.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Source: https://gist.github.com/gorhill/5285193
import (
"math"
"strconv"
"strings"
)

var (
Expand Down Expand Up @@ -181,6 +182,9 @@ func FormatFloat(format string, n float64) string {
if len(fracStr) < precision {
fracStr = "000000000000000"[:precision-len(fracStr)] + fracStr
}
if format == "" && n > 0.1 {
return strings.TrimRight(strings.TrimRight(signStr+intStr+decimalStr+fracStr, "0"), ".")
}

return signStr + intStr + decimalStr + fracStr
}
Expand Down
1 change: 1 addition & 0 deletions number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TestStruct struct {
func TestFormatFloat(t *testing.T) {
tests := []TestStruct{
{"default", "", 12345.6789, "12,345.68"},
{"default", "", 123456789, "123,456,789"},
{"#", "#", 12345.6789, "12345.678900000"},
{"#.", "#.", 12345.6789, "12346"},
{"#,#", "#,#", 12345.6789, "12345,7"},
Expand Down
4 changes: 4 additions & 0 deletions times.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"sort"
"strings"
"time"
)

Expand Down Expand Up @@ -113,5 +114,8 @@ func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnit
escaped = ch == '%'
}
}
if blbl == "" {
return strings.Trim(fmt.Sprintf(mag.Format, args...), " ")
}
return fmt.Sprintf(mag.Format, args...)
}
1 change: 1 addition & 0 deletions times_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func TestPast(t *testing.T) {
now := time.Now()
testList{
{"now", Time(now), "now"},

{"1 second ago", Time(now.Add(-1 * time.Second)), "1 second ago"},
{"12 seconds ago", Time(now.Add(-12 * time.Second)), "12 seconds ago"},
{"30 seconds ago", Time(now.Add(-30 * time.Second)), "30 seconds ago"},
Expand Down