How to divide and round in golang

DDD
Release: 2023-12-14 14:05:01
Original
1326 people have browsed it

Golang division and rounding method: 1. Use integers to divide and then round, divide by converting the dividend and divisor into integers, and then round to get the result; 2. Use the trunc function of the math package , Golang's math package provides the Trunc function, which can truncate floating point numbers; 3. Use the Floor function of the math package. Golang's math package provides the Floor function, which can round down floating point numbers. wait.

How to divide and round in golang

# Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In Golang, there are many ways to implement division and rounding. Below I will introduce three commonly used methods:

Method 1: Use integers to divide and then round

This method is the simplest and most direct method. The dividend and divisor are converted to integers for division, and then rounded to obtain the result. The sample code is as follows:

package main import ( "fmt" ) func main() { dividend := 10 divisor := 3 result := dividend / divisor fmt.Println(result) }
Copy after login

The output result is 3, which is the integer part of 10 divided by 3.

Method 2: Use the Trunc function of the math package

The Golang math package provides the Trunc function, which can truncate floating point numbers. The sample code is as follows:

package main import ( "fmt" "math" ) func main() { dividend := 10.0 divisor := 3.0 result := math.Trunc(dividend / divisor) fmt.Println(result) }
Copy after login

The output result is also 3.

Method 3: Use the Floor function of the math package

Another commonly used method is to use the Floor function of the math package, which can take down floating point numbers. all. The sample code is as follows:

package main import ( "fmt" "math" ) func main() { dividend := 10.0 divisor := 3.0 result := math.Floor(dividend / divisor) fmt.Println(result) }
Copy after login

The same output result is 3.

It should be noted that the above three methods are all for rounding floating point numbers. If you want to round integers, you can directly use the result of dividing integers.

The above are three common methods to implement division and rounding in Golang. Choose the appropriate method to perform division and rounding operations according to the actual situation.

The above is the detailed content of How to divide and round in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!