×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.RoundToEven() Function with Examples

Golang | math.RoundToEven() Function: Here, we are going to learn about the RoundToEven() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 03, 2021

math.RoundToEven()

The RoundToEven() function is an inbuilt function of the math package which is used to get the nearest integer, rounding ties to even.

It accepts a parameter (x), and nearest integer, rounding ties to even.

Syntax

func RoundToEven(x float64) float64

Parameters

  • x : The value whose nearest integer value (rounding ties to even) is to be found.

Return Value

The return type of RoundToEven() function is a float64, it returns the nearest integer, rounding ties to even.

Special Cases

  • RoundToEven(±0) = ±0
    If the parameter is ±0, the function returns the same (±0).
  • RoundToEven(±Inf) = ±Inf
    If the parameter is ±Inf, the function returns the same (±Inf).
  • RoundToEven(NaN) = NaN
    If the parameter is NaN, the function returns the same (NaN).

Example 1

// Golang program to demonstrate the
// example of math.RoundToEven() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.RoundToEven(0.45))
	fmt.Println(math.RoundToEven(0.50))
	fmt.Println(math.RoundToEven(0.65))
	fmt.Println(math.RoundToEven(10.45))
	fmt.Println(math.RoundToEven(15.50))
	fmt.Println(math.RoundToEven(20.65))

	fmt.Println(math.RoundToEven(-0.45))
	fmt.Println(math.RoundToEven(-0.50))
	fmt.Println(math.RoundToEven(-0.65))
	fmt.Println(math.RoundToEven(-10.45))
	fmt.Println(math.RoundToEven(-15.50))
	fmt.Println(math.RoundToEven(-20.65))

	fmt.Println(math.RoundToEven(0))
	fmt.Println(math.RoundToEven(math.Inf(-1)))
	fmt.Println(math.RoundToEven(math.NaN()))
}

Output:

0
0
1
10
16
21
-0
-0
-1
-10
-16
-21
0
-Inf
NaN

Example 2

// Golang program to demonstrate the
// example of math.RoundToEven() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var RoundToEvenX float64

	x = 16
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)

	x = 32.50
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)

	x = 64.60
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)

	x = -128.10
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)

	x = 0
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)

	x = -1
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)

	x = math.Inf(1)
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)

	x = math.NaN()
	RoundToEvenX = math.RoundToEven(x)
	fmt.Println("RoundToEven(", x, ") = ", RoundToEvenX)
}

Output:

RoundToEven( 16 ) =  16
RoundToEven( 32.5 ) =  32
RoundToEven( 64.6 ) =  65
RoundToEven( -128.1 ) =  -128
RoundToEven( 0 ) =  0
RoundToEven( -1 ) =  -1
RoundToEven( +Inf ) =  +Inf
RoundToEven( NaN ) =  NaN

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.