Home > Backend Development > Golang > golang division rounding

golang division rounding

PHPz
Release: 2023-05-10 10:23:06
Original
3350 people have browsed it

Golang Division and Rounding

Golang is a very popular programming language that provides many convenient mathematical functions, including integer division and floating-point division. In this article, we will explore how to perform division and rounding in Golang, that is, how to perform division and rounding.

Integer division

In Golang, integer division uses the / operator, which will directly discard the decimal part and return an integer value. For example, the following code outputs 3:

fmt.Println(10/3)
Copy after login

This method of division is often called "truncated division" or "integer division" because it returns the integer part of the result and discards the fractional part.

But what should we do if we want to round up or down after division? We can use the math package from the standard library to implement such operations.

Rounding up

Rounding up refers to rounding a decimal value up to the nearest integer, for example:

4.1  -> 5
-4.1 -> -4
Copy after login

Golang provides the math.Ceil function, Rounding up can be achieved. math.Ceil is a built-in floating point function that rounds a floating point number x up to the nearest integer. For example, the following code will round the result of 10/3 up to 4:

import "math"
fmt.Println(math.Ceil(float64(10)/float64(3)))
Copy after login

In this example, we convert the integers 10 and 3 to floating point numbers and pass them as arguments to math.Ceil function. The math.Ceil function returns a float64 value, which we need to convert to int or other types for other operations.

Rounding down

Rounding down refers to rounding a decimal value down to the nearest integer, for example:

4.9  -> 4
-4.9 -> -5
Copy after login

Golang provides math. Floor function, which can round down. math.Floor is a built-in floating point function that rounds a floating point number x down to the nearest integer. For example, the following code will round the result of 10/3 down to 3:

import "math"
fmt.Println(math.Floor(float64(10)/float64(3)))
Copy after login

In this example, we convert the integers 10 and 3 to floating point numbers and pass them as arguments to math. Floor function. The math.Floor function returns a float64 value, which we need to convert to int or other types for other operations.

Rounding

Rounding refers to rounding a decimal value to the nearest integer, for example:

4.1  -> 4
4.5  -> 5
4.9  -> 5
-4.1 -> -4
-4.5 -> -4
-4.9 -> -5
Copy after login

Golang provides the math.Round function, which can achieve rounding. math.Round is a built-in floating point function that rounds a floating point number x to the nearest integer. For example, the following code will round the result of 10/3:

import "math"
fmt.Println(math.Round(float64(10)/float64(3)))
Copy after login

In this example, we convert the integers 10 and 3 to floating point numbers and pass them as arguments to the math.Round function. The math.Round function returns a float64 value, which we need to convert to int or other types for other operations.

Summary

In Golang, when using the / operator for integer division, the decimal part will be truncated directly and the integer value will be returned. If we need to perform division and rounding, we can use the functions in the math package: math.Ceil, math.Floor and math.Round functions to implement operations such as upward rounding, downward rounding and rounding.

The above is the method of division and rounding in Golang introduced in this article. I hope it will be helpful to beginners.

The above is the detailed content of golang division rounding. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template