RoundToEven()
Published Oct 11, 2023
Contribute to Docs
The RoundToEven()
function in Go takes a floating-point number as input and rounds it to the nearest even integer. This function is particularly useful in scenarios where precise rounding of floating-point numbers is required.
Syntax
rounded := math.RoundToEven(number)
The RoundToEven()
function returns a single value of type float64
, which represents the input number rounded to the nearest even integer.
Note these special cases:
- Passing an already even integer will return the same even integer.
- Passing
NaN
(Not-a-Number) will returnNaN
. - Passing positive or negative
Inf
will return value with the same sign as the argument.
Example
This Go code example demonstrates a basic implementation of the RoundToEven()
function.
package mainimport ("fmt""math")func main() {// Define a floating-point numberx := 3.5// Round x to the nearest even integerrounded := math.RoundToEven(x)fmt.Printf("%v rounded to the nearest even integer yields: %v\n", x, rounded)}
The above code results in the following output:
3.5 rounded to the nearest even integer yields: 4
Codebyte Example
The example below is runnable and demonstrates the RoundToEven()
function.
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours