Standard library / time

time.Duration.Round()

Round to nearest multiple of m, ties away from zero; nonpositive m returns d unchanged. Saturates if result overflows. Expected stdout: '-2s\n'.

Formal Syntax & Structure

go syntaxgo
func (d Duration) Round(m Duration) Duration

Parameters & Attributes

NameTypeDescription
mDurationRounding or truncation interval.

Return Value

Duration

Round to nearest multiple of m, ties away from zero; nonpositive m returns d unchanged.

Working Example

Example Snippetgo
package main
import (  "fmt"  "time")
func main() { fmt.Println((-1500 * time.Millisecond).Round(time.Second)) }
Go Reference