round even Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/round-even/ Wed, 25 Mar 2020 18:04:00 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 round even Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/round-even/ 32 32 159787465 Round Even a number in Go (Golang) https://vikasboss.github.io/round-even-number-golang/ https://vikasboss.github.io/round-even-number-golang/#respond Wed, 25 Mar 2020 18:03:54 +0000 https://vikasboss.github.io/?p=1825 Overview math package of GO provides a RoundToEven method that can be used to round to even a number. It returns the nearest integer rounding ties to even. Here you can read...

The post Round Even a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math package of GO provides a RoundToEven method that can be used to round to even a number. It returns the nearest integer rounding ties to even.

Here you can read about use case of RoundToEven – https://mathematica.stackexchange.com/questions/2116/why-round-to-even-integers/2120


Below is the signature of the function. It takes input a float and also returns a float.

func RoundToEven(x float64) float64


Some special cases of RoundToEven function are

  • RoundToEven(±0) = ±0
  • RoundToEven(±Inf) = ±Inf
  • RoundToEven(NaN) = NaN

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.RoundToEven(0.5)
    fmt.Println(res)

    res = math.RoundToEven(1.5)
    fmt.Println(res)

    res = math.RoundToEven(2.5)
    fmt.Println(res)

    res = math.RoundToEven(3.5)
    fmt.Println(res)

    res = math.RoundToEven(4.5)
    fmt.Println(res)
}

Output:

0
2
2
4
4

The post Round Even a number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/round-even-number-golang/feed/ 0 1825