# 🎠 Round float to any precision in Go

Last updated:
introduction numbers math

To round a floating-point number in Go, you can use the `math.Round()` function from the built-in `math` package. However, this function rounds to the nearest integer, so it cannot be used directly to round a float to a particular precision. But you can use it to create your own function that rounds to any decimal places. All you need to do is:

1. multiply the number to be rounded times 10 to the power of `X`, where `X` is the precision you want to achieve
2. round this raised number to the nearest integer value using the `math.Round()` function
3. divide the rounded number by 10 to the power of `X`, where `X` is the rounding decimal precision

In this way, by rounding the raised number to the nearest integer and then dividing, we get a number rounded to the specified decimal places.

## Examples

The `roundFloat()` is a floating-point rounding function, working as described above. The way it works is shown in the example:

``````package main

import (
"fmt"
"math"
)

func roundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}

func main() {
number := 12.3456789

fmt.Println(roundFloat(number, 2))
fmt.Println(roundFloat(number, 3))
fmt.Println(roundFloat(number, 4))
fmt.Println(roundFloat(number, 5))

number = -12.3456789
fmt.Println(roundFloat(number, 0))
fmt.Println(roundFloat(number, 1))
fmt.Println(roundFloat(number, 10))
}
``````

Output:

``````12.35
12.346
12.3457
12.34568
-12
-12.3
-12.3456789
``````

### Round float to 2 decimal places

Full example of rounding a float to 2 decimal places:

``````package main

import (
"fmt"
"math"
)

func roundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}

func main() {
number := 12.3456789
fmt.Println(roundFloat(number, 2))
}
``````

Output:

``````12.35
``````

### Round float to 3 decimal places

Full example of rounding a float to 3 decimal places:

``````package main

import (
"fmt"
"math"
)

func roundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}

func main() {
number := 12.3456789
fmt.Println(roundFloat(number, 3))
}
``````

Output:

``````12.346
``````

## Round the float to a `string`

In Go, there is another way of rounding numbers often called float truncation. The difference from classical rounding is that you get the result in the form of a `string`, as truncating is carried out by the formatting function `fmt.Sprintf()` or `fmt.Printf()`. To round a float in this way, you must set the formatting verb `%f` as an argument to these functions with a precision value of `%.nf` where `n` specifies the precision, that is, the number of decimal places.

The example below is equivalent to the first example on rounding, except it uses the `fmt.Printf()` function.

``````package main

import (
"fmt"
)

func main() {
number := 12.3456789

fmt.Printf("%.2f\n", number)
fmt.Printf("%.3f\n", number)
fmt.Printf("%.4f\n", number)
fmt.Printf("%.5f\n", number)

number = -12.3456789
fmt.Printf("%.0f\n", number)
fmt.Printf("%.1f\n", number)
fmt.Printf("%.10f\n", number)
}
``````

Output:

``````12.35
12.346
12.3457
12.34568
-12
-12.3
-12.3456789000
``````

As you can see, the results are similar to using our `roundFloat()` function. The difference is when we set a higher precision in the `fmt.Printf()` than the number itself has - then the trailing zeros are displayed in the resulting `string`:

For:

``````fmt.Printf("%.10f\n", number)
``````

you get:

``````-12.3456789000
``````

Below is the same example, but it formats the results into a `string` type variable:

``````package main

import (
"fmt"
)

func main() {
number := 12.3456789

numberString := fmt.Sprintf("%.2f", number)
fmt.Println(numberString)
numberString = fmt.Sprintf("%.3f", number)
fmt.Println(numberString)
numberString = fmt.Sprintf("%.4f", number)
fmt.Println(numberString)
numberString = fmt.Sprintf("%.5f", number)
fmt.Println(numberString)

number = -12.3456789
numberString = fmt.Sprintf("%.0f", number)
fmt.Println(numberString)
numberString = fmt.Sprintf("%.1f", number)
fmt.Println(numberString)
numberString = fmt.Sprintf("%.10f", number)
fmt.Println(numberString)
}
``````

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

#### 🧊 Cube root in Go

shorts numbers math

#### 🟦 Square root in Go

shorts numbers math

#### 🍰 The maximum and minimum value of the int types in Go

shorts numbers math