Skip to content

Commit

Permalink
Fixed invalid rounding in formatDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Mar 20, 2024
1 parent 6c644f7 commit 967700d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,8 @@ func formatDuration(i time.Duration, millisecondSep string, numberOfMillisecondD
s += strconv.Itoa(seconds) + millisecondSep

// Parse milliseconds
var milliseconds = float64(n/time.Millisecond) / float64(1000)
s += fmt.Sprintf("%."+strconv.Itoa(numberOfMillisecondDigits)+"f", milliseconds)[2:]
var milliseconds = math.Floor(float64(n) / float64(time.Millisecond) / float64(math.Pow(10, 3-float64(numberOfMillisecondDigits))))
s += astikit.StrPad(strconv.FormatFloat(milliseconds, 'f', 0, 64), '0', numberOfMillisecondDigits, astikit.PadLeft)
return
}

Expand Down
2 changes: 2 additions & 0 deletions subtitles_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ func TestFormatDuration(t *testing.T) {
assert.Equal(t, "03:25:45,678", s)
s = formatDuration(34*time.Hour+17*time.Minute+36*time.Second+789*time.Millisecond, ",", 3)
assert.Equal(t, "34:17:36,789", s)
s = formatDuration(12*time.Hour+34*time.Minute+56*time.Second+999*time.Millisecond, ",", 2)
assert.Equal(t, "12:34:56,99", s)
}

0 comments on commit 967700d

Please sign in to comment.