To round a float number to decimal places in Golang, you can use the following steps:
- Create a function in golang that takes a float number and the desired precision as input parameters and returns the rounded number.
- Calculate the value of 10 to the Power of the precision.
- Multiply the Float number by the value calculated in step 2.
- Round the resulting number using the math.Round() function.
- Divide the rounded number by the value calculated in step 2.
Golang Round function round the floating-point number to the nearest int value, it doesn’t round a float number to any precision.
In this article, we will discuss how to round a float number to 2 decimals, or golang round float to 3 decimal places, or round a float number to particular precision.
How to Round a Float to 2 Decimal Places in Golang
To round a float to 2 decimal places in Golang, you can use the following function:
package main import ( "fmt" "math" ) func RoundFloat(number float64, decimalPlace int) float64 { // Calculate the 10 to the power of decimal place temp := math.Pow(10, float64(decimalPlace)) // Multiply floating-point number with 10**decimalPlace and round it // Divide the rounded number with 10**decimalPlace to get decimal place rounding return math.Round(number*temp) / temp } func main() { x := 10.34298 fmt.Printf("Round of %v to 2 Decimal Places is: %v", x, RoundFloat(x, 2)) }
Output
Round of 10.34298 to 2 Decimal Places is: 10.34
In the above Golang program, we import the math package.
We have created a RoundFloat
function that takes float number and precision as input parameters and returns the decimal place rounding.
How to Round a Float to 3 Decimal Places in Golang
To round a float number to 3 decimal places in golang, you can use the RoundFloat()
function and pass 3 as the second argument.
package main import ( "fmt" "math" ) func RoundFloat(number float64, decimalPlace int) float64 { // Calculate the 10 to the power of decimal place temp := math.Pow(10, float64(decimalPlace)) // Multiply floating-point number with 10**decimalPlace and round it // Divide the rounded number with 10**decimalPlace to get decimal place rounding return math.Round(number*temp) / temp } func main() { x := 827.25924245 fmt.Printf("Round of %v to 3 Decimal Places is: %v", x, RoundFloat(x, 3)) }
Output
Round of 827.25924245 to 3 Decimal Places is: 827.259
In the above golang program, we import the math package to use math.Round()
function. RoundFloat
function round the float number to 3 decimal places.
Conclusion
I hope the above article helped you to round the float number to 2 decimal places or round a float number to 3 decimal places.
To round a float number to a particular precision or decimal place, pass the precision number in the RoundFloat function as given below
RoundFloat(344.9852241,4)
– It will round a float number to 4 decimal places.
You can find more topics about the Golang tutorials on the GolangSpot Home page.
Recommended Content
Golang Float64 Max and Min Value
Golang Max and Min Value of Integer