Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 1.4 KB

round-float-golang.md

File metadata and controls

41 lines (32 loc) · 1.4 KB
title author date categories images imageAlts
How to Round a Float in Go
lane
2022-11-13
golang
/img/800/ruinreborn_A_breathtaking_top-down_view_of_an_ancient_Mayan_c_12de6fed-8ce2-4477-8de8-916f9f78f5cd_2.png.webp
Generated with Stable Diffusion. Prompt: 'large round thing, dark, 4k, fantasy'

If you're rounding a floating point number in Go, it's most likely you want to format it in a string. Use the built-in fmt.Sprintf() function.

heightInMeters := 1.76234
msg := fmt.Sprintf("Your height is: %.3f", heightInMeters)
// msg = "Your height is: 1.762"

Round float and store in a float

Use math.Floor, math.Round and math.Ceil from the standard math package.

heightInMeters := 1.76234
roundedDown := math.Floor(x*100)/100 // 1.0
roundedToNearest := math.Round(x*100)/100 // 2.0
roundedUp := math.Ceil(x*100)/100 // 2.0

Round float and store in an int

To store the result as an int, use the same method as before and then cast the result.

heightInMeters := 1.76234
roundedToNearest := int(math.Round(x*100)/100) // 2

PS: I've got a fully interactive Golang course here if you're interested in learning more about Go. It's free to start!