The following code should work for a lot of simple use cases with relatively small numbers and small precision inputs. However, it may not work for some uses cases because of numbers overflowing out of the range of float64 numbers, as well as IEEE-754 rounding errors (other languages have this issue as well).
If you care about using larger numbers or need more precision, the following code may not work for your needs, and you should use a helper library (e.g. https://github.com/shopspring/decimal).
I picked up a one-liner round function from elsewhere, and also made toFixed() which depends on round():
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num * output)) / output
}
Usage:
fmt.Println(toFixed(1.2345678, 0)) // 1
fmt.Println(toFixed(1.2345678, 1)) // 1.2
fmt.Println(toFixed(1.2345678, 2)) // 1.23
fmt.Println(toFixed(1.2345678, 3)) // 1.235 (rounded up)
Answer from David Calhoun on Stack OverflowHello, I'm couple of months into Go, but I'm struggling with a quite basic question. I need to precisely divide decimals and round them to two (or n) decimal places. I know there are external libraries to do calculations with decimals, but I'm looking to solve this with the standard library.
This is where I am so far: https://play.golang.org/p/i54VaI747nM
math: add Round
Printing two digits after decimal in go - Stack Overflow
Rounding value half-up to 2 decimal places in Go - Stack Overflow
go - How to round to at most 2 decimal places, if necessary using golang projection mongodb - Stack Overflow
The following code should work for a lot of simple use cases with relatively small numbers and small precision inputs. However, it may not work for some uses cases because of numbers overflowing out of the range of float64 numbers, as well as IEEE-754 rounding errors (other languages have this issue as well).
If you care about using larger numbers or need more precision, the following code may not work for your needs, and you should use a helper library (e.g. https://github.com/shopspring/decimal).
I picked up a one-liner round function from elsewhere, and also made toFixed() which depends on round():
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num * output)) / output
}
Usage:
fmt.Println(toFixed(1.2345678, 0)) // 1
fmt.Println(toFixed(1.2345678, 1)) // 1.2
fmt.Println(toFixed(1.2345678, 2)) // 1.23
fmt.Println(toFixed(1.2345678, 3)) // 1.235 (rounded up)
You don't need any extra code ... its as simple as
import (
"fmt"
)
func main() {
k := 10 / 3.0
fmt.Printf("%.2f", k)
}
Test Code
You can write:
fmt.Printf("%.2f", 1.22225)
(See http://golang.org/pkg/fmt/.)
If you use the fmt.Printf function there is syntax in the formatting string that lets you print floats to a given level of precision like this. The general syntax is %.[numberofdigits]f.
Examples:
fmt.Printf("%.2f" 1.2222225) // output: 1.22
fmt.Printf("%.2f", 1.356) // output: 1.36
One thing to note is that the round doesn't "carry"
fmt.Printf("%.1f", 1.346)
Will output 1.3, not 1.4. Additionally, negative numbers will behave as expected:
fmt.Printf("%.2f", -1.356) // output: -1.36