Go Trunc()
Published Sep 22, 2023
Contribute to Docs
The Trunc() function returns the integer value of a given number by cutting off the decimals. For example, 10.4 and 10.6 would both be truncated to 10.
Syntax
result := math.Trunc(x)
Where result is the integer value of x returned as a float64, except under the following circumstances:
Trunc(math.Inf(1))yields+Inf.Trunc(math.Inf(-1))yields-Inf.Trunc(NaN)yieldsNaN.Trunc(0)yields0.
Example
The following example implements the math.Trunc() method:
package mainimport ("fmt""math")func main() {fmt.Printf("%f\n", math.Trunc(5.6789))fmt.Printf("%v\n", math.Trunc(-math.Pi))}
The output will be:
5.000000-3
Codebyte Example
The following example is runnable and shows how the Trunc() function can be used in a Go program.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use Go (Golang), an open-source programming language supported by Google!
- Beginner Friendly.6 hours